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实现代码行数统计示例分享
Feb 10 Python
一个计算身份证号码校验位的Python小程序
Aug 15 Python
在Python中使用M2Crypto模块实现AES加密的教程
Apr 08 Python
Python获取央视节目单的实现代码
Jul 25 Python
Python中模块string.py详解
Mar 12 Python
windows下Virtualenvwrapper安装教程
Dec 13 Python
python如何制作英文字典
Jun 25 Python
Python socket模块方法实现详解
Nov 05 Python
python多项式拟合之np.polyfit 和 np.polyld详解
Feb 18 Python
Python Websocket服务端通信的使用示例
Feb 25 Python
linux 下selenium chrome使用详解
Apr 02 Python
pytorch实现ResNet结构的实例代码
May 17 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魔术方法以及关于独立实例与相连实例的全面讲解
2016/10/18 PHP
Laravel 不同生产环境服务器的判断实践
2019/10/15 PHP
jQuery入门问答 整理的几个常见的初学者问题
2010/02/22 Javascript
PHP 与 js的通信(via ajax,json)
2010/11/16 Javascript
from表单多个按钮提交用onclick跳转不同action
2014/04/24 Javascript
canvas时钟效果
2017/02/16 Javascript
用node-webkit把web应用打包成桌面应用(windows环境)
2018/02/01 Javascript
微信小程序实现单选功能
2018/10/30 Javascript
jQuery实现的记住帐号密码功能完整示例
2019/08/03 jQuery
webpack 如何解析代码模块路径的实现
2019/09/04 Javascript
Javascript call及apply应用场景及实例
2020/08/26 Javascript
JavaScript实现手风琴效果
2021/02/18 Javascript
[02:14]DOTA2英雄基础教程 修补匠
2013/12/23 DOTA
简明 Python 基础学习教程
2007/02/08 Python
用python + hadoop streaming 分布式编程(一) -- 原理介绍,样例程序与本地调试
2014/07/14 Python
Python中的hypot()方法使用简介
2015/05/18 Python
python开发之函数定义实例分析
2015/11/12 Python
Flask数据库迁移简单介绍
2017/10/24 Python
Python3实现腾讯云OCR识别
2018/11/27 Python
Python代码实现删除一个list里面重复元素的方法
2019/04/02 Python
Django项目中添加ldap登陆认证功能的实现
2019/04/04 Python
Python文件路径名的操作方法
2019/10/30 Python
Python vtk读取并显示dicom文件示例
2020/01/13 Python
python基于win32api实现键盘输入
2020/12/09 Python
HTML5 canvas实现移动端上传头像拖拽裁剪效果
2016/03/14 HTML / CSS
ProBikeKit美国官网:自行车套件,跑步和铁人三项套件
2016/10/13 全球购物
玩具反斗城葡萄牙官方商城:Toys"R"Us葡萄牙
2016/10/21 全球购物
护理专科毕业推荐信
2013/11/10 职场文书
资料员的岗位职责
2013/11/20 职场文书
本科生的职业生涯规划范文
2014/01/09 职场文书
迟到检讨书400字
2014/01/13 职场文书
会务接待方案
2014/02/27 职场文书
挂牌仪式主持词
2014/03/20 职场文书
离婚协议书范本2014
2014/10/27 职场文书
Vue.js 带下拉选项的输入框(Textbox with Dropdown)组件
2021/04/17 Vue.js
Flutter集成高德地图并添加自定义Maker的实践
2022/04/07 Java/Android