欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)定時自動備份文件到其他主機的實例代碼

 更新時間:2018年02月23日 10:06:19   作者:lzhh  
這篇文章主要介紹了python實現(xiàn)定時自動備份文件到其他主機的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

定時將源文件或目錄使用WinRAR壓縮并自動備份到本地或網(wǎng)絡上的主機

1.確保WinRAR安裝在默認路徑或者把WinRAR.exe添加到環(huán)境變量中

2.在代碼里的sources填寫備份的文件或目錄,target_dir填寫備份目的目錄

3.delete_source_file為備份完后是否刪除源文件(不刪除子文件夾)

4.備份成功/失敗后生成備份日志

按照格式,填寫源目的:

sources = [r'E:\目錄1', r'E:\目錄2\b.txt'] #例:= [ r'E:\test\1234.txt', r'E:\test1']
target_dir = r'\\10.1.5.227\共享\備份'   #例:= r'D:\備份' 或 = r'\\10.1.5.227\共享目錄'
delete_source_file = False        #False/True

手動運行三次,已經(jīng)有兩個備份zip了

打開log查看為什么少了一個

可以看到目錄1備份失敗了,細看發(fā)現(xiàn),目錄1下的a.txt沒有權(quán)限(讀取),是因為用戶對該文件沒有權(quán)限。

如果該目錄或者子目錄下有一個沒有權(quán)限,會導致整個目錄都不能備份, 日志看到a.txt沒有權(quán)限.

第二次備份的時候?qū)⒃次募h除后,第三次備份就沒有文件備份了

接下來將腳本程序添加到win的計劃任務里,就能實現(xiàn)定時自動備份辣<( ̄︶ ̄)>

把代碼文件添加進來,同時也可以在這里添加參數(shù)-d, 指明備份完后刪除源文件

完整代碼

python3.0

# -*- coding=utf-8 -*-
#進行了一場py/etherchannel
import os, sys
import time
import logging
sources = [r'E:\視頻筆記', r'E:\目錄\b.txt'] #例:= [ r'E:\test\1234.txt', r'E:\test1']
target_dir = r'\\10.1.5.227\共享\備份'    #例:= r'D:\備份' 或 = r'\\10.1.5.227\共享目錄'
delete_source_file = False         #False/True
def Init_Logging(path):
  logging.basicConfig(level=logging.INFO, 
    format='%(asctime)s %(levelname)-8s %(message)s',  
    filename=path + '\\' + 'log.txt', 
    filemode='a',
    datefmt='%Y-%m-%d %X')
def Ctypes(message, title):
  import ctypes
  ctypes.windll.user32.MessageBoxA(0,message.encode('gb2312'), \
  title.encode('gb2312'),0)
  sys.exit()
def Check_Dir_Permit(dirs, dirc_permit=True, root=''):
  for dirc in dirs:
    dirc = os.path.join(root,dirc)
    try:
      os.chdir(dirc)
    except IOError as e:
      logging.error("找不到指定文件或沒有權(quán)限 >>> " + str(e))
      dirc_permit = False
  return dirc_permit
def Create_Directory(dir):
  if not os.path.exists(dir):
    try:
      os.mkdir(dir)
      print('Successfully created directory',dir)
    except IOError as e:
      Ctypes(u"target_dir 目錄路徑不存在 ", u' 錯誤')
  assert Check_Dir_Permit([dir]), Ctypes(u"target_dir 沒有權(quán)限 ", u' 錯誤')
  return dir
def Check_File_Permit(files, file_permit=True, root=''):
  for filename in files:
    file = os.path.join(root,filename)
    try:
      f = open(file)
      f.close()
    except IOError as e:
      logging.error("找不到指定文件或沒有權(quán)限 >>> " + str(e))
      file_permit = False
  return file_permit
def Permit_Source(sources):
  allow_sources = []
  disallow_sources = []
  for source in sources:
    file_permit = True
    dirc_permit = True
    for (root, dirs, files) in os.walk(source):
      file_permit = Check_File_Permit(files, file_permit,root=root)
      dirc_permit = Check_Dir_Permit(dirs, dirc_permit,root=root)
    if os.path.isdir(source) and file_permit and dirc_permit or \
      os.path.isfile(source) and Check_File_Permit([source], file_permit):
      allow_sources.append(source)
    else:
      disallow_sources.append(source)
  return (allow_sources,disallow_sources)
