Django备份mysql数据库

DB_USER = 'root'
DB_PASSWORD = 'root'
DB_NAME = 'dbshow'
BACK_DIR = r'C:\Users\admin\Desktop\test\\'
TODAY = time.strftime('%Y-%m-%d')  # -%H-%M-%S
TODAY_DIR = BACK_DIR + TODAY

#备份数据库
def backupsql():
    # 如果目录不存在,新建目录
    if not os.path.exists(TODAY_DIR):
        os.makedirs(TODAY_DIR)
    # 执行mysql命令,导出数据库到新建的文件
    sqlcmd = "mysqldump  --single-transaction -u" + DB_USER + " -p" + DB_PASSWORD + " " + DB_NAME + " > " + TODAY_DIR + "/" + DB_NAME + ".sql"
    print(sqlcmd)
    os.system(sqlcmd)

backupsql()

你可能感兴趣的:(django,数据库,mysql)