python实现备份目录的方法

 更新时间:2015年08月03日 12:53:28   作者:不是JS  
这篇文章主要介绍了python实现备份目录的方法,实例总结了Python实现备份目录的三种常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • Django集成Celery实现高效的异步任务处理的全过程

    Django集成Celery实现高效的异步任务处理的全过程

    Django 作为一个强大的 Python Web 框架,可以通过集成 Celery 这一异步任务队列来优化这些任务的处理,本文将深入探讨如何在 Django 项目中集成 Celery,包括 Celery 的基本配置、定义任务、以及监控任务执行,需要的朋友可以参考下
    2023-11-11
  • Python 文件操作的详解及实例

    Python 文件操作的详解及实例

    这篇文章主要介绍了Python 文件操作的详解及实例的相关资料,希望通过本文大家能够理解掌握Python 文件操作的知识,需要的朋友可以参考下
    2017-09-09
  • Python编程中的反模式实例分析

    Python编程中的反模式实例分析

    这篇文章主要介绍了Python编程中的反模式,详细讲述了反模式的害处并以实例形式具体分析了容易造成的易错点,对于Python学习来说具有一定的参考借鉴价值,需要的朋友可以参考下
    2014-12-12
  • 宝塔面板成功部署Django项目流程(图文)

    宝塔面板成功部署Django项目流程(图文)

    这篇文章主要介绍了宝塔面板成功部署Django项目流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • 一些Python中的二维数组的操作方法

    一些Python中的二维数组的操作方法

    这篇文章主要介绍了一些Python中的二维数组的操作方法,是Python学习当中的基础知识,需要的朋友可以参考下
    2015-05-05
  • Python与Redis的连接教程

    Python与Redis的连接教程

    这篇文章主要介绍了Python与Redis的连接教程,Redis是一个高性能的基于内存的数据库,需要的朋友可以参考下
    2015-04-04
  • Python heapq使用详解及实例代码

    Python heapq使用详解及实例代码

    这篇文章主要介绍了Python heapq使用详解及实例代码的相关资料,需要的朋友可以参考下
    2017-01-01
  • Python实现基本数据结构中队列的操作方法示例

    Python实现基本数据结构中队列的操作方法示例

    这篇文章主要介绍了Python实现基本数据结构中队列的操作方法,结合实例形式演示了Python针对数据结构中队列的初始化、插入、删除、判断队列满及队列空等相关操作技巧,需要的朋友可以参考下
    2017-12-12
  • python操作mysql数据库

    python操作mysql数据库

    本篇文章主要介绍了python操作mysql数据库的相关知识,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • python GUI库图形界面开发之PyQt5布局控件QGridLayout详细使用方法与实例

    python GUI库图形界面开发之PyQt5布局控件QGridLayout详细使用方法与实例

    这篇文章主要介绍了python GUI库图形界面开发之PyQt5布局控件QGridLayout详细使用方法与实例,需要的朋友可以参考下
    2020-03-03

最新评论