温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python10.4备份脚本的代码怎么写

发布时间:2021-10-26 15:47:44 阅读:172 作者:柒染 栏目:编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

python10.4备份脚本的代码怎么写,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

版本四的代码是相当长的,所以很多人对其有束手无策之感,但是你通过以下的代码对其,有更好的了解,以下就是相关内容的详细介绍。希望你会有所收获。

#!/usr/bin/python  # Filename: backup_ver4.py  import os  import time  # 1. The files and directories to be backed up are specified in a list.  source = [‘/home/swaroop/byte’‘/home/swaroop/bin’]  # If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that  # 2. The backup must be stored in a main backup directory  target_dir = ‘/mnt/e/backup/’ # Remember to change this to what you will be using  # 3. The files are backed up into a zip file.  # 4. The current day is the name of the subdirectory in the main directory  today = target_dir + time.strftime(‘%Y%m%d’)  # The current time is the name of the zip archive  now = time.strftime(‘%H%M%S’)  # Take a comment from the user to create the name of the zip file  comment = raw_input(‘Enter a comment –> ‘)  if len(comment) == 0: # check if a comment was entered  target = today + os.sep + now + ‘.zip’  else:  target = today + os.sep + now + ‘_’ + \  comment.replace(‘ ‘‘_’) + ‘.zip’  # Notice the backslash!  # 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 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 ‘Successful backup to’, target  else:  print ‘Backup FAILED’ 

文中提到“我还希望有的一个优化是使用tar命令替代zip命令。这样做的一个优势是在你结合使用tar和gzip命令的时候,备份会更快更小。如果你想要在Windows中使用这些归档,WinZip也能方便地处理这些.tar.gz文件。tar命令在大多数Linux/Unix系统中都是默认可用的。Windows用户也可以下载安装它。命令字符串现在将成为:

tar = ‘tar -cvzf %s %s -X /home/swaroop/excludes.txt’ % (target, ‘ ‘.join(srcdir)) 

需要补充的是Windows用户安装完LibArchive后命令为bsdtar,而且若想使用-z这个选项,还需安装gzip软件。代码中的

target = today + os.sep + now + ‘.zip’

改为

target = today + os.sep + now + ‘.tar.gz’

使用

bsdtar -cvzf %s %s -X /home/swaroop/excludes.txt’ % (target, ‘ ‘.join(source)) 

关于python10.4备份脚本的代码怎么写问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×