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 相关文章推荐
Django中几种重定向方法
Apr 28 Python
Python模拟登录验证码(代码简单)
Feb 06 Python
Python入门_条件控制(详解)
May 16 Python
Tensorflow简单验证码识别应用
May 25 Python
pandas读取csv文件,分隔符参数sep的实例
Dec 12 Python
Python设计模式之享元模式原理与用法实例分析
Jan 11 Python
Python实现网站表单提交和模板
Jan 15 Python
python根据完整路径获得盘名/路径名/文件名/文件扩展名的方法
Apr 22 Python
Django实现文章详情页面跳转代码实例
Sep 16 Python
python process模块的使用简介
May 14 Python
Python&Matlab实现樱花的绘制
Apr 07 Python
PYTHON 使用 Pandas 删除某列指定值所在的行
Apr 28 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过程中的一些注意点的总结
2013/10/25 PHP
PHP中功能强大却很少使用的函数实例小结
2016/11/10 PHP
如何直接访问php实例对象中的private属性详解
2017/10/12 PHP
用apply让javascript函数仅执行一次的代码
2010/06/27 Javascript
JQuery魔力之$("tagName")与selector
2012/03/05 Javascript
Javascript添加监听与删除监听用法详解
2014/12/19 Javascript
微信JS接口汇总及使用详解
2015/01/09 Javascript
jQuery学习笔记之Ajax用法实例详解
2015/12/01 Javascript
使用bootstrap typeahead插件实现输入框自动补全之问题及解决办法
2016/07/07 Javascript
BOM之navigator对象和用户代理检测
2017/02/10 Javascript
jQuery插件HighCharts实现的2D条状图效果示例【附demo源码下载】
2017/03/15 Javascript
SpringMVC+bootstrap table实例详解
2017/06/02 Javascript
Vue2路由动画效果的实现代码
2017/07/10 Javascript
js异步编程小技巧详解
2017/08/14 Javascript
BootStrap给table表格的每一行添加一个按钮事件
2017/09/07 Javascript
js最简单的双向绑定实例讲解
2018/01/02 Javascript
小程序ios音频播放没声音问题的解决
2018/07/11 Javascript
微信小程序实现一张或多张图片上传(云开发)
2019/09/25 Javascript
JavaScript原型式继承实现方法
2019/11/06 Javascript
vue中echarts引入中国地图的案例
2020/07/28 Javascript
关于pip的安装,更新,卸载模块以及使用方法(详解)
2017/05/19 Python
python+matplotlib绘制3D条形图实例代码
2018/01/17 Python
python实现隐马尔科夫模型HMM
2018/03/25 Python
PyQt5每天必学之工具提示功能
2018/04/19 Python
在Pycharm中将pyinstaller加入External Tools的方法
2019/01/16 Python
对Python强大的可变参数传递机制详解
2019/06/13 Python
python3 实现的对象与json相互转换操作示例
2019/08/17 Python
python--shutil移动文件到另一个路径的操作
2020/07/13 Python
css3 transform过渡抖动问题解决
2020/10/23 HTML / CSS
纽约通行卡:The New York Pass(免费游览纽约90多个景点)
2017/07/29 全球购物
印尼网上商店:Alfacart.com
2019/03/11 全球购物
Java servlet面试题
2012/03/04 面试题
建筑行业的大学生自我评价
2013/12/08 职场文书
党的群众路线教育实践活动心得体会(企业)
2014/11/03 职场文书
vue如何使用模拟的json数据查看效果
2022/03/31 Vue.js
Android Studio实现简易进制转换计算器
2022/05/20 Java/Android