python linux备份程序吗,【Python笔记】no.3 练习-实现linux下的差异备份

前序

首先,说明一下我的程序大概能做什么事:标题是实现linux下的差异备份,但我这里只是实现基本的功能,没有针对输出格式错误的时候进行处理!差异备份是指在一次全备份后到进行差异备份的这段时间内,对那些增加或者修改文件的备份,但是我这里只能备份增加的和对文件名修改过的文件进行备份,不能备份只修改内容的文件,后续会解决。

代码:

#coding=utf-8

import mbackup'''备份写入日志模块''',sys,time,dif'''计算差异文件模块''',getfilename,datetime

#上面mbackup/dif/getfilename三个模块是另写的,在下面有

#时间部分#

t=datetime.datetime.now()

h=t.hour

mi=t.minute

s=t.second

##########

#主程序部分#

oldpath=sys.argv[1]

oldlist=[]

getfilename.getfn(oldpath,oldlist)

time.sleep(5)

newpath=oldpath

newlist=[]

getfilename.getfn(newpath,newlist)

diflist=[]

dif.dif(newlist,oldlist,diflist)

if len(diflist) !=0:

print 'ok'

print diflist

else:print 'none'

##########

#开始备份#

#print '###start backuping###'

result=mbackup.bk(diflist)

if result == 0:

print 'backup operation is success! backup files in /home/leibackup !'

else:print'oh,backup operation is failed!'

#########

#时间部分#

t1=datetime.datetime.now()

nh=t1.hour-h

nmi=t1.minute-mi

ns=t1.second-s

##########

print 'The program runs for %shours %smin %ss !'% (nh,nmi,ns)

功能:根据所给的目录获取目录下的文件名并写入列表

#coding=utf-8

#运行函数请提供下面两个参数,函数结果是将目录下的所有文件名录入列表中!

import os

def getfn(path,list):

for i in os.listdir(path):

list.append(str(i))

if len(list) != 0:

print 'Got all filenames in %s!'%s list

else:print 'the input dir is not exists!'

功能:根据给的两个列表a、b,a为最新的目录,将a较b不同的元素写入新的列表中

#coding=utf-8

import sys,os,time,logger

#运行函数请给出两个参数:newf(新目录下的文件名列表) oldf(旧的)

#函数结果是将新目录的所有新文件名录入列表 diflist[] 中!

def dif(newf,oldf,diflist):

la = len(newf)

lb = len(oldf)

for aa in range(0,la):

for bb in range(0,lb):

if newf[aa] ==oldf[bb]:

break

elif bb==lb-1:

diflist.append(newf[aa])

功能:备份所给的列表中的文件,并写入日志

#coding=utf-8

import os,time,dif_logger#dif_logger这个模块在下一个

t=time.strftime('%Y-%m-%d_%H-%M-%S')

def bk(diflist):

for i in diflist:

backup_cmd='cp /home/lei/%s /home/leibackup/%s_home_lei_%s'% (i,t,i)

run=os.system(backup_cmd)

source_file='/home/lei/%s'% i

backup_file='/home/leibackup/%s_home_lei_%s'% (t,i)

if run ==0:

dif_logger.record_log(source_file,backup_file,'success')

if run ==0:

return 0

功能:将备份操作写入日志

import time

logfile = 'log.log'#log.log这个文件要存在于运行命令的目录

#def record_log(acount,expense,costtype,interest):

def record_log(a,b,c):

date = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())

record_line = "%s %s %s '%s' \n" % (date,a,b,c)

f= file(logfile,'a')

f.write(record_line)

f.flush()

f.close()

运行程序

root@controller:~# python linux_diff.py /home/lei

因为在代码中,我设定时间沉睡5s,所以在5s内我们要新增一个文件到/home/lei这个目录

root@controller:/home/lei# ls

666666.txt  file.txt  gainfilename.py  tt.py

root@controller:/home/lei# cp tt.py 123123123.txt #(我这里使用的是securecrt工具克隆了一个远程窗口)

然后程序自动运行...

root@controller:~# python linux_diff.py /home/lei

Got all filenames in list!

Got all filenames in list!

ok

['123123123.txt']

backup operation is success! backup files in /home/leibackup !

the program runs for  0hours 0min 5s !

root@controller:~# more log.log

2017-09-15 23:18:13   backup  'Failure'

2017-09-17 00:23:14   /home/lei/123.txt  '/home/leibackup/2017-09-17_00-23-09_home_lei_1

23.txt'

2017-09-17 00:26:47   /home/lei/123123123.txt  /home/leibackup/2017-09-17_00-26-42_home_

lei_123123123.txt 'success'

root@controller:/home/lei# ls /home/leibackup/

2017-09-17_00-11-09_home_lei_666666.txt    2017-09-17_00-26-00_home_lei_123123.txt

2017-09-17_00-15-23_home_lei_qwqweqwe.txt  2017-09-17_00-26-42_home_lei_123123123.txt

2017-09-17_00-23-09_home_lei_123.txt       home_lei_lei.txt

可以看到,备份成功,且日志已写入!

后序

上面采用了沉睡时间来模拟时间间隔,真实环境肯定不能这样,但核心功能已经实现,若要写个用于真实环境的差异备份脚本,理清思路即可:也就是先在‘今天’单独运行getfilename脚本得到旧目录下的所有文件名列表1,然后在‘明天’运行getfilename得到新目录下的所有文件名列表2,然后就不用我说了吧。

谢谢你的阅览,如你有建议或发现笔记中有错误可向我提出,谢谢~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值