#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
简明python教程笔记(5)压缩备份
最新推荐文章于 2022-03-07 09:38:59 发布