下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
编写一个Python脚本,实现为重要的文件或文件夹在指定的目录下创建备份。[设计思路]
[1] 将需要备份的文件和目录由一个列表指定,通过传入参数获得并保存到列表中。
[2] 备份应该保存在主备份目录中。
[3] 将文件备份成一个压缩文件。
[4] 每一次备份都根据当前的日期在主备份目录中创建一个子文件夹,而所备份的文件命名为当期的时间保存在这个子文件夹中。
[5] 压缩命令由本地用户决定。可以使用任何本地的存档压缩命令,只要它有命令行界面就可以了,那样就可以从脚本中传递参数给它。
[参考]
[1] A Byte of Python,2005
[2] Python Manuals 2.6
#! /usr/bin/python # filename: backup_ver1.py # 2010-7-12 wcdj import os import time import sys # 1,The files and directorIEs to be backed up are specifIEd in a List # source = ['/home/wcdj/my_prog','/home/wcdj/local_installed'] # The following information is the deBUG used print '--------------------------------' source=[] print 'The command line arguments are: ' for i in sys.argv: print i if i == sys.argv[0]: continue source.append(i) print source print '--------------------------------' # check input,if error app exit if len(source) == 0: print '''''You should input the files or directorIEs,like python backup_ver1.py /home/wcdj/myfile /home/wcdj/mydir ...''' exit() else: print 'Some files or directorIEr will be saved into .tar.gz format: ' print source # If you are using windows,use # source=[r'c:/documents',r'd:/work'] or # source=['c://documents','d://work'] or # something like that # 2,The backup must be stored in a main backup directory # Remember to change this to what you will be using target_dir = '/home/wcdj/backup/' # 3,The files are backed up into a tar file # 4,The name of subdirectory and tar file today = target_dir + time.strftime('%Y%m%d') Now = time.strftime('%H%M%s') # Take a comment from the user to create the name of the tar file comment = raw_input('Enter a comment: ') if len(comment) == 0:# check if a comment was entered target = today + os.sep + Now + '.tar.gz' else: target = today + os.sep + Now + '_' + / comment.replace(' ','_') + '.tar.gz' #Create the subdirectory if it isn't already there if not os.path.exists(today): os.mkdir(today)# make directory print 'Successfully created directory',today # 5,We use the tar command(in Unix/linux) to put the files in a tgz archive tar_command = "tar -zcf %s %s" % (target,' '.join(source)) # Run the backup if os.system(tar_command) == 0: print 'Successful backup to',target else: print 'Backup Failed' # end
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的一个Python备份脚本全部内容,希望文章能够帮你解决一个Python备份脚本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)