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的Django框架中的通用视图
May 04 Python
python读取TXT到数组及列表去重后按原来顺序排序的方法
Jun 26 Python
Python定时器实例代码
Nov 01 Python
python 删除字符串中连续多个空格并保留一个的方法
Dec 22 Python
对python中Json与object转化的方法详解
Dec 31 Python
几行Python代码爬取3000+上市公司的信息
Jan 24 Python
python 回溯法模板详解
Feb 26 Python
Python3 io文本及原始流I/O工具用法详解
Mar 23 Python
Python实现加密接口测试方法步骤详解
Jun 05 Python
python如何保存文本文件
Jun 07 Python
一文详述 Python 中的 property 语法
Sep 01 Python
python如何实时获取tcpdump输出
Sep 16 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版
2012/04/20 PHP
PHP实现的ID混淆算法类与用法示例
2018/08/10 PHP
php面向对象程序设计中self与static的区别分析
2019/05/21 PHP
[HTML/CSS/Javascript]WWTJS
2007/09/25 Javascript
jQuery效果 slideToggle() 方法(在隐藏和显示之间切换)
2011/06/28 Javascript
FusionCharts图表显示双Y轴双(多)曲线
2012/11/22 Javascript
原生js实现跨浏览器获取鼠标按键的值
2013/04/08 Javascript
javascript图片相似度算法实现 js实现直方图和向量算法
2014/01/14 Javascript
ext combobox动态加载数据库数据(附前后台)
2014/06/17 Javascript
使用jquery实现放大镜效果
2014/09/02 Javascript
js控制台输出的方法(详解)
2016/11/26 Javascript
详解javascript中对数据格式化的思考
2017/01/23 Javascript
jquery easyui DataGrid简单示例
2017/01/23 Javascript
js获取浏览器和屏幕的各种宽度高度
2017/02/22 Javascript
jQuery使用正则验证15/18身份证的方法示例
2017/04/27 jQuery
Vue.js中关于侦听器(watch)的高级用法示例
2018/05/02 Javascript
在vue 中使用 less的教程详解
2018/09/26 Javascript
基于vue2.0的活动倒计时组件countdown(附源码下载)
2018/10/09 Javascript
Python中的random()方法的使用介绍
2015/05/15 Python
python动态网页批量爬取
2016/02/14 Python
如何在Python函数执行前后增加额外的行为
2016/10/20 Python
python3实现UDP协议的服务器和客户端
2017/06/14 Python
Python编程求解二叉树中和为某一值的路径代码示例
2018/01/04 Python
Python列表解析操作实例总结
2020/02/26 Python
Python中的整除和取模实例
2020/06/03 Python
解决pytorch下出现multi-target not supported at的一种可能原因
2021/02/06 Python
CSS3感应鼠标的背景闪烁和图片缩放动画效果
2014/05/14 HTML / CSS
2014客服代表实习自我鉴定
2014/09/18 职场文书
2014年业务工作总结
2014/11/17 职场文书
2014年督导工作总结
2014/11/19 职场文书
优秀班组申报材料
2014/12/25 职场文书
化验室岗位职责
2015/02/14 职场文书
爱鸟护鸟的宣传语
2015/07/13 职场文书
详解Node.js如何处理ES6模块
2021/05/15 Javascript
《勇者辞职不干了》ED主题曲无字幕动画MV公开
2022/04/13 日漫
Qt数据库应用之实现图片转pdf
2022/06/01 Java/Android