def Delete_Files(allow_sources):
  for source in allow_sources:
    if os.path.isdir(source):
      command = 'del /a/s/f/q ' + source  #/s:也把子文件夾的文件一并刪除
      if os.system(command) == 0:
        logging.info('del: ' + str(source))
      else:
        logging.error(str(source) + ' 刪除失敗')
    else:
      command = 'del /a/f/q ' + source
      if os.system(command) == 0:
        logging.info('del: ' + str(source))
      else:
        logging.error(str(source) + ' 刪除失敗')
def Compress_Backup(target, source):
  target = target + '\\' + time.strftime('%Y%m%d%H%M%S') + '.rar'
  if os.path.exists(r"C:\Program Files (x86)\WinRAR\WinRAR.exe"):
    rar_command = r'"C:\Program Files (x86)\WinRAR\WinRAR.exe" A %s %s' % (target,' '.join(source)) #WinRAR.exe" A %s %s -r'加上-r是作用到子文件夾中同名的文件
  else:
    rar_command = 'WinRAR' + ' A %s %s' % (target,' '.join(source))
  if os.system(rar_command) == 0: 
    print('Successful backup to', target)
    logging.info(str(source) + ' 備份到 ' + str(target) + ' 成功')
    try:
      if delete_source_file or sys.argv[1] == '-d':
        Delete_Files(source)
    except IndexError:
      pass
  else:
    logging.error("備份失敗:WinRAR出錯,確認路徑 或 壓縮被中斷")
    Ctypes(u"備份失敗:WinRAR出錯,確認路徑 或 壓縮被中斷", u' 錯誤')
if __name__ == '__main__':
  target_dir = Create_Directory(target_dir)
  Init_Logging(target_dir)
  logging.info('=' * 80)
  allow_sources, disallow_sources = Permit_Source(sources)
  if allow_sources:
    Compress_Backup(target_dir, allow_sources)
  if disallow_sources:
    print(disallow_sources, ' 備份失敗')
    logging.error(str(disallow_sources) + ' 備份失敗')

總結(jié)

以上所述是小編給大家介紹的python實現(xiàn)定時自動備份文件到其他主機的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • python django 實現(xiàn)驗證碼的功能實例代碼

    python django 實現(xiàn)驗證碼的功能實例代碼

    本篇文章主要介紹了python django 實現(xiàn)驗證碼的功能實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • python下os模塊強大的重命名方法renames詳解

    python下os模塊強大的重命名方法renames詳解

    這篇文章主要介紹了python下os模塊強大的重命名方法renames詳解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • python切換hosts文件代碼示例

    python切換hosts文件代碼示例

    開發(fā)或者測試網(wǎng)站程序時,我們很多時候都會遇到多個hosts文件來回切換,windows的hosts文件目錄比較深,麻煩,因此,用python寫了個小腳本來簡化此功能
    2013-12-12
  • 詳解pandas映射與數(shù)據(jù)轉(zhuǎn)換

    詳解pandas映射與數(shù)據(jù)轉(zhuǎn)換

    這篇文章主要介紹了pandas映射與數(shù)據(jù)轉(zhuǎn)換的相關(guān)資料,幫助大家更好的利用python進行數(shù)據(jù)分析,感興趣的朋友可以了解下
    2021-01-01
  • Python中計算圓周率的方法匯總(方法合集)

    Python中計算圓周率的方法匯總(方法合集)

    這篇文章主要介紹了Python中計算圓周率的方法匯總(方法合集),包括使用math庫中的pi常量,使用級數(shù)展開公式計算π,本文給大家列舉多種方法幫助大家學習,需要的朋友可以參考下
    2022-06-06
  • Python圖片處理模塊PIL操作方法(pillow)

    Python圖片處理模塊PIL操作方法(pillow)

    這篇文章主要介紹了Python圖片處理模塊PIL操作方法(pillow),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • PyMongo進行MongoDB查詢和插入操作的高效使用示例

    PyMongo進行MongoDB查詢和插入操作的高效使用示例

    這篇文章主要為大家介紹了PyMongo進行MongoDB查詢和插入操作的高效使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • python 實現(xiàn)在一張圖中繪制一個小的子圖方法

    python 實現(xiàn)在一張圖中繪制一個小的子圖方法

    今天小編就為大家分享一篇python 實現(xiàn)在一張圖中繪制一個小的子圖方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • 淺談Selenium+Webdriver 常用的元素定位方式

    淺談Selenium+Webdriver 常用的元素定位方式

    這篇文章主要介紹了淺談Selenium+Webdriver 常用的元素定位方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • TensorFlow人工智能學習Keras高層接口應用示例

    TensorFlow人工智能學習Keras高層接口應用示例

    這篇文章主要為大家介紹了TensorFlow人工智能學習中Keras高層接口的應用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-11-11

最新評論