Python rsync 伺服器之間資料夾同步指令碼
About rsync:https://download.samba.org/pub/rsync/rsync.html
配置兩臺伺服器之間ssh-key後,可以實現自動化無需手動輸入密碼,指令碼如下:
import argparse
import datetime
from functools import partial
import multiprocessing as mp
import os
import paramiko as pmk
import time
def check_ssh(host, user, port, passwd, dest_path):
ssh_client = pmk.SSHClient()
ssh_client.load_system_host_keys()
ssh_client.set_missing_host_key_policy(pmk.AutoAddPolicy())
try:
ssh_client.connect(host, username=user, port=port, timeout=10, password=passwd)
ssh_client.exec_command('mkdir ' + os.path.join(dest_path, 'data'))
except BaseException as e:
print 'failed to connect to host: %r: %r' % (host, e)
return False
else:
return True
def select_from_file(file_path):
file_list = []
if os.path.exists(file_path):
path_dir = os.listdir(file_path)
for all_dir in path_dir:
file_list.append(os.path.join('%s' % all_dir))
return file_list
def sync_file(file_name, remote_host, remote_user, remote_port, src_path, dest_path):
sync_cmd = "rsync -azrvhP --progress -e 'ssh -p " + str(remote_port) + "' --skip-compress=gz/zip/ " + \
file_name + " " + remote_user + "@" + remote_host + ":" + os.path.join(dest_path,'data')
print sync_cmd
os.chdir(src_path)
os.system(sync_cmd)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-w', '--workers', dest='workers', type=int, default=12)
parser.add_argument('-H', '--host', dest='host', type=str, default='192.168.254.156')
parser.add_argument('-u', '--user', dest='user', type=str, default='shubao')
parser.add_argument('-p', '--password', dest='password', type=str, default='123456')
parser.add_argument('-P', '--port', dest='port', type=int, default=22)
parser.add_argument('-r', '--remotepath', dest='remotepath', type=str, default='/home/shubao/')
parser.add_argument('-s', '--srcpath', dest='srcpath', type=str, default='/home/Jesse/data')
args = parser.parse_args()
if not check_ssh(args.host, args.user, args.port, args.password, args.remotepath):
print 'SSH connect faild!'
exit(-1)
pool = mp.Pool(processes=args.workers)
try:
while True:
print "New check start at %s..." % str(datetime.datetime.now())
file_list_ = select_from_file(args.srcpath)
print "File_list: "
print file_list_
p_work = partial(sync_file, remote_host=args.host, remote_user=args.user, remote_port=args.port,
src_path=args.srcpath, dest_path=args.remotepath)
pool.map(p_work, file_list_)
time.sleep(10)
finally:
pool.terminate()
pool.join()
相關文章
- Linux之rsync同步分發指令碼編寫.Linux指令碼
- rsync詳解_伺服器同步資料伺服器
- 資料同步rsync
- Rsync資料同步
- 資料同步——rsync遠端同步
- Linux rsync配置用於伺服器之間傳輸大量的資料Linux伺服器
- 搭建rsync服務並同步重要資料
- rsync+inotify資料的實時同步
- 3576.02空間指令碼brain.script,02檔案, 02“XX_brain”資料夾, 01空間資料夾, 04TB檔案指令碼AI
- rsync客戶端一鍵安裝rsync指令碼(原始碼)客戶端指令碼原始碼
- Linux 檔案同步工具之 rsyncLinux
- 大資料叢集服務啟停指令碼/常用埠/時間同步大資料指令碼
- Python學習筆記|Python之pycache資料夾Python筆記
- python指令碼批次建立資料表Python指令碼
- Windows ntp時間同步設定(bat指令碼)WindowsBAT指令碼
- docker基礎:mysql容器建立,時間同步,掛載到資料夾DockerMySql
- Sync Folders Pro for Mac 資料夾資料同步工具Mac
- Sync Folders Pro for Mac資料夾資料同步工具Mac
- Sync Folders Pro for mac - 資料夾資料同步工具Mac
- rsync 同步目錄
- rsync實時同步
- rsync遠端同步
- Elasticsearch批量匯入資料指令碼(python)Elasticsearch指令碼Python
- 同步線上伺服器程式碼到web節點指令碼伺服器Web指令碼
- 教你如何運用python實現不同資料庫間資料同步功能Python資料庫
- Rsync 資料備份
- (20201026已解決)bash指令碼中給rsync指定密碼指令碼密碼
- 批次非同步上傳aws圖片指令碼(python)非同步指令碼Python
- 適合時間序列資料的計算指令碼指令碼
- Linux下如何使用Rsync備份伺服器重要資料Linux伺服器
- 批處理指令碼:遞迴移動資料夾內所有檔案指令碼遞迴
- lvs 負載均衡兩臺Discuz+rsync+innotify實現資料同步負載
- 【Python】Python 使用http時間同步設定系統時間原始碼PythonHTTP原始碼
- rsync+inotify實時同步
- 在Docker容器和主機之間複製檔案/資料夾Docker
- 在Linux中,rsync 同步資料時,如何過濾出所有.txt的檔案不同步?Linux
- mklink命令使得OneDrive同步任意一個資料夾
- Mac技巧|如何阻止 iCloud 同步某個資料夾?MacCloud
- hadoop_批量命令指令碼&同步檔案指令碼Hadoop指令碼