Python实现网站文件的全备份和差异备份

作者:mdxy-dxy 时间:2022-11-01 04:26:14 

之前有写利用md5方式来做差异备份,但是这种md5方式来写存在以下问题:

•md5sum获取有些软连接的MD5值存在问题
•不支持对空目录进行备份,因为md5sum无法获取空目录的md5值
•权限的修改md5sum无法判断

解决方案:

利用文件的mtime ctime

mtime(Modified time)是在写入文件时随文件内容的更改而更改的
ctime(Create time)是在写入文件、更改所有者、权限或链接设置时随Inode的内容更改而更改的
废话不多说直接上代码:


#!/usr/bin/env python
import time,os,sys,cPickle

fileInfo = {}

def logger(time,fileName,status,fileNum):
 f = open('backup.log','a')
 f.write("%s\t%s\t%s\t\t%s\n" % (time,fileName,status,fileNum))

def tar(sDir,dDir,fileNum):
 command = "tar zcf %s %s >/dev/null 2>&1" % (dDir + ".tar.gz",sDir)
 if os.system(command) == 0:
   logger(time.strftime('%F %X'),dDir + ".tar.gz",'success',fileNum)
 else:
   logger(time.strftime('%F %X'),dDir + ".tar.gz",'failed',fileNum)

def fullBak(path):
 fileNum = 0
 for root,dirs,files in os.walk(path):
   for name in files:
     file = os.path.join(root, name)
     mtime = os.path.getmtime(file)
     ctime = os.path.getctime(file)
     fileInfo[file] = (mtime,ctime)
     fileNum += 1
 f = open(P,'w')
 cPickle.dump(fileInfo,f)
 f.close()
 tar(S,D,fileNum)

def diffBak(path):
 for root,dirs,files in os.walk(path):
   for name in files:
     file = os.path.join(root,name)
     mtime = os.path.getmtime(file)
     ctime = os.path.getctime(file)
     fileInfo[file] = (mtime,ctime)

if os.path.isfile(P) == 0:
   f = open(P,'w')
   f.close()

if os.stat(P).st_size == 0:
   f = open(P,'w')
   cPickle.dump(fileInfo,f)
   fileNum = len(fileInfo.keys())
   f.close()
   print fileNum
   tar(S,D,fileNum)
 else:
   f = open(P)
   old_fileInfo = cPickle.load(f)
   f.close()
   difference = dict(set(fileInfo.items())^set(old_fileInfo.items()))
   fileNum = len(difference)
   print fileNum

difference_file = ' '.join(difference.keys())
   print difference_file

tar(difference_file,D,fileNum)
   f = open(P,'w')
   cPickle.dump(fileInfo,f)
   f.close()

def Usage():
 print '''
   Syntax: python file_bakcup.py pickle_file model source_dir filename_bk
     model: 1:Full backup 2:Differential backup

example: python file_backup.py fileinfo.pk 2 /etc etc_$(date +%F)
     explain: Automatically add '.tar.gz' suffix
 '''
 sys.exit()

if len(sys.argv) != 5:
 Usage()

P = sys.argv[1]
M = int(sys.argv[2])
S = sys.argv[3]
D = sys.argv[4]

if M == 1:
 fullBak(S)
elif M == 2:
 diffBak(S)
else:
 print "\033[;31mDoes not support this mode\033[0m"
 Usage()

测试:


$ python file_backup.py data.pk 1 data data_$(date +%F) #全备份
$ > data/www.jb51.net #测试创建文件,修改文件权限
$ chmod 777 data/py/eshop_bk/data.db
$ python file_backup.py data.pk 2 data data_$(date +%F)_1 #备份改变的文件
2
data/py/eshop_bk/data.db data/www.jb51.net

看了博主的代码,很受启发,但是有一个问题,如果我完成完整备份之后,删除了其中某个文件,再做差异备份,可以检测出被删除的文件,但是执行tar就会出错,因为这个文件已经是不存在的了,所以在执行tar之前,最好用os.path.exists()判断一下差异文件路径是否存在,如果不存在则不执行tar, 反馈一条文件删除信息。

标签:全备份,差异备份
0
投稿

猜你喜欢

  • 基于python实现学生管理系统

    2021-11-24 21:07:48
  • workerman写mysql连接池的实例代码

    2024-01-20 02:52:26
  • GO语言(golang)基础知识

    2024-02-13 18:04:03
  • Python3中的算术运算符详解

    2022-04-13 03:01:45
  • 手把手教你用python抢票回家过年(代码简单)

    2023-07-13 22:46:02
  • photoshop快捷键大全及使用技巧

    2007-10-26 07:40:00
  • pytorch geometric的GNN、GCN的节点分类方式

    2022-12-24 16:01:23
  • Django学习笔记之ORM基础教程

    2022-07-25 22:59:07
  • python re的findall和finditer的区别详解

    2022-05-19 23:04:33
  • CentOS7.2虚拟机上安装MySQL 5.6.32的教程

    2024-01-23 07:30:59
  • 加快Firefox 3.5启动速度的方法

    2009-07-16 15:22:00
  • linux mysql5.5升级至mysql5.7的步骤与踩到的坑

    2024-01-21 17:09:08
  • python mysqldb连接数据库

    2024-01-15 17:52:56
  • 对Python中 \\r, \\n, \\r\\n的彻底理解

    2022-09-14 07:00:15
  • MySQL MyISAM 优化设置点滴

    2024-01-17 11:58:17
  • js选项卡的实现方法

    2024-04-22 22:23:51
  • Python中基本数据类型和常用语法归纳分享

    2023-09-08 21:08:01
  • JavaScript程序执行顺序问题总结

    2010-01-29 13:06:00
  • python处理json文件的四个常用函数

    2023-01-17 21:26:58
  • MySQL 视图 第1349号错误解决方法

    2024-01-18 14:45:41
  • asp之家 网络编程 m.aspxhome.com