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实现扫描指定目录下的子目录及文件的方法
Jul 16 Python
Python随机生成信用卡卡号的实现方法
May 14 Python
python遍历 truple list dictionary的几种方法总结
Sep 11 Python
python 中random模块的常用方法总结
Jul 08 Python
CentOS下使用yum安装python-pip失败的完美解决方法
Aug 16 Python
python利用OpenCV2实现人脸检测
Apr 16 Python
python使用pandas处理大数据节省内存技巧(推荐)
May 05 Python
python3的print()函数的用法图文讲解
Jul 16 Python
Python_查看sqlite3表结构,查询语句的示例代码
Jul 17 Python
Python run()函数和start()函数的比较和差别介绍
May 03 Python
python requests库的使用
Jan 06 Python
Pytorch模型迁移和迁移学习,导入部分模型参数的操作
Mar 03 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
基于HBase Thrift接口的一些使用问题及相关注意事项的详解
2013/06/03 PHP
php 去除html标记--strip_tags与htmlspecialchars的区别详解
2013/06/26 PHP
php使用ftp远程上传文件类(完美解决主从文件同步问题的方法)
2016/09/23 PHP
php数值转换时间及时间转换数值用法示例
2017/05/18 PHP
php实现支持中文的文件下载功能示例
2017/08/30 PHP
三个思路解决laravel上传文件报错:413 Request Entity Too Large问题
2017/11/13 PHP
PHP实现八皇后算法
2019/05/06 PHP
javascript 文档的编码问题解决
2009/03/01 Javascript
Document对象内容集合(比较全)
2010/09/06 Javascript
有关于JS构造函数的重载和工厂方法
2013/04/07 Javascript
jquery根据锚点offset值实现动画切换
2014/09/11 Javascript
JavaScript中的boolean布尔值使用学习及相关技巧讲解
2016/05/26 Javascript
js Canvas绘制圆形时钟教程
2017/02/06 Javascript
BootStrap表单控件之复选框checkbox和单选择按钮radio
2017/05/23 Javascript
详解AngularJS跨页面传值(ui-router)
2017/08/23 Javascript
JavaScript中的"=、==、==="区别讲解
2019/01/22 Javascript
解决Vue.js应用回退或刷新界面时提示用户保存修改问题
2019/11/24 Javascript
浅析Vue 中的 render 函数
2020/02/28 Javascript
vue实现图片上传到后台
2020/06/29 Javascript
Vue 构造选项 - 进阶使用说明
2020/08/14 Javascript
vue实现下拉菜单树
2020/10/22 Javascript
python中enumerate函数用法实例分析
2015/05/20 Python
详细解读Python中解析XML数据的方法
2015/10/15 Python
python实现redis三种cas事务操作
2017/12/19 Python
PyCharm 常用快捷键和设置方法
2017/12/20 Python
Python列表解析配合if else的方法
2018/06/23 Python
python 使用while写猜年龄小游戏过程解析
2019/10/07 Python
python操作链表的示例代码
2020/09/27 Python
幼儿园毕业教师感言
2014/02/21 职场文书
行政部工作岗位职责范本
2014/03/05 职场文书
机电一体化专业求职信
2014/07/22 职场文书
公司停电通知
2015/04/15 职场文书
西安事变观后感
2015/06/12 职场文书
家长会后的感想
2015/08/11 职场文书
基于Redis结合SpringBoot的秒杀案例详解
2021/10/05 Redis
《Estab Life》4月6日播出 正式PV、主视觉图公开
2022/03/20 日漫