业务场景,异地机房自动拉取已备份好的tar.gz数据库压缩包,并且只保留3天内的压缩包文件,用python实现
#!/usr/bin/env python import requests,time,os,datetime,platform
from threading import Thread #cd backup dir
if platform.system() == 'Windows':
os.chdir('D:\python\mysqlbackup_all')
elif platform.system() == 'Linux':
os.chdir('/data/mysqlbackup_all/') #Get 3 days ago
now_time = datetime.datetime.now()
yes_time = now_time + datetime.timedelta(days=-3)
yes_time_3day_ago = yes_time.strftime('%Y%m%d%H%M').replace('','') #Get the whole point time
Data_time = time.strftime("%Y%m%d%H%M",time.localtime())
Data_time_30Minute = Data_time.replace('','') #request url
def for_url(url_name):
for i in url_name: file_url = 'http://186.87.1.98/'+i + '/'+ i + '_aaa_' + '' +'.tar.gz' r = requests.get(file_url, stream=True)
with open(i + '_'+Data_time + '.tar.gz', 'wb') as didi:
for chunk in r.iter_content(chunk_size=1024 * 1024):
if chunk:
didi.write(chunk)
print file_url #delete 3 day ago tar.gz file
def delete_3day(url_name):
for i in url_name:
targat_dir = i + '_' + yes_time_3day_ago + '.tar.gz'
delete_home_ssdb = 'rm %s -rf ' % targat_dir
os.system(delete_home_ssdb) def main():
url_name = ('', '', '', 'aa', 'qq', 'baidu', '')
t = Thread(target=for_url(url_name),args=url_name)
t.start()
t.join()
delete_3day(url_name) if __name__ == '__main__':
main()