Daily automatic backup of your data

In order to prevent downtime disasters, I use python's schedule library to regularly do daily automatic data backups.

See db_backup.py in the shared code.

import schedule
import time
import xconfig as C
import util.xtool as xtool
import util.tradeDate as TD
import util.dbcom as dbcom
import dataMan.dbMan as dbMan

runTime = "23:30:00"
print('backup DB, waiting to start at %s ...' % (runTime))

def db_backup(title):
    print(title)
    if xtool.nowDate() in TD.tradeDate:
            dbcom.backup(dbName='future')
            dbcom.backup(dbName='acc')
            dbMan.dumpAdog(db_name='future')
            dbMan.dumpAdog(db_name='userDB')
    else:
        print('%s not in tradeDate'%(xtool.nowDate()))
  
schedule.every().day.at(runTime).do(db_backup, 'start to db_backup....')

while True:
    schedule.run_pending()
    time.sleep(1)

in:

schedule.every().day.at(runTime).do(db_backup, 'start to db_backup....')

Is timed to automatically set any time to perform tasks.

The backup() method is to automatically back up a database in MySQL. The core statement is:

cmd="mysqldump -h%s -P%s -u%s -p%s %s --default_character-set=%s > %s/%s.sql" %( host, port,user,pwd,dbName,dbCharset,backupDir,dbName )
os.system(cmd)

The dump() method is to automatically back up a database of mongodb. The core statement is:

com='mongodump -u user -p password --port 27017  --authenticationDatabase admin -d %s -o /home/xfs/adog/ftp/db/mongo/%s/'%(db_name,xtool.nowDate())
os.system(com)

If you have any questions, please see Zhihu (A Dao Ge) personal homepage scan the code "Zhi Shi Planet (A Dao Ge)" to join the discussion.

Guess you like

Origin blog.csdn.net/weixin_41192839/article/details/123412391