大家在了解了Python这一编程语言之后,会发现它在一些特定环境中的应用方式是非常简便的,而且能够很好的帮助开发人员完成这些环境下的功能需求。在这里我们先来一起了解一下Python增量备份的相关操作。
Python增量备份代码示例:
#!/usr/bin/python
#-*-coding:utf-8-*-
#Filename: auto_bak.py
#Author: zz
import os
import sys
def get_dir(path):
print path, '\n'
return os.listdir(path)
def bak_file(path,path_bak):
list = os.listdir(path)
for l in list:
file_path = os.path.join(path, l)
file_path_bak = os.path.join(path_bak, l)
print file_path
#如果文件路径为目录
if os.path.isdir(file_path):
#如果在备份目录中文件夹不存在则创建
if not os.path.isdir(file_path_bak):
create_com = '''mkdir -p '%s' ''' \
% (file_path_bak)
if os.system(create_com) == 0:
print create_com
else:
print 'create folder failure!'
os._exit(0)
bak_file(file_path, file_path_bak)
else:
#如果文件已经存在,则比较文件修改时间
if os.path.isfile(file_path_bak):
stat_bak = os.stat(file_path_bak)
stat_source = os.stat(file_path)
#判断文件修改时间
if stat_source.st_mtime <= stat_bak.st_mtime:
continue
cp_com = '''cp '%s' '%s' ''' \
% (file_path, file_path_bak)
if os.system(cp_com) == 0:
print cp_com
else:
print 'create folder failure!'
os._exit(0)
#要备份的文件目录
path = '/home/zyf/appspot/auto_bak/a'
#备份文件目录
path_bak = '/home/zyf/appspot/auto_bak/bak'
#开始备份
bak_file(path, path_bak)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
以上就是我们对Python增量备份的相关操作方法的介绍。
【编辑推荐】