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实现socket客户端和服务端简单示例
Feb 24 Python
python装饰器与递归算法详解
Feb 18 Python
详解Python中的文件操作
Aug 28 Python
Python使用asyncio包处理并发详解
Sep 09 Python
python爬取亚马逊书籍信息代码分享
Dec 09 Python
Python 记录日志的灵活性和可配置性介绍
Feb 27 Python
如何安装并使用conda指令管理python环境
Jul 10 Python
使用Python来做一个屏幕录制工具的操作代码
Jan 18 Python
python GUI库图形界面开发之PyQt5信号与槽事件处理机制详细介绍与实例解析
Mar 08 Python
Python学习之os模块及用法
Jun 03 Python
Python3 用matplotlib绘制sigmoid函数的案例
Dec 11 Python
基于Python 函数和方法的区别说明
Mar 24 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 实用代码收集
2010/01/22 PHP
thinkPHP框架实现图像裁剪、缩放、加水印的方法
2017/03/14 PHP
源码分析 Laravel 重复执行同一个队列任务的原因
2017/12/25 PHP
javascript英文日期(有时间)选择器
2007/05/02 Javascript
一个简单的jQuery插件制作 学习过程及实例
2010/04/25 Javascript
优化Jquery,提升网页加载速度
2013/11/14 Javascript
浅析jquery某一元素重复绑定的问题
2014/01/03 Javascript
jquery实现页面图片等比例放大缩小功能
2014/02/12 Javascript
jquery实现倒计时效果
2015/12/14 Javascript
有关文件上传 非ajax提交 得到后台数据问题
2016/10/12 Javascript
Node.js服务器开启Gzip压缩教程
2017/08/11 Javascript
vue.js编译时给生成的文件增加版本号
2018/09/17 Javascript
从零开始封装自己的自定义Vue组件
2018/10/09 Javascript
详解如何使用微信小程序云函数发送短信验证码
2019/03/13 Javascript
vue分页插件的使用方法
2019/12/25 Javascript
wxPython框架类和面板类的使用实例
2014/09/28 Python
Python实现登录人人网并抓取新鲜事的方法
2015/05/11 Python
Python中DJANGO简单测试实例
2015/05/11 Python
Python中datetime模块参考手册
2017/01/13 Python
Python字符串逆序输出的实例讲解
2019/02/16 Python
python导入坐标点的具体操作
2019/05/10 Python
Python中的pathlib.Path为什么不继承str详解
2019/06/23 Python
django实现将后台model对象转换成json对象并传递给前端jquery
2020/03/16 Python
python实现猜数游戏
2020/03/27 Python
windows10 pycharm下安装pyltp库和加载模型实现语义角色标注的示例代码
2020/05/07 Python
html5 利用canvas实现超级玛丽简单动画
2013/09/06 HTML / CSS
吃透移动端 Html5 响应式布局
2019/12/16 HTML / CSS
解决方案设计综合面试题
2015/08/31 面试题
广告创意求职信
2014/03/17 职场文书
员工入职担保书范文
2014/04/01 职场文书
护士节演讲稿开场白
2014/08/25 职场文书
幼儿园端午节活动方案
2014/08/25 职场文书
全国优秀教师事迹材料
2014/08/26 职场文书
缓刑人员思想汇报500字
2014/09/12 职场文书
手机被没收的检讨书
2014/10/04 职场文书
MySQL中rank() over、dense_rank() over、row_number() over用法介绍
2022/03/23 MySQL