简明python教程笔记(5)压缩备份

#backup_ver#自动备份文件
'''
import os
import time
#1、the file and directories to be backed up are specified in a list.
source = [r'C:/Documents',r'D:/Work']
#这个是苹果的电脑source = ['/home/swaroop/byte','/home/swaroop/bin']
#1、if you are using windows,use source = [r'C:\Documents',r'D:\Work'] os something like that
#2、The backup must be stored in a main backup directory
target_dir = 'D:/backup' #Remember to change this to what you will be using
#3. The files are backed up into a zip file.
#4.the name of the zip archive is the current date and time
target = target_dir + time.strftime('%Y%m%d%H%M%S')+ '.zip'
#5.We use the zip command(in unix/linux) to put the files in a zip archive
zip_command = "zip-qr'%s'%s"%(target,''.join(source))
#Run the backup
if os.system(zip_command)==0:
    print 'Sussessful backup to ',target
else:
    print 'Backup FAILED'
windows的本件备份方式
'''
#这是备份单个文件的
# import os
# import  time
# source = ['F:\Practice_backup\myfile.txt']
# target_dir ='F:\\'
# target = target_dir+'Practice_backup'+'.zip'
# zip_command = "makecab %s %s" % (source[0],target)
# if os.system(zip_command)==0:
#     print 'Successful backup to',target
# else:
#     print 'backup failed'
#这是备份整个文件夹的没有调试成功
# import os,shutil
# import  time
# path1 = 'F:\Practice_backup'
# path2 = 'F:\Practice_backup2'
# def file_copy(path1,path2):
#         f2 = [filename1 for filename1 in os.listdir(path2)]
#         for filename in os.listdir(path1):
#             if filename  not in f2 :
# #判断是否源文件是否在备份文件夹内存在
#                 path3= os.path.join(path1,filename)
#                 path4=os.path.join(path2,filename)
#                 shutil.copy(path3,path4)
# #同步文件
"""
功能:备份文件
版本:1.0
作者:yuri_cto
"""
import  os,time,sys
d_dir = '/data/backup/'
d_file = 'system_bak.tar.gz'
s_dir = ['/etc', '/boot', '/var/www/html']
date = time.strftime ('%Y%m%d')
r_dir = d_dir + date + '/'
r_name = r_dir + d_file
def all_bak():
    print('Backup Scripts is Starting!~ Please Waitting......')
    print('\033[32m------------------------------------------\033[0m')
    time.sleep (2)
    if os.path.exists (r_dir) == False:
        os.makedirs (r_dir)
        print('The DIR {} create success!'.format (r_dir))
    else:
        print ('The DIR {} is exists!'.format (r_dir))
    tar_cmd = 'tar -czvf {} {}'.format (r_name, ' '.join (s_dir))
    if os.system (tar_cmd) == 0:
        # 打印绿色输出成功信息!~
        print('\033[32mThe backup Files {} exec success!~\033[0m'.format (r_name))
    else:
        print("The backup Files is failed!~")


try:
    if len (sys.argv[1]) == 0:
        print('\033[34mUsage: {} help|all_bak\033[0m'.format (sys.argv[0]))
except IndexError:
    print('\033[34m----------------------------------\033[0m')
    print('                     ')
    print('\033[34mUsage: {} Please Exec help|all_bak\033[0m'.format (sys.argv[0]))
try:
    if sys.argv[1] == 'all_bak':
        all_bak ()
    else:
        print('\033[34m----------------------------------\033[0m')
        print('\033[34mUsage: {} Please Exec help|all_bak\033[0m'.format (sys.argv[0]))
except IndexError:
    pass
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百年渔翁_肯肯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值