无聊的MYSQL 备份脚本,零时备用着

#!/usr/bin/env python
#-*-coding:utf8-*-
import os,sys,time,traceback
import commands
import shutil
def dbBack():
    try:
        paths = '/usr/home/wangqiang1/dbBackup/'
        times = time.strftime('%Y%m%d%H',time.localtime(time.time()))
        os.chdir(paths)
        cmd = '/usr/bin/mysqldump -uadmin -padmin zabbix > /%s/%s_zabbix.sql'%(paths,times)
        x,y = commands.getstatusoutput(cmd)
        if x == 0:
            os.system('tar -czvf %s_zabbix.tgz %s_zabbix.sql'%(times,times))
            shutil.move('%s_zabbix.sql'%times,'/dev/null')
    except:
        print traceback.format_exc()
          
def delDbs():
    try:
        paths =  '/usr/home/wangqiang1/dbBackup/'
        os.chdir(paths)
    fileall = dict()
        files = os.listdir(paths)
        if len(files) >= 10:
            for i in files:
                strtime = os.path.getctime(i)
                fileall[i] = strtime
        sortfile = fileall.values()
         
        sortfile.sort()
        b = sortfile[10:]
        print b
              
        for d,s in fileall.items():
            if s in b:
                #shutil.move(d,'/opt/')
                #print 'deddlete file %s' %s,d
            else:
                print s,d
    except:
        print traceback.format_exc()       
if __name__ == "__main__":
    #sc = dbBack()
    sd = delDbs()


你可能感兴趣的:(mysql,python,OS,MysqlDump,time)