用Python写了一个定时备份Mysql的脚本,Python的好处就是代码简单,而且一看就懂。
这个程序基本上改改基本的配置,就能完成绝大多数的Mysql定期备份的需求了。
把代码贴在下面:
strSqlFile = StrBackupDir + strSqlFile
strBackupCMD = StrBackupCMD + strSqlFile
print strBackupCMD
#mysqldump the DB to the new sql file
os.system( strBackupCMD )
#tar the sql file to the zip file
strTarFile = StrBackupFile + str( yesterday ) + ".tar.gz"
strTarFile = StrBackupDir + strTarFile
strTarCMD = StrTarCMD + strTarFile + " " + strSqlFile
print strTarCMD
#execute the tar cmd
os.system( strTarCMD )
#rm the sql file after zip
strDelSqlCMD = "rm " + strSqlFile
print strDelSqlCMD
#execute the del cmd
os.system( strDelSqlCMD )
#rm the old zip file which is born 15 days ago
oldday = today - datetime.timedelta( days = IntOlddayCount )
strOldTarFile = StrBackupFile + str( oldday ) + ".tar.gz"
strOldTarFile = StrBackupDir + strOldTarFile
strDelOldTarCMD = "rm " + strOldTarFile
print strDelOldTarCMD
#execute the del old zip file cmd
os.system( strDelOldTarCMD )