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程序设计有所帮助。

相关文章

  • 一文带你深入理解Python的`functools.lru_cache`装饰器

    一文带你深入理解Python的`functools.lru_cache`装饰器

    Python中的functools.lru_cache装饰器是一个非常有用的装饰器,它可以帮助我们优化递归函数,避免重复计算已经计算过的值,在这篇文章中,我们将探讨 functools.lru_cache 的工作原理以及如何使用它,感兴趣的朋友跟着小编一起来学习吧
    2023-07-07
  • TensorFlow实现卷积神经网络

    TensorFlow实现卷积神经网络

    这篇文章主要为大家详细介绍了TensorFlow实现卷积神经网络,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Python实现极限车神游戏的示例代码

    Python实现极限车神游戏的示例代码

    今天小编要为大家介绍一款小编自己用Python代码码出来的赛车风格的打字小游戏,不仅能游戏还能学到很多不同类型的编程代码关键字的语言,需要的可以参考一下
    2023-02-02
  • python_tkinter弹出对话框创建

    python_tkinter弹出对话框创建

    这篇文章主要介绍了python_tkinter弹出对话框实现,tkinter提供了三个模块,可以创建弹出对话窗口,下面详细介绍,需要的小伙伴可以参考一下,希望对你的学习有所帮助
    2022-03-03
  • 如何使用 Python 读取文件和照片的创建日期

    如何使用 Python 读取文件和照片的创建日期

    这篇文章主要介绍了如何使用 Python 读取文件和照片的创建日期,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • 详解Python使用Plotly绘图工具,绘制甘特图

    详解Python使用Plotly绘图工具,绘制甘特图

    这篇文章主要介绍了Python使用Plotly绘图工具,绘制甘特图,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • 利用LyScript实现应用层钩子扫描器

    利用LyScript实现应用层钩子扫描器

    Capstone 是一个轻量级的多平台、多架构的反汇编框架。本篇文章将运用LyScript插件结合Capstone反汇编引擎实现一个钩子扫描器,感兴趣的可以了解一下
    2022-08-08
  • 5行Python代码实现一键批量扣图

    5行Python代码实现一键批量扣图

    在日常生活或者工作中,经常会遇到想将某张照片中的人物抠出来,本文就介绍了Python代码实现一键批量扣图,感兴趣的可以了解一下
    2021-06-06
  • Python识别html主要文本框过程解析

    Python识别html主要文本框过程解析

    这篇文章主要介绍了python识别html主要文本框过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • python安装及变量名介绍详解

    python安装及变量名介绍详解

    这篇文章主要介绍了python安装及变量名介绍详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12

最新评论