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制作GIF表情包生成工具

    基于Python制作GIF表情包生成工具

    在当前无表情包不会聊天的时代,怎么也不能输在表情包数量不足上啊,今天咱们就来基于Python制作一个 gif 生成工具,用来制作表情包也太好用啦
    2023-07-07
  • python 判断一组数据是否符合正态分布

    python 判断一组数据是否符合正态分布

    这篇文章主要介绍了python 如何判断一组数据是否符合正态分布,帮助大家更好的利用python分析数据,感兴趣的朋友可以了解下
    2020-09-09
  • Flask框架的学习指南之用户登录管理

    Flask框架的学习指南之用户登录管理

    本文是Flask框架的学习指南系列文章的第三篇,主要给大家讲述的是制作flask的登陆管理模块,有需要的小伙伴可以参考下
    2016-11-11
  • Python实现SICP赋值和局部状态

    Python实现SICP赋值和局部状态

    这篇文章主要介绍了Python实现SICP 赋值和局部状态的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • pyqt5 获取显示器的分辨率的方法

    pyqt5 获取显示器的分辨率的方法

    今天小编就为大家分享一篇pyqt5 获取显示器的分辨率的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • Python调用ollama本地大模型进行批量识别PDF

    Python调用ollama本地大模型进行批量识别PDF

    现在市场上有很多PDF文件的识别,然而随着AI的兴起,本地大模型的部署,这些成为一种很方便的方法,本文我们就来看看Python如何调用ollama本地大模型进行PDF相关操作吧
    2025-03-03
  • 详解如何使用Pandas删除DataFrame中的非数字类型数据

    详解如何使用Pandas删除DataFrame中的非数字类型数据

    在数据处理和分析过程中,经常会遇到需要清洗数据的情况,本文将详细介绍如何使用Pandas删除DataFrame中的非数字类型数据,感兴趣的小伙伴可以了解下
    2024-03-03
  • 基于Python和openCV实现图像的全景拼接详细步骤

    基于Python和openCV实现图像的全景拼接详细步骤

    这篇文章主要介绍了基于Python和openCV实现图像的全景拼接,本文分步骤通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2021-10-10
  • 对python的文件内注释 help注释方法

    对python的文件内注释 help注释方法

    今天小编就为大家分享一篇对python的文件内注释 help注释方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • python利用dir函数查看类中所有成员函数示例代码

    python利用dir函数查看类中所有成员函数示例代码

    这篇文章主要给大家介绍了关于python如何利用dir函数查看类中所有成员函数的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习下吧。
    2017-09-09

最新评论