python实现备份目录的方法


Posted in Python onAugust 03, 2015

本文实例讲述了python实现备份目录的方法。分享给大家供大家参考。具体如下:

备份脚本1:

#!/usr/bin/python
# Filename: backup_ver1.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 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 'Successful backup to', target
else:
  print 'Backup FAILED'

输出:

$ python backup_ver1.py
Successful backup to /mnt/e/backup/20041208073244.zip

备份脚本2:

#!/usr/bin/python
# Filename: backup_ver2.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')
# 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
# The name of the zip file
target = today + os.sep + now + '.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 'Successful backup to', target
else:
  print 'Backup FAILED'

输出:

$ python backup_ver2.py
Successfully created directory /mnt/e/backup/20041208
Successful backup to /mnt/e/backup/20041208/080020.zip
$ python backup_ver2.py
Successful backup to /mnt/e/backup/20041208/080428.zip

备份脚本3:

#!/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'

输出:

$ python backup_ver4.py
Enter a comment --> added new examples
Successful backup to /mnt/e/backup/20041208/082156_added_new_examples.zip
$ python backup_ver4.py
Enter a comment -->
Successful backup to /mnt/e/backup/20041208/082316.zip

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
python生成指定长度的随机数密码
Jan 23 Python
Python提示[Errno 32]Broken pipe导致线程crash错误解决方法
Nov 19 Python
详解python3中的真值测试
Aug 13 Python
PyQt5 加载图片和文本文件的实例
Jun 14 Python
Flask使用Pyecharts在单个页面展示多个图表的方法
Aug 05 Python
python的移位操作实现详解
Aug 21 Python
python中对_init_的理解及实例解析
Oct 11 Python
tensorflow tf.train.batch之数据批量读取方式
Jan 20 Python
pytorch快速搭建神经网络_Sequential操作
Jun 17 Python
Python2.x与3​​.x版本有哪些区别
Jul 09 Python
Python 使用 PyQt5 开发的关机小工具分享
Jul 16 Python
Python绘画好看的星空图
Mar 17 Python
python使用MySQLdb访问mysql数据库的方法
Aug 03 #Python
浅谈Python中列表生成式和生成器的区别
Aug 03 #Python
详解Python3中的Sequence type的使用
Aug 01 #Python
将Python代码嵌入C++程序进行编写的实例
Jul 31 #Python
Python制作数据导入导出工具
Jul 31 #Python
简单理解Python中的装饰器
Jul 31 #Python
python简单分割文件的方法
Jul 30 #Python
You might like
php正则校验用户名介绍
2008/07/19 PHP
一个php Mysql类 可以参考学习熟悉下
2009/06/21 PHP
关于PHP结束标签的使用细节探讨及联想
2013/03/04 PHP
PHP框架Swoole定时器Timer特性分析
2014/08/19 PHP
php去除二维数组的重复项方法
2015/11/03 PHP
PHP实现的简单异常处理类示例
2017/05/04 PHP
PHP-CGI远程代码执行漏洞分析与防范
2017/05/07 PHP
php mysql数据库操作类(实例讲解)
2017/08/06 PHP
jquery 上下滚动广告
2009/06/17 Javascript
Jquery 最近浏览过的商品的功能实现代码
2010/05/14 Javascript
Javascript 异步加载详解(浏览器在javascript的加载方式)
2012/05/20 Javascript
jquery动态调整div大小使其宽度始终为浏览器宽度
2014/06/06 Javascript
纯javascript版日历控件
2016/11/24 Javascript
vue使用drag与drop实现拖拽的示例代码
2017/09/07 Javascript
利用CDN加速react webpack打包后的文件详解
2018/02/22 Javascript
详解vue组件基础
2018/05/04 Javascript
原生JS实现的自动轮播图功能详解
2018/12/28 Javascript
vue 需求 data中的数据之间的调用操作
2020/08/05 Javascript
[02:51]2018年度DOTA2最佳中单位选手-完美盛典
2018/12/17 DOTA
Python设计足球联赛赛程表程序的思路与简单实现示例
2016/06/28 Python
python音频处理用到的操作的示例代码
2017/10/27 Python
【python】matplotlib动态显示详解
2019/04/11 Python
Python3内置模块random随机方法小结
2019/07/13 Python
django如何实现视图重定向
2019/07/24 Python
selenium 多窗口切换的实现(windows)
2020/01/18 Python
利用纯CSS3实现文字向右循环闪过效果实例(可用于移动端)
2017/06/15 HTML / CSS
使用分层画布来优化HTML5渲染的教程
2015/05/08 HTML / CSS
国际性能运动服装品牌:Dare 2b
2018/07/27 全球购物
Skyscanner加拿大:全球旅行搜索平台
2018/11/19 全球购物
会计实习自我鉴定
2013/12/04 职场文书
旅游项目开发策划书
2014/01/18 职场文书
个人求职信范例
2014/01/29 职场文书
群众路线领导班子整改方案
2014/10/25 职场文书
2014年园林绿化工作总结
2014/12/11 职场文书
2016入党培训心得体会范文
2016/01/08 职场文书
解决Nginx 配置 proxy_pass 后 返回404问题
2021/03/31 Servers