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

Posted Ray雷

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python自动拉取备份压缩包并删除3天前的旧备份相关的知识,希望对你有一定的参考价值。

业务场景,异地机房自动拉取已备份好的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天前的旧备份的主要内容,如果未能解决你的问题,请参考以下文章

mysql自动备份删除5天前的备份

sql server 2005怎么自动定时删除3天前的备份文件?

sql server 2005怎么自动定时删除3天前的备份文件?

mysql自动备份删除5天前的备份

ORACLE自动备份-压缩格式,定期删除.bat

自动备份MYSQL数据库,自动删除10天前的备份