• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python实现网站文件的全备份和差异备份

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

作者:mdxy-dxy 字体:[增加 减小] 来源:互联网

mdxy-dxy 通过本文主要向大家介绍了python备份,python 备份文件,python备份脚本,python做网站,python开发网站实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

之前有写利用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()
</div>

测试:

$ 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
</div>

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

</div>

您可能想查找下面的文章:

  • Python 备份程序代码实现
  • Python备份目录及目录下的全部内容的实现方法
  • Python实现新浪博客备份的方法
  • Python实现新浪博客备份的方法
  • Python实现网站文件的全备份和差异备份
  • Python实现备份文件实例
  • python备份文件以及mysql数据库的脚本代码
  • python备份文件以及mysql数据库的脚本代码
  • Python备份Mysql脚本
  • python备份文件的脚本

相关文章

  • Python中的生成器和yield详细介绍
  • Python中dictionary items()系列函数的用法实例
  • 以windows service方式运行Python程序的方法
  • Python使用Supervisor来管理进程的方法
  • python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)
  • 详解Python中的__new__、__init__、__call__三个特殊方法
  • Python实现树莓派WiFi断线自动重连的实例代码
  • python实现博客文章爬虫示例
  • Python爬取qq music中的音乐url及批量下载
  • 使用Python的Tornado框架实现一个一对一聊天的程序

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Python字典简介以及用法详解
    • go和python调用其它程序并得到程序输出
    • python实现堆栈与队列的方法
    • 在Python中使用CasperJS获取JS渲染生成的HTML内容的教程
    • 用Python代码来解图片迷宫的方法整理
    • 编写Python CGI脚本的教程
    • python&MongoDB爬取图书馆借阅记录
    • python pickle 和 shelve模块的用法
    • python过滤字符串中不属于指定集合中字符的类实例
    • 在Heroku云平台上部署Python的Django框架的教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有