无聊的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()


本文出自 “willaim” 博客,请务必保留此出处http://swq499809608.blog.51cto.com/797714/1363305

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。