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常用模块用法分析
Sep 08 Python
Python写的英文字符大小写转换代码示例
Mar 06 Python
python通过函数属性实现全局变量的方法
May 16 Python
Python使用cx_Freeze库生成msi格式安装文件的方法
Jul 10 Python
Python WEB应用部署的实现方法
Jan 02 Python
Python 3.3实现计算两个日期间隔秒数/天数的方法示例
Jan 07 Python
python在新的图片窗口显示图片(图像)的方法
Jul 11 Python
Python3 Click模块的使用方法详解
Feb 12 Python
使用Python第三方库pygame写个贪吃蛇小游戏
Mar 06 Python
Python的in,is和id函数代码实例
Apr 18 Python
Python3.7在anaconda里面使用IDLE编译器的步骤详解
Apr 29 Python
Python StringIO及BytesIO包使用方法解析
Jun 15 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处理restful请求的路由类分享
2014/02/27 PHP
ThinkPHP模板中判断volist循环的最后一条记录的验证方法
2014/07/01 PHP
php基于base64解码图片与加密图片还原实例
2014/11/03 PHP
Symfony页面的基本创建实例详解
2015/01/26 PHP
WordPress中自定义后台管理界面配色方案的小技巧
2015/12/29 PHP
PHP中的use关键字及文件的加载详解
2016/11/28 PHP
JQuery之拖拽插件实现代码
2011/04/14 Javascript
用Javascript评估用户输入密码的强度(Knockout版)
2011/11/30 Javascript
jquery解析XML字符串和XML文件的方法说明
2014/02/21 Javascript
jquery的ajax异步请求接收返回json数据实例
2014/06/16 Javascript
Nodejs sublime text 3安装与配置
2014/06/19 NodeJs
详解JavaScript的Polymer框架中的通知交互
2015/07/29 Javascript
跟我学习javascript的prototype,getPrototypeOf和__proto__
2015/11/17 Javascript
JS获取CSS样式(style/getComputedStyle/currentStyle)
2016/01/19 Javascript
jquery插件ContextMenu设置右键菜单
2017/03/13 Javascript
nodejs动态创建二维码的方法
2017/08/12 NodeJs
微信小程序实现循环动画效果
2018/07/16 Javascript
koa-router源码学习小结
2018/09/07 Javascript
vue移动端模态框(可传参)的实现
2019/11/20 Javascript
js 计算月/周的第一天和最后一天代码
2020/02/01 Javascript
[00:32]DOTA2上海特级锦标赛 Ehome战队宣传片
2016/03/03 DOTA
[58:37]Serenity vs Fnatic 2018国际邀请赛淘汰赛BO1 8.21
2018/08/22 DOTA
Python2.7基于淘宝接口获取IP地址所在地理位置的方法【测试可用】
2017/06/07 Python
Python中顺序表的实现简单代码分享
2018/01/09 Python
梅尔频率倒谱系数(mfcc)及Python实现
2019/06/18 Python
python+Django实现防止SQL注入的办法
2019/10/31 Python
Python3.7 读取 mp3 音频文件生成波形图效果
2019/11/05 Python
在tensorflow中实现去除不足一个batch的数据
2020/01/20 Python
浅谈Pycharm的项目文件名是红色的原因及解决方式
2020/06/01 Python
Python字典取键、值对的方法步骤
2020/09/30 Python
HTML5 File API改善网页上传功能
2009/08/19 HTML / CSS
Ted Baker英国官网:男士和女士服装及配件
2017/03/13 全球购物
名词解释型面试题(主要是网络)
2013/12/27 面试题
秘书岗位职责
2013/11/18 职场文书
2014年企业党支部工作总结
2014/12/04 职场文书
redis实现的四种常见限流策略
2021/06/18 Redis