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列表与元组详解实例
Nov 01 Python
Python 判断 有向图 是否有环的实例讲解
Feb 01 Python
PyQt5每天必学之像素图控件QPixmap
Apr 19 Python
python 获取sqlite3数据库的表名和表字段名的实例
Jul 17 Python
python网络编程 使用UDP、TCP协议收发信息详解
Aug 29 Python
Python程序暂停的正常处理方法
Nov 07 Python
Python实现线性判别分析(LDA)的MATLAB方式
Dec 09 Python
Windows下实现将Pascal VOC转化为TFRecords
Feb 17 Python
使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例
Mar 16 Python
Python 解析xml文件的示例
Sep 29 Python
Python用户自定义异常的实现
Dec 25 Python
python 如何获取页面所有a标签下href的值
May 06 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中extract()函数的定义和用法
2012/08/17 PHP
微信支付PHP SDK之微信公众号支付代码详解
2015/12/09 PHP
Yii2 assets清除缓存的方法
2016/05/16 PHP
PHP多进程之pcntl_fork的实例详解
2017/10/15 PHP
PHP中in_array的隐式转换的解决方法
2018/03/06 PHP
Jquery乱码的一次解决过程 图解教程
2010/02/20 Javascript
javascript for循环设法提高性能
2010/02/24 Javascript
javascript之典型高阶函数应用介绍二
2013/01/10 Javascript
js定时器的使用(实例讲解)
2014/01/06 Javascript
介绍一个简单的JavaScript类框架
2015/06/24 Javascript
第一次接触神奇的Bootstrap表单
2016/07/27 Javascript
js实现点击图片自动提交action的简单方法
2016/10/16 Javascript
详解AngularJs路由之Ui-router-resolve(预加载)
2017/06/13 Javascript
vue的mixins属性详解
2018/03/14 Javascript
Node.js中,在cmd界面,进入退出Node.js运行环境的方法
2018/05/12 Javascript
微信小程序保存多张图片的实现方法
2019/03/05 Javascript
基于Vue 实现一个中规中矩loading组件
2019/04/03 Javascript
微信小程序返回上一级页面的实现代码
2020/06/19 Javascript
打开电脑上的QQ的python代码
2013/02/10 Python
python正则表达式抓取成语网站
2013/11/20 Python
Python卸载模块的方法汇总
2016/06/07 Python
对Python进行数据分析_关于Package的安装问题
2017/05/22 Python
numpy.delete删除一列或多列的方法
2018/04/03 Python
Python数据结构之图的应用示例
2018/05/11 Python
pycharm执行python时,填写参数的方法
2018/10/29 Python
python实现网页自动签到功能
2019/01/21 Python
详解Python odoo中嵌入html简单的分页功能
2019/05/29 Python
python实现修改固定模式的字符串内容操作示例
2019/12/30 Python
Python 面向对象静态方法、类方法、属性方法知识点小结
2020/03/09 Python
python3.x中安装web.py步骤方法
2020/06/23 Python
HTML5实现动画效果的方式汇总
2016/02/29 HTML / CSS
欢迎领导标语
2014/06/27 职场文书
创业计划书之奶茶店开店方案范本!
2019/08/06 职场文书
Java中的继承、多态以及封装
2022/04/11 Java/Android
JS实现简单的九宫格抽奖
2022/06/28 Javascript
MySQL 原理与优化之原数据锁的应用
2022/08/14 MySQL