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中的WSGI接口
May 11 Python
Python爬虫:通过关键字爬取百度图片
Feb 17 Python
在win10和linux上分别安装Python虚拟环境的方法步骤
May 09 Python
python中的colorlog库使用详解
Jul 05 Python
python字典的常用方法总结
Jul 31 Python
python实现知乎高颜值图片爬取
Aug 12 Python
从训练好的tensorflow模型中打印训练变量实例
Jan 20 Python
Tensorflow实现部分参数梯度更新操作
Jan 23 Python
TensorFlow 显存使用机制详解
Feb 03 Python
Python中pyecharts安装及安装失败的解决方法
Feb 18 Python
windows支持哪个版本的python
Jul 03 Python
Python tkinter界面实现历史天气查询的示例代码
Aug 23 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
自动跳转中英文页面
2006/10/09 PHP
很好用的PHP数据库类
2009/05/27 PHP
thinkPHP实现表单自动验证
2014/12/24 PHP
在Win2003(64位)中配置IIS6+PHP5.2.17+MySQL5.5的运行环境
2016/04/04 PHP
微信公众号实现会员卡领取功能
2017/06/08 PHP
PHP的curl函数的用法总结
2019/02/14 PHP
Laravel Validator 实现两个或多个字段联合索引唯一
2019/05/08 PHP
javascript静态的url如何传递
2007/05/03 Javascript
JavaScript 异步调用框架 (Part 5 - 链式实现)
2009/08/04 Javascript
基于jquery的页面划词搜索JS
2010/09/14 Javascript
再说AutoComplete自动补全之实现原理
2011/11/05 Javascript
JS.elementGetStyle(element, style)应用示例
2013/09/24 Javascript
javascript如何创建表格(javascript绘制表格的二种方法)
2013/12/10 Javascript
调试代码导致IE出错的避免方法
2014/04/04 Javascript
JS判断客户端是手机还是PC的2个代码
2014/04/12 Javascript
谈谈JavaScript异步函数发展历程
2015/09/29 Javascript
Easyui的组合框的取值与赋值
2016/10/28 Javascript
angularjs http与后台交互的实现示例
2018/12/21 Javascript
vue element-ui table组件动态生成表头和数据并修改单元格格式 父子组件通信
2019/08/15 Javascript
[50:12]EG vs Fnatic 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
如何爬取通过ajax加载数据的网站
2019/08/15 Python
Python 列表中的修改、添加和删除元素的实现
2020/06/11 Python
最新pycharm安装教程
2020/11/18 Python
如何用PyPy让你的Python代码运行得更快
2020/12/02 Python
水上运动奥特莱斯:Wasterports Outlet
2018/08/08 全球购物
工程预算与管理应届生求职信
2013/10/06 职场文书
大学生物业管理求职信
2013/10/24 职场文书
医学类导师推荐信范文
2013/11/19 职场文书
生物技术专业毕业生求职信范文
2013/12/14 职场文书
韩国商务邀请函
2014/01/14 职场文书
食品安全标语
2014/06/07 职场文书
会计学专业求职信
2014/07/17 职场文书
房屋产权共有协议书范本
2014/11/03 职场文书
结婚喜宴祝酒词
2015/08/10 职场文书
预备党员表决心的话
2015/09/22 职场文书
创业计划书之网络外卖
2019/10/31 职场文书