業務場景,異地機房自動拉取已備份好的tar.gz數據庫壓縮包,而且只保留3天內的壓縮包文件,用python實現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('30','00') #Get the whole point time Data_time = time.strftime("%Y%m%d%H%M",time.localtime()) Data_time_30Minute = Data_time.replace('30','00') #request url def for_url(url_name): for i in url_name: file_url = 'http://186.87.1.98/'+i + '/'+ i + '_aaa_' + '201801311800' +'.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 = ('123', '456', '789', 'aa', 'qq', 'baidu', '163') t = Thread(target=for_url(url_name),args=url_name) t.start() t.join() delete_3day(url_name) if __name__ == '__main__': main()