码迷,mamicode.com
首页 > 编程语言 > 详细

python自动拉取备份压缩包并删除3天前的旧备份

时间:2018-02-01 00:10:54      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:name   gpo   point   sys   reading   备份   数据   lin   http   

业务场景,异地机房自动拉取已备份好的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(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()

 

python自动拉取备份压缩包并删除3天前的旧备份

标签:name   gpo   point   sys   reading   备份   数据   lin   http   

原文地址:https://www.cnblogs.com/rayong/p/8395078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!