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抓取Discuz!用户名脚本代码
Dec 30 Python
浅析Python中的join()方法的使用
May 19 Python
Python 2.x如何设置命令执行的超时时间实例
Oct 19 Python
Python字典数据对象拆分的简单实现方法
Dec 05 Python
Python 12306抢火车票脚本 Python京东抢手机脚本
Feb 06 Python
Python中的函数作用域
May 07 Python
Python基于生成器迭代实现的八皇后问题示例
May 23 Python
Python3.6基于正则实现的计算器示例【无优化简单注释版】
Jun 14 Python
python模拟实现分发扑克牌
Apr 22 Python
了解一下python内建模块collections
Sep 07 Python
Anaconda的安装与虚拟环境建立
Nov 18 Python
python快速安装OpenCV的步骤记录
Feb 22 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源代码
2006/10/09 PHP
VPS中使用LNMP安装WordPress教程
2014/12/28 PHP
关于PHP虚拟主机概念及如何选择稳定的PHP虚拟主机
2018/11/20 PHP
Laravel 微信小程序后端实现用户登录的示例代码
2019/11/26 PHP
Jquery遍历节点的方法小集
2014/01/22 Javascript
jquery 实现返回顶部功能
2014/11/17 Javascript
javascript和jquery实现设置和移除文本框默认值效果代码
2015/01/13 Javascript
基于JavaScript实现瀑布流效果(循环渐近)
2016/01/27 Javascript
浅谈React 属性和状态的一些总结
2016/11/21 Javascript
详解jQuery简单的表格应用
2016/12/16 Javascript
input type=file 选择图片并且实现预览效果的实例
2017/10/26 Javascript
node使用promise替代回调函数
2018/05/07 Javascript
JS中验证整数和小数的正则表达式
2018/10/08 Javascript
详解如何使用router-link对象方式传递参数?
2019/05/02 Javascript
react用Redux中央仓库实现一个todolist
2019/09/29 Javascript
Javascript ParentNode和ChildNode接口原理解析
2020/03/16 Javascript
[03:15]DOTA2-DPC中国联赛1月22日Recap集锦
2021/03/11 DOTA
python实现的重启关机程序实例
2014/08/21 Python
详解将Django部署到Centos7全攻略
2018/09/26 Python
python整小时 整天时间戳获取算法示例
2019/02/20 Python
Python实现的列表排序、反转操作示例
2019/03/13 Python
ubuntu 18.04搭建python环境(pycharm+anaconda)
2019/06/14 Python
python中对二维列表中一维列表的调用方法
2020/06/07 Python
提高python代码运行效率的一些建议
2020/09/29 Python
Spy++的使用方法及下载教程
2021/01/29 Python
分享CSS3中必须要知道的10个顶级命令
2012/04/26 HTML / CSS
时尚设计师手表:The Watch Cabin
2018/10/06 全球购物
Jones Bootmaker官网:优质靴子和鞋子在线
2020/11/30 全球购物
是否可以从一个static方法内部发出对非static方法的调用?
2014/08/18 面试题
领导证婚人证婚词
2014/01/13 职场文书
美发活动策划书
2014/01/14 职场文书
DIY手工制作经营店创业计划书
2014/02/01 职场文书
综治工作汇报材料
2014/10/27 职场文书
教师工作表现自我评价
2015/03/05 职场文书
旷工检讨书大全
2015/08/15 职场文书
关于CSS自定义属性与前端页面的主题切换问题
2022/03/21 HTML / CSS