python自動拉取備份壓縮包並刪除3天前的舊備份

來源:互聯網
上載者:User

標籤:name   gpo   point   sys   reading   備份   資料   lin   http   

業務情境,異地機房自動拉取已備份好的tar.gz資料庫壓縮包,並且只保留3天內的壓縮包檔案,用python實現

#!/usr/bin/env pythonimport requests,time,os,datetime,platformfrom threading import Thread#cd backup dirif platform.system() == ‘Windows‘:    os.chdir(‘D:\python\mysqlbackup_all‘)elif platform.system() == ‘Linux‘:    os.chdir(‘/data/mysqlbackup_all/‘)#Get 3 days agonow_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 timeData_time = time.strftime("%Y%m%d%H%M",time.localtime())Data_time_30Minute = Data_time.replace(‘30‘,‘00‘)#request urldef 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 filedef 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()

 

python自動拉取備份壓縮包並刪除3天前的舊備份

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.