|
发表于 2019-1-24 22:12:39
|
显示全部楼层
乔峰 发表于 2019-1-24 22:22
还没这个能力自己写
额。。。那没办法,我的是对应FreeBSD系统的,Linux下没测试,你可以看看
[ol]#!/usr/local/bin/python# -*- coding: utf-8 -*-import osimport sysimport commandsimport tarfilefrom datetime import date,datetime,timedeltafrom qcloud_cos import CosS3Clientfrom qcloud_cos import CosConfig# Qcloud COSSecretID = u'xxx'SecretKey = u'xxx'Bucket = u'xxx'Region = 'ap-chengdu'# BackupToDay = date.today()OldDay = date.today() - timedelta(5)BackDir = '/data/backup'LogFile = BackDir + '/backup.log'DBUser = 'root'DBPass = 'xxx'SQLFile = 'DB_' + str(ToDay.year) + str(ToDay.month) + str(ToDay.day) + '.sql'DBFile = ('/DB_' + str(ToDay.year) + str(ToDay.month) + str(ToDay.day) + '.tgz').decode('utf-8')DBOld = 'DB_' + str(OldDay.year) + str(OldDay.month) + str(OldDay.day) + '.tgz'WebDir = '/data/www'WebFile = ('/Web_' + str(ToDay.year) + str(ToDay.month) + str(ToDay.day) + '.tgz').decode('utf-8')WebOld = 'Web_' + str(OldDay.year) + str(OldDay.month) + str(OldDay.day) + '.tgz'print('Checking backup directory...')try: if not os.path.isdir(BackDir): os.mkdir(BackDir)except IOError, err: print err sys.exit()print('Compress your website directory...')try: tar = tarfile.open(BackDir + WebFile, 'w:gz') pre_len = len(os.path.dirname(WebDir)) for root, dir, files in os.walk(WebDir): for file in files: fullpath = os.path.join(root, file) arcname = fullpath[pre_len:].strip(os.path.sep) tar.add(fullpath, arcname) tar.close()except IOError, err: print err sys.exit()print('Export your databases...')try: cmd = '/usr/local/bin/mysqldump -hxxx.xxx.xxx.xxx -u' + DBUser + ' -p' + DBPass + ' usebsd > ' + BackDir + '/' + SQLFile h = commands.getstatusoutput(cmd) if h[0] != 0: print('Export failed.') sys.exit() else: tar = tarfile.open(BackDir + '/' + DBFile, 'w:gz') tar.add(BackDir + '/' + SQLFile, SQLFile) tar.close() os.remove(BackDir + '/' + SQLFile)except IOError, error: print error sys.exit()print('Prepare upload your backup data to qcloud...')config = CosConfig(Secret_id=SecretID, Secret_key=SecretKey, Region=Region, Token='')client = CosS3Client(config)response = client.upload_file(Bucket=Bucket, LocalFilePath=BackDir + DBFile, Key=DBFile, PartSize=10, MAXThread=10)print 'Databases file %s upload to bucket %s : %s' % (DBFile, Bucket, response['ETag'])response = client.upload_file(Bucket=Bucket, LocalFilePath=BackDir + WebFile, Key=WebFile, PartSize=10, MAXThread=10)print 'Website file %s upload to bucket %s : %s' % (WebFile, Bucket, response['ETag'])print('Delete old backup data...')try: if os.path.isfile(BackDir + '/' + DBOld): os.remove(BackDir + '/' + DBOld) if os.path.isfile(BackDir + '/' + WebOld): os.remove(BackDir + '/' + WebOld) response = client.list_objects(Bucket=Bucket) for item in response['Contents']: if item['Key'] == DBOld or item['Key'] == WebOld: client.delete_object(Bucket=Bucket, Key=item['Key'])except IOError, err: print err sys.exit()print 'Backup finished...'[/ol]复制代码 |
|