#-*-coding:utf-8-*-
import os
import time
#备份文件
# 1. The files and directories to be backed up are specified in a list.
source = ['/usr/local/apache2', '/usr/local/backup']
# 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 = '/usr/local/qs/' # 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备份文件的方法有很多种,和大家分享的这种方法是针对特定的文件类型来用python方法完成备份。简单的来理解就是:我们只对某个特定后缀名的文件进行检测及备份;在for file in files 循环的内部加一个适当的...
python定时压缩备份文件 定时器+os.system方法调用zip命令
该脚本是用python编写,用于对每日文件进行压缩备份存放,适用于初学者,望多指教~
python实现输入要备份的文件名称:test.txt 12行代码实现文件备份功能 第一步:打开我们的pycharm软件,然后新建一个Python文件 第二步:新建好我们的Python文件后,我们在编辑界面输入以下代码: oldfilename=...
本文实例讲述了Python实现备份文件的方法,是一个非常实用的技巧。分享给大家供大家参考。具体方法如下: 该实例主要实现读取一个任务文件, 根据指定的任务参数自动备份. 任务文件的格式: (注意,分号后面注释是不...
python文件备份源码.rar
实际效果:假设给定目录”/media/data/programmer/project/python” ,备份路径”/home/diegoyun/backup/“ , 则会将python目录下的文件按照全路经备份到备份路径下,形如: /home/diegoyun/backup/yyyymmddHHMMSS/...
Python3写的脚本,windows上应用的,实现服务器上的数据库通过磁盘映射备份到本地其他机器上的共享文件夹中,服务器上指定文件夹下的文件也备份到指定共享文件夹中,并发邮件通知
在windows环境下通过Python对重要的文件进行备份,备份的文件将打包为zip格式。由于windows环境下不提供标准的zip命令进行打包,因此要通过GnuWin32安装zip命令,并将C:\Program Files (x86)\GnuWin32\bin添加到系统...
使用python语言写的打包备份文件脚本,使用tar命令进行打包。自定义备份目录,和备份源文件目录。
python定期备份交换机配置文件二
备份文件
python自动备份mysql数据库,并删除七天前文件。 省去每天手动备份 删除 空出的时间摸鱼不香吗? 备份时间可自己设置 备份文件夹可自己设置 删除文件时间范围可自己设置
python Windows 压缩备份文件(支持多文件夹)
/usr/local/python import os import time import string source=[‘/var/www/html/xxx1/’,’/var/www/html/xxx2/’] target_dir=’/backup/’ target=target_dir+time.strftime(‘%Y%m%d’) zip_comm=’zip -r %s ...
下面小编就为大家带来一篇python 简单备份文件脚本v1.0的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望对大家有所帮助
介绍使用python结合mysqldump对mysql数据库进行备份
用python实现简易文本备份的功能: 主要运用: 1、python的os库 2、文件读写相关操作 """ @author:maoWeiyang @file:yes.py @ide:PyCharm @time:2020/3/1317:57 """ import os import os.path class FileBackup...
只要是交换机支持ftp都可以用此脚本进行备份,只需要改一下交换机配置文件的名称,不同厂家交换机备份文件名称不一样。同时需要交换机开启ftp
py交换机自动备份脚本。支持华为、思科、华三、 Aruba交换机。将设备ip、账号、密码、写入配置文件运行即可。多台设备一键备份。记录日志,自定义备份位置。需要先安装调用的模块,怎么安装自行百度。