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编程语言的35个与众不同之处(语言特征和使用技巧)
Jul 07 Python
Python格式化压缩后的JS文件的方法
Mar 05 Python
Python中字符串对齐方法介绍
May 21 Python
Python tkinter模块弹出窗口及传值回到主窗口操作详解
Jul 28 Python
Python实现将Excel转换成为image的方法
Oct 23 Python
Python使用Slider组件实现调整曲线参数功能示例
Sep 06 Python
python如何从文件读取数据及解析
Sep 19 Python
python3.6连接mysql数据库及增删改查操作详解
Feb 10 Python
基于Python的OCR实现示例
Apr 03 Python
Python map及filter函数使用方法解析
Aug 06 Python
python一些性能分析的技巧
Aug 30 Python
python中pyqtgraph知识点总结
Jan 26 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
用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤
2014/01/19 PHP
php判断访问IP的方法
2015/06/19 PHP
php爬取天猫和淘宝商品数据
2018/02/23 PHP
PHP实现Snowflake生成分布式唯一ID的方法示例
2020/08/30 PHP
JS的数组的扩展实例代码
2008/07/09 Javascript
JS 遮照层实现代码
2010/03/31 Javascript
JavaScript设置首页和收藏页面的小例子
2013/11/11 Javascript
谈谈AngularJs中的隐藏和显示
2015/12/09 Javascript
js格式化时间的方法
2015/12/18 Javascript
jQuery获取父元素节点、子元素节点及兄弟元素节点的方法
2016/04/14 Javascript
jQuery+正则+文本框只能输入数字的实现方法
2016/10/07 Javascript
jQuery Pagination分页插件使用方法详解
2017/02/28 Javascript
JS实现线性表的顺序表示方法示例【经典数据结构】
2017/04/11 Javascript
VueJS 集成 Medium Editor的示例代码 (自定义编辑器按钮)
2017/08/24 Javascript
对angularJs中自定义指令replace的属性详解
2018/10/09 Javascript
微信二次分享报错invalid signature问题及解决方法
2019/04/01 Javascript
google广告之另类js调用实现代码
2020/08/22 Javascript
VUE前端从后台请求过来的数据进行转换数据结构操作
2020/11/11 Javascript
django接入新浪微博OAuth的方法
2015/06/29 Python
Python实现截屏的函数
2015/07/26 Python
python代码实现ID3决策树算法
2017/12/20 Python
python爬虫 urllib模块发起post请求过程解析
2019/08/20 Python
pycharm下配置pyqt5的教程(anaconda虚拟环境下+tensorflow)
2020/03/25 Python
使用keras实现Precise, Recall, F1-socre方式
2020/06/15 Python
Python调用C语言程序方法解析
2020/07/07 Python
Python的collections模块真的很好用
2021/03/01 Python
体育教育个人自荐信范文
2013/12/01 职场文书
个人自荐信
2013/12/05 职场文书
教学实习自我评价
2014/01/28 职场文书
小学庆六一活动方案
2014/02/28 职场文书
优秀的应届生自荐信
2014/05/23 职场文书
专项法律服务方案
2014/06/11 职场文书
财政局个人总结
2015/03/04 职场文书
2016年党员公开承诺书格式范文
2016/03/24 职场文书
未来,这5大方向都很适合创业
2019/07/22 职场文书
一篇文章搞懂python混乱的切换操作与优雅的推导式
2021/08/23 Python