Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >一个Python备份脚本

一个Python备份脚本

原创
作者头像
大师级码师
修改于 2021-09-22 02:47:12
修改于 2021-09-22 02:47:12
57700
代码可运行
举报
文章被收录于专栏:大师级码师大师级码师
运行总次数:0
代码可运行

编写一个Python脚本,实现为重要的文件或文件夹在指定的目录下创建备份。 [设计思路] [1] 将需要备份的文件和目录由一个列表指定,通过传入参数获得并保存到列表中。 [2] 备份应该保存在主备份目录中。 [3] 将文件备份成一个压缩文件。 [4] 每一次备份都根据当前的日期在主备份目录中创建一个子文件夹,而所备份的文件命名为当期的时间保存在这个子文件夹中。 [5] 压缩命令由本地用户决定。可以使用任何本地的存档压缩命令,只要它有命令行界面就可以了,那样就可以从脚本中传递参数给它。 [参考] [1] A Byte of Python, 2005 [2] Python Manuals 2.6

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    #! /usr/bin/python

# Filename: backup_ver1.py  
# 2010-7-12 wcdj  
import os  
import time  
import sys  
# 1, The files and directories to be backed up are specified in a list  
# source = ['/home/wcdj/my_prog', '/home/wcdj/local_installed']  
# The following information is the debug used  
print '--------------------------------'  
source=[]  
print 'The command line arguments are: '  
for i in sys.argv:  
    print i  
    if i == sys.argv[0]:  
        continue  
    source.append(i)  
print source  
print '--------------------------------'  
# check input, if error app exit  
if len(source) == 0:  
    print '''''You should input the files or directories, like 
python backup_ver1.py /home/wcdj/myfile /home/wcdj/mydir ...'''  
    exit()  
else:  
    print 'Some files or directorier will be saved into .tar.gz format: '  
    print source  
# If you are using Windows, use   
# source=[r'c:/Documents', r'd:/work'] or   
# source=['c://Documents', 'd://work'] or   
# something like that  
# 2, The backup must be stored in a main backup directory  
# Remember to change this to what you will be using  
target_dir = '/home/wcdj/backup/'  
# 3, The files are backed up into a tar file  
# 4, The name of subdirectory and tar file  
today = target_dir + time.strftime('%Y%m%d')  
now = time.strftime('%H%M%S')  
# Take a comment from the user to create the name of the tar file  
comment = raw_input('Enter a comment: ')  
if len(comment) == 0:# check if a comment was entered  
    target = today + os.sep + now + '.tar.gz'  
else:  
    target = today + os.sep + now + '_' + /  
    comment.replace(' ', '_') + '.tar.gz'  
#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 tar command(in Unix/Linux) to put the files in a tgz archive  
tar_command = "tar -zcf %s %s" % (target, ' '.join(source))  
# Run the backup  
if os.system(tar_command) == 0:  
    print 'Successful backup to', target  
else:  
    print 'Backup failed'  
