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实现简单ftp客户端的方法
Jun 28 Python
深入理解Django自定义信号(signals)
Oct 15 Python
Python模拟浏览器上传文件脚本的方法(Multipart/form-data格式)
Oct 22 Python
CentOS6.9 Python环境配置(python2.7、pip、virtualenv)
May 06 Python
python 判断三个数字中的最大值实例代码
Jul 24 Python
Django Rest framework权限的详细用法
Jul 25 Python
python 3.74 运行import numpy as np 报错lib\site-packages\numpy\__init__.py
Oct 06 Python
Python使用Turtle库绘制一棵西兰花
Nov 23 Python
python创建n行m列数组示例
Dec 02 Python
python生成大写32位uuid代码
Mar 03 Python
PyQt5实现简单的计算器
May 30 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截取后台登陆密码的代码
2012/05/05 PHP
php 利用socket发送HTTP请求(GET,POST)
2015/08/24 PHP
thinkphp实现163、QQ邮箱收发邮件的方法
2015/12/18 PHP
jquery getScript动态加载JS方法改进详解
2012/11/15 Javascript
网页中返回顶部代码(多种方法)另附注释说明
2013/04/24 Javascript
JavaScript中创建类/对象的几种方法总结
2013/11/29 Javascript
JS方法调用括号的问题探讨
2014/01/24 Javascript
gridview生成时如何去掉style属性中的border-collapse
2014/09/30 Javascript
jquery制作LED 时钟特效
2015/02/01 Javascript
javascript实现页面刷新时自动清空表单并选中的方法
2015/07/18 Javascript
JavaScript实现删除,移动和复制文件的方法
2015/08/05 Javascript
JS操作时间 - UNIX时间戳的简单介绍(必看篇)
2017/08/16 Javascript
浅谈js中的this问题
2017/08/31 Javascript
基于vue-element组件实现音乐播放器功能
2018/05/06 Javascript
基于jQuery实现无缝轮播与左右点击效果
2018/05/13 jQuery
微信二次分享报错invalid signature问题及解决方法
2019/04/01 Javascript
js实现淘宝首页的banner栏效果
2019/11/26 Javascript
[00:42]《辉夜杯》—职业组预选赛12月3日15点 正式打响
2015/12/03 DOTA
django rest framework之请求与响应(详解)
2017/11/06 Python
Python基于生成器迭代实现的八皇后问题示例
2018/05/23 Python
Python3安装Pillow与PIL的方法
2019/04/03 Python
python实现接口并发测试脚本
2019/06/25 Python
python实现日志按天分割
2019/07/22 Python
python批量将excel内容进行翻译写入功能
2019/10/10 Python
python爬取本站电子书信息并入库的实现代码
2020/01/20 Python
意大利在线药房:shop-farmacia.it
2019/03/12 全球购物
澳大利亚在线高跟鞋商店:Shoe Me
2019/11/19 全球购物
古驰英国官网:GUCCI英国
2020/03/07 全球购物
广告学专业自荐信范文
2014/02/24 职场文书
孝女彩金观后感
2015/06/10 职场文书
2016秋季小学开学寄语
2015/12/03 职场文书
《小小的船》教学反思
2016/02/18 职场文书
会计工作自我鉴定范文
2019/06/21 职场文书
浅谈Laravel中使用Slack进行异常通知
2021/05/29 PHP
如何使用 resize 实现图片切换预览功能
2021/08/23 HTML / CSS
uniapp引入支付宝原生扫码插件步骤详解
2022/07/23 Javascript