日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python文件实时同步_python文件自动同步备份v1.2【运维必备】2020/12/31

發布時間:2024/1/23 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python文件实时同步_python文件自动同步备份v1.2【运维必备】2020/12/31 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本帖最后由 We. 于 2021-1-4 08:18 編輯

v1版本打包在這里了,感興趣的自己下來看:

同步備份v1.rar

(1.6 KB, 下載次數: 8)

2020-12-30 16:12 上傳

點擊文件名下載附件

下載積分: 吾愛幣 -1 CB

聲明:

感謝@的提醒,本方案只適用于局域網內同步備份,沒有做加密/認證,沒有過防火墻。

以下是v1.2內容:

新增加了多進程下載

修改下目錄就可以直接拿去用了

--------------------------------------------------------------------------------------------------------------------------

需求:平臺會把虛擬機備份的文件打包到服務器A,再同步備份到服務器B(只需要考慮A到B)。

思路:

服務器A作為服務端,定時遍歷自己的文件目錄,把文件目錄信息打包成一個校驗文件。

服務器B作為客戶端,下載校驗文件,遍歷自己的文件目錄是否和服務器相同,并下載本地沒有的文件。

通過http傳輸,使用python開啟一個簡單的http服務。有防火墻需要把端口放通,沒有就不管。

生產環境:python3.7.9,兩臺CentOS7.9服務器。

在服務端的備份目錄下開啟http服務:

nohup是用來后臺開啟http服務的,不然控制臺沒法干其他事情。

image.png (23.68 KB, 下載次數: 0)

2020-12-30 16:16 上傳

服務端:

[Python] 純文本查看 復制代碼import os

path = '/H3C_Backup'

def func(path):

contents = os.walk(path, topdown=True)

dir = []

file = []

for (root, dirs, files) in contents:

dir.append(root)

for i in files:

file.append(root+'/'+i)

return [dir, file]

content = func(path)

with open(path+'/'+'content.txt', 'w', encoding='utf-8') as f:

for i in content[0]:

f.write(i)

f.write('\n')

with open(path+'/'+'file.txt', 'w', encoding='utf-8') as f:

for i in content[1]:

f.write(i)

f.write('\n')

客戶端:

[Python] 純文本查看 復制代碼import os

import time

import shutil

import multiprocess

import requests

def init() :

url = ['http://172.172.172.1:8000/file.txt', 'http://172.172.172.1:8000/content.txt']

download_file = requests.get(url[0], stream=True)

with open('/download/file.txt', 'wb') as f :

for chunk in download_file.iter_content(chunk_size=4096) :

f.write(chunk)

download_content = requests.get(url[1], stream=True)

with open('/download/content.txt', 'wb') as f :

for chunk in download_content.iter_content(chunk_size=4096) :

f.write(chunk)

def function(path) :

# 通過os.walk()方法遍歷到所有文件夾和文件

file = []

dir = []

x = os.walk(path, topdown=True)

for (root, dirs, files) in x :

dir.append(root)

for i in files :

file.append(root + '/' + i)

return [dir, file]

def check_dir(path) :

# 獲取本地目錄

x = function(path)

dir_so = x[0]

# 清洗服務端目錄

dirs = open('/download/content.txt', 'r', encoding='utf-8')

dir_dst = dirs.readlines()

dir_dst_info = []

for i in dir_dst :

i = i.replace('\n', '')

print(i)

dir_dst_info.append(i)

# 比較目錄,目錄不一致就添加

for i in dir_dst_info[1 :] + dir_so :

if i not in dir_so :

os.mkdir(i)

print('創建了' + i)

if i not in dir_dst_info :

try :

shutil.rmtree(i)

print('刪除了' + i)

except :

pass

def download(url, path) :

download_file = requests.get(url, stream=True)

with open(path, 'wb') as f :

for chunk in download_file.iter_content(chunk_size=10240) :

f.write(chunk)

print('添加了' + path)

def check_file(path) :

x = function(path)

file_so = x[1]

pool = multiprocessing.Pool(processes=10)

# 清洗服務端文件

files = open('/download/file.txt', 'r', encoding='utf-8')

files_dst = files.readlines()

files_dst_info = []

for i in files_dst :

i = i.replace('\n', '')

files_dst_info.append(i)

# 沒有的下載,多余的刪掉

for i in file_so + files_dst_info :

if i not in file_so :

url = 'http://172.172.172.1:8000' + i

pool.apply_async(download, (url, i,))

if i not in files_dst_info :

os.remove(i)

print('刪除了' + i)

pool.close()

pool.join()

if __name__ == '__main__' :

path = '/H3C_Backup'

init()

check_dir(path)

check_file(path)

10個進程起飛,一共12T數據慢慢跑。

image.png (116.95 KB, 下載次數: 0)

2020-12-30 18:02 上傳

12個進程一起跑這cpu占用率有點高啊。

image.png (47.66 KB, 下載次數: 0)

2020-12-30 18:32 上傳

速度也不算慢,一小會兒80個G了。

image.png (78.08 KB, 下載次數: 0)

2020-12-30 18:34 上傳

今早起來一看,傳了10個T了,還在運行,等他慢慢弄完把。

image.png (29.79 KB, 下載次數: 0)

2020-12-31 08:55 上傳

待優化:

1、寫法待優化

2、觸發方式待優化

3、用socket的tcp會不會比http更快?

另外,為什么多線程這么拉跨比單線程還慢?總感覺多進程有點浪費cpu資源。迅雷的下載方式又是如何實現的?

歡迎指教。

總結

以上是生活随笔為你收集整理的python文件实时同步_python文件自动同步备份v1.2【运维必备】2020/12/31的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。