# end  </pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
[PYTHON]python 基础笔记(
尽管这是一个简单的问题,但是问题本身并没有给我们足够的信息来解决它。进一步的分析是必需的。例如,我们如何确定该备份哪些文件?备份保存在哪里?我们怎么样存储备份?
py3study
2020/01/14
6920
python---备份目录和文件
1.需要备份的文件和目录由一个列表指定。 2.备份应该保存在主备份目录中。 3.文件备份成一个tar.gz文件。 4.zip存档的名称是当前的日期和时间。 5.我们使用标准的tar.gz命令.
py3study
2020/01/06
8520
从0开始的Python学习013编写一个Python脚本
通过之前的学习我们已经了解了Python的很多基础运用了,现在我们尝试着做一个有使用价值的小脚本。
Happy、Liu
2019/04/24
9360
从0开始的Python学习013编写一个Python脚本
Python 学习笔记: 备份工具
1 #!/usr/bin/python  2 #filename:backup_ver3.py  3 import os  4 import time  5   6 source=['/media/Work/WorkSpaces/gooapp','/media/Work/WorkSpaces/RsaTool']  7 target_dir='/media/Work/backup/'  8 today = target_dir + time.strftime('%Y%m%d')  9 now = time.s
用户3135539
2018/09/12
4410
Python自动化备份系统及网站
随着目前IT迅猛的发展,自动化运维对于Linux运维人员也越来越重要,传统的运维方式靠大量的人力,现在也逐渐转向自动化运维,我们常见的跟自动化有关的软件有哪些呢。
py3study
2020/01/07
9310
利用Python进行文件的自动备份(第三版和第四版)
# 利用python进行文件的自动备份(第三版和第四版) # 代码 import os import time ''' 第三版和第四版差别不大整合在一起,用户注释的内容可以添加到文件名中来实现 ''' source = [r'E:\aaa.txt'] target_dir = 'E:\\Backup\\' if not os.path.exists(target_dir): os.mkdir(target_dir) # 子目录名称 today = target_dir + os.sep + t
benym
2022/07/14
6000
python常用的备份脚本
4)source 可以修改为想备份的目录,因为备份目录一般不经常变动,所以这里写死了
py3study
2020/01/07
1.1K0
python脚本备份
#!/usr/bin/python #encodeing:utf-8 import os import time import string #需要备份的目录 source = ['/usr/local/nagios','/var/www/html/cacti'] #存放备份的路径 target_dir = '/data/backup/nagios&cacti' #目录日期 today = target_dir + time.strftime('%Y%m%d') #当前时间 now = time.strft
py3study
2020/01/10
7080
Python:实现文件归档
实现功能: 将E:\123文件备份至E:\backup 文件夹下,以当前的日期为子目录存放备份后的文件 #! /usr/bin/python #Filename:backup.py #功能说明:备份文件,以当前日期为子目录存放备份后的文件 import os import time #要备份的目录,可在此列表中增加 source = [r'E:\123'] #备份文件存放的目录 target_dir = 'E:\\backup\\' #取当前时间为备份子目录名 today = targ
IT架构圈
2018/06/01
9570
利用Python进行文件的自动备份(第二版)
# 利用python进行文件的自动备份(第二版) # 代码 import os import time ''' 第二版是第一版的改进, 启用一种更好的文件命名机制——使用时间作为文件名,存储在以当前时间为名字的文件夹中,这一文件夹则照常存储在主备份目录下。 ''' # 例如在 Windows 下: source = [r'E:\aaa.txt'] # 在这里要注意到我们必须在字符串中使用双引号 # 用以括起其中包含空格的名称。 # 2. 备份文件必须存储在一个 # 主备份目录中 # 例如在 Windo
benym
2022/07/14
4880
python备份脚本解析
5)source 可以修改为想备份的目录,因为备份目录一般不经常变动,所以这里写死了
py3study
2020/01/09
6300
用python备份文件
光说不练假把式,用小脚本学习Python。 一个简单的备份脚本。 #!/usr/bin/env python3 #-*- coding:utf-8 -*- #for backup import os import time #需要备份的目录 source = ['/var/log/history/','/var/log/httpd/'] #保存备份的目录 target_dir = '/tmp/' today_dir = target_dir + time.strftime('%Y%m%d') time_d
py3study
2020/01/14
1.4K0
python备份目录脚本
#!/usr/bin/env python #backup app python script. import os import time import sys
py3study
2020/01/10
9460
利用Python进行文件的自动备份
os.system 函数的命令,这一函数可以使命令像是从系统中运行的。也就是说,从 shell 中运行的——如果运行成功,它将返回 0 ,如果运行失败,将返回一个错误代码。
benym
2022/07/14
8250
Notes for python (2)
#!/usr/bin/python # Filename: using_tuple.py zoo = ('wolf', 'elephant', 'penguin') print 'Number of animals in the zoo is', len(zoo) new_zoo = ('monkey', 'dolphin', zoo) print 'Number of animals in the new zoo is', len(new_zoo) print 'All animals in new zoo are', new_zoo print 'Animals brought from old zoo are', new_zoo[2] print 'Last animal brought from old zoo is', new_zoo[2][2]
py3study
2020/01/13
6600
Python学习笔记四(Python
Python os模块提供了一个统一的操作系统接口函数,通过python os模块可以实现对系统本身的命令,文件,目录进行操作,官方参考文档( http://docs.python.org/library/os)。 1)os.sep 可以取代操作系统特定的路径分割符(在Linux、Unix下它是'/',在Windows下它是'\\',而在Mac OS下它是':')。
py3study
2020/01/10
3700
python 脚本实现备份文件
''' 功能:备份文件 版本:1.0 作者:白 ''' import  os,time,sys d_dir = '/data/backup/' d_file = 'system_bak.tar.gz' s_dir = ['/etc','/boot','/var/www/html'] date = time.strftime('%Y%m%d') r_dir = d_dir + date + '/' r_name = r_dir + d_file def all_bak():     print('Backup
py3study
2020/01/08
1.3K0
怎样用Python解压缩*.tgz文件
KnightPython: 正如*大熊*所说,使用tarfile模块即可: import tarfile tarobj = tarfile.open("my_backup_file.tgz", "r:gz") for tarinfo in tarobj:     tarobj.extract(tarinfo.name, r"d:/temp/backup") tarobj.close() 十分感谢!
py3study
2020/01/10
4.3K0
利用Python编写linux自动备份脚
  周末的时候帮朋友写了一个备份需求的脚本,现在整理一下,分享出来使用Python语言的Fabric模块,这里就不扫盲了,运维必用的Python模块;
py3study
2020/01/07
9610
python windows备份文件in
看着python简明教程学习python,备份文件的时候老是出现问题。这里记录一下解决方案。
py3study
2020/01/11
1.6K0
相关推荐
[PYTHON]python 基础笔记(
更多 >
领券
一站式MCP教程库,解锁AI应用新玩法
涵盖代码开发、场景应用、自动测试全流程,助你从零构建专属AI助手
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验