python实现备份目录的方法
作者:不是JS 发布时间:2022-10-30 02:54:04
标签: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程序设计有所帮助。


猜你喜欢
- LyScript 可实现自定义汇编指令的替换功能,用户可以自行编写一段汇编指令,将程序 * 定的通用函数进行功能改写与转向操作,此功能原理是简
- 第一步:下载mysql驱动cmd进入创建好的django项目目录:使用命令pip install mysqlclient等待安装
- 支持Python的IDE有IPython、Aptana Studio(在Eclipse的基础上加插件集改的)、PyCharm(由 JetBr
- 本文实例讲述了PHP实现二维数组中的查找算法。分享给大家供大家参考,具体如下:方法1:silu从左下角最后一行的第一个元素开始,遍历。如果小
- Memoization 是一种将函数返回值缓存起来的方法,在 Lisp, Ruby, Perl, Python 等语言中使用非常广泛。随着
- 操作步骤1.下载BeautifulReport文件,本例文件下载地址 最新文件下载地址2.复制文件BeautifulReport,至pyth
- 今天给大家介绍一个可以获取当前系统信息的库——psutil利用psutil库可以获取系统的一些信息,如cpu,内存等使用率,从而可以查看当前
- hints是oracle提供的一种机制,用来告诉优化器按照我们的告诉它的方式生成执行计划。我们可以用hints来实现:  
- 1.用一个栈【python中可以用List】就可以解决,时间和空间复杂度都是O(n)# -*- coding: utf8 -*-# 符号表S
- QCheckBox 是具有两种状态的控件:开和关。它是一个带有标签的复选框。复选框通常用于表示应用程序可以启用或禁用的功能。#!/usr/b
- MySQL的异常处理分析如下:标准格式DECLARE handler_type HANDLER FOR condition_value[,.
- 话不多说,请看代码------------------------------------------作者:张欣宇-----时间:2013-
- 三角函数如果我们以OP作为圆的半径r,以o点作为圆的圆心,圆上的点的x坐标就是r * cos a ,y坐标就是 r * sin a。pyth
- 虽然每个图像具有多个通道和多层卷积层。例如彩色图像具有标准的RGB通道来指示红、绿和蓝。但是到目前为止,我们仅展示了单个输入和单个输出通道的
- 一、前言 前面我们学习了if分支判断和for循环语句,在这次推送中我们将继续了解循环大家庭的成员们。大家可以通过上面的流程图来了解
- 如何在pytorch中指定CPU和GPU进行训练,以及cpu和gpu之间切换由CPU切换到GPU,要修改的几个地方:网络模型、损失函数、数据
- 方法一:onPullDownRefresh和onReachBottom方法实现小程序下拉加载和上拉刷新首先要在json文件里设置window
- github demo: github地址闲聊背景本文主要以 vue-cli3 搭建的项目为例,来聊一下如何在项目中更优雅的使用 svg 。
- 一、触发器的介绍触发器是与表有关的数据库对象,指在insert/update/delete 之前或之后,触发并执行触发器中定义的SQL语句集
- 从这一章开始进入正式的算法学习。首先我们学习经典而有效的分类算法:决策树分类算法。1、决策树算法决策树用树形结构对样本的属性进行分类,是最直