Windows 使用rsync同步数据

  1. 首先在windows建立一个rsync的专用账户,并设置好密码

  2. 到下载站下载win版rsync的服务端和客户端,网址如下
    https://www.cr173.com/soft/110806.html

  3. 安装rsync server 服务端,并保持rsync账户与新建的windows账户一致
    在这里插入图片描述

  4. 安装完成后服务端目录结构
    在这里插入图片描述

  5. 修改rsyncd.conf配置

use chroot = false
strict modes = false
lock file = rsyncd.lock
hosts allow = *
max connections = 5
port = 35132
gid = 0
uid = 0

log file = rsyncd.log

# Module definitions
# Remember cygwin naming conventions : c:\work becomes /cygdrive/c/work

[data_backup]
# path = /cygdrive/c/dataBackup #  和linux不同的是windows的备份目录前面都要加/cygdrive
path = /cygdrive/c/Users/Administrator/BakData/data    # /cygdrive/c/     是windows下写法,此目录需要给用户授权
auth users = ync
secrets file = /cygdrive/c/config/rsyncd.passwd       # 在此目录下放置rsync的用户名/密码配置 格式为:ync:pwd
read only = no
list = no
transfer logging = yes

# use chroot = false
# strict modes = false
# hosts allow = *
# log file = rsyncd.log

# Module definitions
# Remember cygwin naming conventions : c:\work becomes /cygwin/c/work
#
# [test]
# path = /cygdrive/c/work
# read only = false
# transfer logging = yes

  1. 在上述配置的 c/config/rsyncd.passwd(C:盘,config目录下,新建rsyncd.passwd文件)格式为 用户名:密码
ync:yncpwd
  1. 上述步骤完成之后,启动服务
    在这里插入图片描述
  2. 检查端口
    netstat -ano

9.安装cwRsync客户端
https://www.jb51.net/softs/55934.html

  1. 客户端同步服务端数据
    rsync.exe -avz --include 2021-03-08 --port=35132 --progress ync@192.168.1.12::data_backup /cygdrive/d/databackup/ --password-file=/cygdrive/c/config/rsync.passwd
    (rsync.passwd: 只需要保存密码)

11.windows 定时任务
windows执行脚本 exec.cmd 内容:

TASKKILL /F /FI "IMAGENAME eq rsync.exe"
cd "C:\Program Files (x86)\cwRsync\bin"
start rsync.exe -avz --port=35132 --progress zmrsync@192.168.1.123::data_backup /cygdrive/d/databackup/ --password-file=/cygdrive/c/rsync/rsync.passwd"

python调用脚本:

import datetime
import os

import pandas as pd


def exec_rsync_backup(menu_path_1, menu_path_2):
    current_day = datetime.datetime.now().strftime('%Y-%m-%d')
    dataframe_1 = pd.read_csv(menu_path_1, encoding="gbk")
    dataframe_2 = pd.read_csv(menu_path_2, encoding="gbk")

    final_date_1 = ""
    data_1_size = 0
    final_date_2 = ""
    data_2_size = 0
    for row in dataframe_1.itertuples():
        date_1 = row.time_dir
        size_1 = row.dir_size
        if date_1 == current_day:
            final_date_1 = date_1
            data_1_size = size_1

    for row in dataframe_2.itertuples():
        date_2 = row.time_dir
        size_2 = row.dir_size
        if date_2 == current_day:
            final_date_2 = date_2
            data_2_size = size_2

    if final_date_1 == "" and final_date_2 == "":
        print("current date no data!")
        return

    if current_day == final_date_1 and current_day == final_date_2:
        win_rsync_cmd_1 = '"C:/Program Files (x86)/cwRsync/bin/rsync.exe" -avz --include ' + current_day + ' --exclude=/* --port=35132 ' \
                        '--progress ync@192.168.1.222::data_backup /cygdrive/d/databackup/' \
                        ' --password-file=/cygdrive/c/rsync/rsync.passwd'

        win_rsync_cmd_2 = '"C:/Program Files (x86)/cwRsync/bin/rsync.exe" -avz --include ' + current_day + ' --exclude=/* --port=35132 ' \
                        '--progress ync@192.168.1.223::data_backup /cygdrive/d/databackup/' \
                        ' --password-file=/cygdrive/c/rsync/rsync.passwd'
        if data_1_size >= data_2_size:
            result = os.popen(win_rsync_cmd_1)
            output = result.read()
            print(output.encode("gbk"), "hhh1")
        else:
            result = os.popen(win_rsync_cmd_2)
            output = result.read()
            print(output.encode("gbk"), "hhh2")
    else:
        return


if __name__ == '__main__':
    menu_file_path_1 = r'menu.csv'
    menu_file_path_2 = r'menu_1.csv'
    exec_rsync_backup(menu_file_path_1, menu_file_path_2)
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