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实现
Jan 28 Python
python读写json文件的简单实现
Apr 11 Python
Python Cookie 读取和保存方法
Dec 28 Python
python读取几个G的csv文件方法
Jan 07 Python
Python3 sys.argv[ ]用法详解
Oct 24 Python
Python计算不规则图形面积算法实现解析
Nov 22 Python
Python 获取numpy.array索引值的实例
Dec 06 Python
Python 简单计算要求形状面积的实例
Jan 18 Python
执行Python程序时模块报错问题
Mar 26 Python
python下对hsv颜色空间进行量化操作
Jun 04 Python
完美解决keras保存好的model不能成功加载问题
Jun 11 Python
Python列表推导式实现代码实例
Sep 09 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
怎么使 Mysql 数据同步
2006/10/09 PHP
php MySQL与分页效率
2008/06/04 PHP
php _autoload自动加载类与机制分析
2012/02/10 PHP
php上传图片存入数据库示例分享
2014/03/11 PHP
laravel框架学习笔记之组件化开发实现方法
2020/02/01 PHP
Javascript实现的类似Google的Div拖动效果代码
2011/08/09 Javascript
jQuery控制文本框只能输入数字和字母及使用方法
2016/05/26 Javascript
微信小程序 页面传参实例详解
2016/11/16 Javascript
JS仿淘宝搜索框用户输入事件的实现
2017/06/19 Javascript
详解React中的组件通信问题
2017/07/31 Javascript
基于Vue.js 2.0实现百度搜索框效果
2020/12/28 Javascript
详解RequireJs官方使用教程
2017/10/31 Javascript
vue组件中使用iframe元素的示例代码
2017/12/13 Javascript
vue-cli脚手架引入图片的几种方法总结
2018/03/13 Javascript
Vue.js 事件修饰符的使用教程
2018/11/01 Javascript
微信小程序云开发使用方法新手初体验
2019/05/16 Javascript
详解vue高级特性
2020/06/09 Javascript
详解如何在Javascript中使用Object.freeze()
2020/10/18 Javascript
react ant Design手动设置表单的值操作
2020/10/31 Javascript
Python实现简单多线程任务队列
2016/02/27 Python
python常用函数详解
2016/09/13 Python
PHP统计代码行数的小代码
2019/09/19 Python
Python Socketserver实现FTP文件上传下载代码实例
2020/03/27 Python
python用TensorFlow做图像识别的实现
2020/04/21 Python
python如何实时获取tcpdump输出
2020/09/16 Python
Python爬虫之Selenium实现关闭浏览器
2020/12/04 Python
总经理办公室主任岗位职责
2013/11/12 职场文书
普通简短的个人自我评价
2014/02/15 职场文书
2014社区三八妇女节活动总结
2014/03/01 职场文书
铁路安全反思材料
2014/12/24 职场文书
职工年度考核评语
2014/12/31 职场文书
2015年营销工作总结范文
2015/04/23 职场文书
2019教师的学习计划
2019/06/25 职场文书
Mysql官方性能测试工具mysqlslap的使用简介
2021/05/21 MySQL
pytorch 使用半精度模型部署的操作
2021/05/24 Python
十大最强飞行系宝可梦,BUG燕上榜,第二是飞行系王者
2022/03/18 日漫