python實現增量備份目錄

來源:互聯網
上載者:User

標籤:python

# !/usr/bin/env python
# -*- coding: UTF-8 -*-

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 = str(raw_input('請輸入需要備份目錄:'))
# 備份檔案目錄
path_bak = str(raw_input('請輸入儲存目錄'))
# 開始備份
bak_file(path, path_bak)

python實現增量備份目錄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.