Python增量備份實現(xiàn)技巧分享
作者:佚名
我們今天將會通過對一段代碼的解讀來詳細了解一下有關(guān)Python增量備份的具體操作方法,希望可以給大家?guī)硪恍椭?/div>
大家在了解了Python這一編程語言之后,會發(fā)現(xiàn)它在一些特定環(huán)境中的應(yīng)用方式是非常簡便的,而且能夠很好的幫助開發(fā)人員完成這些環(huán)境下的功能需求。在這里我們先來一起了解一下Python增量備份的相關(guān)操作。
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):
- #如果在備份目錄中文件夾不存在則創(chuàng)建
- 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:
- #如果文件已經(jīng)存在,則比較文件修改時間
- 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)
以上就是我們對Python增量備份的相關(guān)操作方法的介紹。
【編輯推薦】
責任編輯:曹凱
來源:
博客園


相關(guān)推薦
2010-09-06 16:02:00




