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框架下管理站点的基本方法
Jul 17 Python
Python正则表达式如何进行字符串替换实例
Dec 28 Python
python3爬虫之设计签名小程序
Jun 19 Python
APIStar:一个专为Python3设计的API框架
Sep 26 Python
使用Python进行目录的对比方法
Nov 01 Python
python实现控制电脑鼠标和键盘,登录QQ的方法示例
Jul 06 Python
python实现输入的数据在地图上生成热力图效果
Dec 06 Python
基于Tensorflow高阶读写教程
Feb 10 Python
keras打印loss对权重的导数方式
Jun 10 Python
Pycharm的Available Packages为空的解决方法
Sep 18 Python
Python中random模块常用方法的使用教程
Oct 04 Python
详解python爬取弹幕与数据分析
Nov 14 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 采集书并合成txt格式的实现代码
2009/03/01 PHP
Windows和Linux中php代码调试工具Xdebug的安装与配置详解
2014/05/08 PHP
php中如何执行linux命令详解
2018/11/06 PHP
PHP实现的抓取小说网站内容功能示例
2019/06/27 PHP
PHP设计模式之 策略模式Strategy详解【对象行为型】
2020/05/01 PHP
jquery.tmpl JQuery模板插件
2011/10/10 Javascript
jquery实现图片等比例缩放以及max-width在ie中不兼容解决
2013/03/21 Javascript
js控制TR的显示隐藏
2016/03/04 Javascript
利用纯Vue.js构建Bootstrap组件
2016/11/03 Javascript
微信开发 JS-SDK 6.0.2 经常遇到问题总结
2016/12/08 Javascript
js仿网易表单及时验证功能
2017/03/07 Javascript
jQuery实现radio第一次点击选中第二次点击取消功能
2017/05/15 jQuery
Angular 2父子组件数据传递之@Input和@Output详解(下)
2017/07/05 Javascript
基于jstree使用AJAX请求获取数据形成树
2017/08/29 Javascript
bootstrap select下拉搜索插件使用方法详解
2017/11/23 Javascript
jQuery实现的下雪动画效果示例【附源码下载】
2018/02/02 jQuery
JavaScript链式调用实例浅析
2018/12/19 Javascript
uni-app之APP和小程序微信授权方法
2019/05/09 Javascript
使用webpack搭建pixi.js开发环境
2020/02/12 Javascript
[58:57]2018DOTA2亚洲邀请赛3月29日小组赛B组 Effect VS VGJ.T
2018/03/30 DOTA
用Python写一个无界面的2048小游戏
2016/05/24 Python
教大家玩转Python字符串处理的七种技巧
2017/03/31 Python
关于pip的安装,更新,卸载模块以及使用方法(详解)
2017/05/19 Python
python 信息同时输出到控制台与文件的实例讲解
2018/05/11 Python
对python 操作solr索引数据的实例详解
2018/12/07 Python
python如何获取当前文件夹下所有文件名详解
2019/01/25 Python
Django模板Templates使用方法详解
2019/07/19 Python
Python使用matplotlib 模块scatter方法画散点图示例
2019/09/27 Python
Python3 集合set入门基础
2020/02/10 Python
python闭包、深浅拷贝、垃圾回收、with语句知识点汇总
2020/03/11 Python
Mamaearth官方网站:印度母婴护理产品公司
2019/10/06 全球购物
毕业生个人投资创业计划书
2014/01/04 职场文书
个人自我剖析材料
2014/09/30 职场文书
卖房协议书样本
2014/10/30 职场文书
2015年高中生国庆节演讲稿
2015/07/30 职场文书
Python OpenCV快速入门教程
2021/04/17 Python