python windowserver服务器异地备份

python windowserver服务器异地备份

要求:将数据库备份文件定时备份到共享盘
思路:
python windowserver服务器异地备份_第1张图片

1.备份源目录

source = r"C:\Users\XXXX\Desktop\20200615"

2.备份

def zip_file_local(source,target_path): # 本地备份
    print("<<<<<文件开始备份,请等待>>>>>\n")
    for path ,dir,filenames in os.walk(source):
        for filename in filenames:
            # print(filename)
            old_path = os.path.join(path, filename)
            mailtime = os.path.getmtime(old_path)
            filetime = (datetime.datetime.now() - datetime.datetime.fromtimestamp(mailtime)).days
            if filetime >= 0:   # 当前日期与备份文件的日期差

                # 以当前日期和时间为文件名命名生成的压缩文件
                target = target_path + \
                         time.strftime('%Y') + \
                         time.strftime('%m') + \
                         time.strftime('%d') + '.rar'


                # 使用zip命令压缩文件
                zip_command = "WinRAR a {0} \"{1}\"".format(target, ''.join(old_path))
                # 通过给系统传递参数来执行压缩命令(压缩使用的是WinRAR所带文件rar.exe来执行压缩)
                if os.system(zip_command) == 0:
                    print('{} Successful backup to {}'.format(filename, target))
                    os.remove(old_path)   # 每次遍历压缩之后删除源文件
                else:
                    print(os.system(zip_command))
                    print('{} Backup Faild {}'.format(filename, target))
            else:

                print("\n{}-未达到备份时间或者不符合备份规则\n".format(filename))
    print("<<<<<文件本地备份已结束,即将开始异地备份>>>>>\n")
    print("""
***************************************************
    """)

def zip_file_other(target_dir,target_other_dir):  #  异地备份
    print("\n<<<<<异地备份进行中>>>>>\n")
    os.system('net use z: \\\\192.168.1.210\\temp mima /user:administrator')
    for path, dir, filenames in os.walk(target_dir):
        if len(filenames) == 0:
            print("目录文件空")
            break
        else:
            for filename in filenames:
                # print(filename)
                old_path = os.path.join(path, filename)
                # print(old_path)
                mailtime = os.path.getmtime(old_path)
                filetime = (datetime.datetime.now() - datetime.datetime.fromtimestamp(mailtime)).days
                if filetime == 0:
                    shutil.copyfile(old_path, "{}{}".format(target_other_dir,filename))
                    print("{} Successful backup to other dir{}".format(filename,target_other_dir))
                else:
                    print("{} 未达到备份时间或者不符合备份规则\n".format(filename))

    print("\n<<<<<异地备分已完成>>>>> \n")

3.备份目的目录

target_path = r'i:\EPM-backup\{}'.format("epm-")  #本地备份路径+文件名起始
target_dir = os.path.dirname(target_path)  #本地备份目录
target_other_dir = "z:\\"   #共享盘目录

4.完整代码:

import os
import time
import shutil
import datetime
"""
备份文件:当前目录、共享目录各保留一分压缩包,删除源文件
备份源文件特征:时间大于当前日期一个月的
备份后文件命名规则:文件属性+当前年月日
"""
source = r"C:\Users\IT-huangyj\Desktop\20200615"
target_path = r'i:\EPM-backup\{}'.format("epm-")
target_dir = os.path.dirname(target_path)
target_other_dir = "z:\\"
def zip_file_local(source,target_path):
    print("<<<<<文件开始备份,请等待>>>>>\n")
    for path ,dir,filenames in os.walk(source):
        for filename in filenames:
            # print(filename)
            old_path = os.path.join(path, filename)
            mailtime = os.path.getmtime(old_path)
            filetime = (datetime.datetime.now() - datetime.datetime.fromtimestamp(mailtime)).days
            if filetime >= 0:   # 当前日期与备份文件的日期差

                # 以当前日期和时间为文件名命名生成的压缩文件
                target = target_path + \
                         time.strftime('%Y') + \
                         time.strftime('%m') + \
                         time.strftime('%d') + '.rar'


                # 使用zip命令压缩文件
                zip_command = "WinRAR a {0} \"{1}\"".format(target, ''.join(old_path))
                # 通过给系统传递参数来执行压缩命令(压缩使用的是WinRAR所带文件rar.exe来执行压缩)
                if os.system(zip_command) == 0:
                    print('{} Successful backup to {}'.format(filename, target))
                    # os.remove(old_path)
                else:
                    print(os.system(zip_command))
                    print('{} Backup Faild {}'.format(filename, target))
            else:

                print("\n{}-未达到备份时间或者不符合备份规则\n".format(filename))
    print("<<<<<文件已备份结束,即将开始异地备份>>>>>\n")
    print("""
***************************************************
    """)

def zip_file_other(target_dir,target_other_dir):
    print("\n<<<<<异地备份进行中>>>>>\n")
    os.system('net use z: \\\\192.168.1.210\\temp mima /user:administrator')  # 挂载共享盘为本地盘
    for path, dir, filenames in os.walk(target_dir):
        if len(filenames) == 0:
            print("目录文件空")
            break
        else:
            for filename in filenames:
                # print(filename)
                old_path = os.path.join(path, filename)
                # print(old_path)
                mailtime = os.path.getmtime(old_path)
                filetime = (datetime.datetime.now() - datetime.datetime.fromtimestamp(mailtime)).days
                if filetime == 0:
                    shutil.copyfile(old_path, "{}{}".format(target_other_dir,filename))
                    print("{} Successful backup to other dir{}".format(filename,target_other_dir))
                else:
                    print("{} 未达到备份时间或者不符合备份规则\n".format(filename))

    print("\n<<<<<异地备分已完成>>>>> \n")


if __name__ == '__main__':
    zip_file_local(source, target_path)
    zip_file_other(target_dir,target_other_dir)





你可能感兴趣的:(Python)