完成1.0版本,学到不少东西,感谢唐唐。
查看系统PATH变量的值:
wmic ENVIRONMENT where "name='path' and UserName='<system>'" get VariableValue /value
在windows系统变量PATH里增加:D:\Program Files (x86)\WinRAR;D:\Python27
wmic ENVIRONMENT where "name='path' and UserName='<system>'" set VariableValue = "%PATH%;D:\Program Files (x86)\WinRAR;D:\Python27"
注意:Windows 7里这个命令不能直接在cmd.exe里执行,会提示拒绝访问的错误,以管理员身份登录也不行。我的解决方法是以管理员身份运行C:\Windows\SysWOW64\cmd.exe。
程序代码:
# -*- coding: cp936 -*-
# Filename: backup_ver1.0.py
'''Windows系统下的文件备份脚本
在Windows系统下通过调用winrar的rar.exe,备份由参数指定的目录或文件。
备份文件的文件名里包含当天的日期。
'''
import os
import time
import sys
def backup():
'''执行备份操作
'''
# 如果目录不存在就建立
if not os.path.exists(target_dir):
os.mkdir(target_dir)
# 用winrar的rar命令压缩文件
zip_command = "rar a %s %s" %(target_dir + target_file, source)
# 执行压缩打包命令
if os.system(zip_command)==0:
print('备份成功')
else:
print('备份失败')
if __name__ == "__main__":
# 定义备份文件存放目录和文件名
target_dir = 'D:\\backup\\'
target_file = 'web_backup_' + time.strftime('%Y%m%d') + '.rar'
# 获取程序参数
if len(sys.argv) < 2:
print '没有指定参数'
sys.exit()
if sys.argv[1].startswith('--'):
#抓取第1个参数--后面的字符串
option = sys.argv[1][2:]
if option == 'version':
print 'Version 1.0'
sys.exit()
elif option == 'help':
print '''\
在Windows系统下通过调用winrar的rar.exe,备份由参数指定的目录或文件。
执行程序前需要把rar.exe的路径加入PATH变量。
备份文件的文件名里包含当天的日期。
备份到D:\backup\目录下。
--version : 输出版本号
--help : 输出帮助信息'''
sys.exit()
else:
print '未知参数'
sys.exit()
else:
source = ''
for filename in sys.argv[1:]:
print filename
source += ' '+filename
print 'source = ', source, '\n'
# 确认PATH变量里是否有winrar目录
path_value = os.popen('wmic ENVIRONMENT where "name=\'path\'" get VariableValue /value')
have_rar = path_value.read().find('WinRAR')
if have_rar == '-1':
print '请在系统PATH变量里设置WinRAR的路径'
sys.exit()
backup()
1.1版本
# -*- coding: cp936 -*-
# Filename: backup_ver1.0.py
'''Windows系统下的文件备份脚本
在Windows系统下通过调用winrar的rar.exe,备份由参数指定的目录或文件。
备份文件的文件名里包含当天的日期。
通过参数传递变量值到backup()
'''
import os
import time
import sys
def backup(target_dir, target_file, source):
'''执行备份操作
'''
# 如果目录不存在就建立
if not os.path.exists(target_dir):
os.mkdir(target_dir)
# 用winrar的rar命令压缩文件
zip_command = "rar a %s %s" %(target_dir + target_file, source)
# 执行压缩打包命令
if os.system(zip_command)==0:
print('备份成功')
else:
print('备份失败')
if __name__ == "__main__":
# 获取程序参数
if len(sys.argv) < 2:
print '没有指定参数'
sys.exit()
if sys.argv[1].startswith('--'):
#抓取第1个参数--后面的字符串
option = sys.argv[1][2:]
if option == 'version':
print 'Version 1.0'
sys.exit()
elif option == 'help':
print '''\
在Windows系统下通过调用winrar的rar.exe,备份由参数指定的目录或文件。
执行程序前需要把rar.exe的路径加入PATH变量。
备份文件的文件名里包含当天的日期。
备份到D:\backup\目录下。
--version : 输出版本号
--help : 输出帮助信息'''
sys.exit()
else:
print '未知参数'
sys.exit()
else:
source = ''
for filename in sys.argv[1:]:
print filename
source += ' '+filename
print 'source = ', source, '\n'
# 确认PATH变量里是否有winrar目录
path_value = os.popen('wmic ENVIRONMENT where "name=\'path\'" get VariableValue /value')
have_rar = path_value.read().find('WinRAR')
if have_rar == '-1':
print '请在系统PATH变量里设置WinRAR的路径'
sys.exit()
# 定义备份文件存放目录和文件名
bak_dir = 'D:\\backup\\'
bak_file = 'web_backup_' + time.strftime('%Y%m%d') + '.rar'
backup(bak_dir, bak_file, source)
输出:
1.txt
source = 1.txt
2.txt
source = 1.txt 2.txt
RAR 4.10 beta 5 版权 (C) 1993-2011 Alexander Roshal 15 十二月 2011
共享版本 输入 RAR -? 获得帮助
评估版本,请注册
正在更新 压缩文件 D:\backup\web_backup_20120111.rar
正在更新 2.txt 完成
正在添加 1.txt 完成
完成
备份成功
1.2版本:
# -*- coding: cp936 -*-
# Filename: backup_ver1.2.py
'''Windows系统下的文件备份脚本
在Windows系统下备份由参数指定的文件,不能备份目录。
备份文件的文件名里包含当天的日期。
1.1 通过参数传递变量值到backup()。
1.2 用zipfile模块替换rar.exe,判断被压缩文件是否存在
'''
import os
import time
import sys
import zipfile
def backup(target_dir, target_file, source):
'''执行备份操作
'''
# 如果目录不存在就建立
if not os.path.exists(target_dir):
os.mkdir(target_dir)
filename = target_dir + target_file
# 用zipfile模块压缩文件
# zipfile每次只能把1个文件加入zip文件,所以写入多个文件时用'a'模式,而不是'w'模式
z = zipfile.ZipFile(filename, mode="a")
for sourcefile in source:
# 判断文件是否存在
if os.path.exists(sourcefile):
res = z.write(sourcefile)
print sourcefile, "压缩成功"
#print res
else :
print sourcefile, "文件或目录不存在"
z.close()
# 判断压缩文件是否存在
if os.path.getsize(filename) == 0:
print('备份失败')
else:
print('备份成功')
if __name__ == "__main__":
# 获取程序参数
if len(sys.argv) < 2:
print '没有指定参数'
sys.exit()
if sys.argv[1].startswith('--'):
#抓取第1个参数--后面的字符串
option = sys.argv[1][2:]
if option == 'version':
print 'Version 1.0'
sys.exit()
elif option == 'help':
print '''\
在Windows系统下通过通过ZIP打包的方式备份由参数指定的目录或文件。
备份文件的文件名里包含当天的日期。
备份到D:\backup\目录下。
--version : 输出版本号
--help : 输出帮助信息'''
sys.exit()
else:
print '未知参数'
sys.exit()
# 定义备份文件存放目录和文件名
bak_dir = 'D:\\backup\\'
bak_file = 'web_backup_' + time.strftime('%Y%m%d') + '.zip'
backup(bak_dir, bak_file, sys.argv[1:])
1.3版本
# -*- coding: cp936 -*-
# Filename: backup_ver1.3.py
'''Windows系统下的文件备份脚本
在Windows系统下用tarfile模块备份由参数指定的目录或文件。
备份文件的文件名里包含当天的日期。
1.1 通过参数传递变量值到backup()。
1.2 用zipfile模块替换rar.exe,判断被压缩文件是否存在
1.3 改用tarfile模块,先对所有文件和目录做tar包,再对tar包压缩
'''
import os
import time
import sys
import tarfile
# 因为压缩目标可能是多个目录,所以先用tar打包,再调用gz/bzip2对tar包做压缩
def backup(target_dir, target_file, source, compression):
'''执行备份操作
'''
# 如果目录不存在就建立
if not os.path.exists(target_dir):
os.mkdir(target_dir)
# 连结备份文件的目录名和文件名
# 以下两个语句的效果相同
#dist_tar_full_path_filename = '%s%s.tar' % (target_dir, target_file)
dist_tar_full_path_filename = target_dir + target_file + ".tar"
out = tarfile.TarFile.open(dist_tar_full_path_filename, 'a')
# 用tar对目标文件或目录列表进行打包
for sourcefile in source:
print sourcefile
if os.path.exists(sourcefile):
out.add(sourcefile)
print sourcefile, "压缩成功"
else:
print sourcefile, "不存在"
# tar打包过程结束
out.close( )
# 判断是否对tar包进行压缩
# print "compression = ", compression
if compression:
# 压缩后的文件名
dist_compress_file = dist_tar_full_path_filename + "." + compression
# 对tar包进行压缩
out = tarfile.TarFile.open(dist_compress_file, 'w:'+compression)
out.add(dist_tar_full_path_filename)
out.close()
# 判断压缩后的文件是否存在
if os.path.getsize(dist_compress_file) == 0:
print('备份失败')
else:
print('备份成功')
# 删除tar包
os.remove(dist_tar_full_path_filename)
else:
# 判断tar包是否存在
if os.path.getsize(dist_tar_full_path_filename) == 0:
print('备份失败')
else:
print('备份成功')
if __name__ == "__main__":
# 获取程序参数
if len(sys.argv) < 2:
print '没有指定参数'
sys.exit()
if sys.argv[1].startswith('--'):
#抓取第1个参数--后面的字符串
option = sys.argv[1][2:]
if option == 'version':
print 'Version 1.0'
sys.exit()
elif option == 'help':
print '''\
在Windows系统下通过通过打包的方式备份由参数指定的目录或文件。
备份文件的文件名里包含当天的日期。
备份到D:\backup\目录下。
--version : 输出版本号
--help : 输出帮助信息'''
sys.exit()
else:
print '未知参数'
sys.exit()
# 定义备份文件存放目录和文件名
bak_dir = 'D:\\backup\\'
bak_file = 'web_backup_' + time.strftime('%Y%m%d')
backup(bak_dir, bak_file, sys.argv[1:], "gz")