连接断开后通过 SSH 恢复 rsync 吗?

53 linux ssh rsync command-line

我必须使用 rsync 通过 ssh 传输大量数据(> 80 GB)。一切正常,但发送备份数据的 DSL 连接将每 24 小时断开一次,最多 3 分钟(不能选择切换提供商)。

我如何能:

  1. 连接恢复后自动重新开始传输?

  2. 确保没有意外地同时运行两个 rsync 命令?

Pet*_*ter 61

以下内容应该会有所帮助:

#!/bin/bash

while [ 1 ]
do
    rsync -avz --timeout=60 --partial source dest
    if [ "$?" = "0" ] ; then
        echo "rsync completed normally"
        exit
    else
        echo "Rsync failure. Backing off and retrying..."
        sleep 180
    fi
done
Run Code Online (Sandbox Code Playgroud)

当连接终止时,rsync 会以非零退出代码退出。这个脚本只是不断地重新运行 rsync,让它继续运行,直到同步正常完成。

  • 啊,很高兴知道。bash 永远不会停止让我惊讶/恐惧 :-P (8认同)
  • == 是 = :D 的别名 (6认同)
  • 晚会晚了,但对于后代:A)只需使用:if rsync -avz --partial source dest; then ... B) 如果要比较整数值,如果使用双括号进行算术扩展: if (( $? = 0 )) then; (2认同)

Ker*_*ers 13

这与 Peter 的回答大致相同,但为用户提供了他想要的远程文件的选项,以及他想要保存它的位置(以及通过 ssh 进行 rsync)。分别用您的用户名和主机替换 USER 和 HOST。

#!/bin/bash
echo -e "请输入完整的(转义)文件路径:"
读取 -r 路径
回声“路径:$路径”
echo -e "请输入目的地:"
读-r dst
echo "目的地:$dst"
而 [ 1 ]
做
    rsync --progress --partial --append -vz -e ssh "USER@HOST:$path" $dst
    如果 [ "$?" =“0”]; 然后
        echo "rsync 正常完成"
        出口
    别的
        echo "rsync 失败。一分钟后重试..."
        睡觉 60
    菲
完毕

The rsync options used here enable the progress stats during transfer, the saving of partial files upon unexpected failure, and the ability to append to partially completed files upon resume. The -v option increases verbosity, the -z option enables compression (good for a slow connection, but requires more cpu power on both ends), and the -e option enables us to conduct this transfer over ssh (encryption is always good).

Note: Use this only if you have public key login enabled with your ssh, otherwise it will ask you for a password when it restarts (killing all functionality of the script).


小智 8

当您不想打开编辑器时,有一种快速而肮脏的方法:

while ( ! rsync -avzP <source> <dest> ); do sleep 1; done
Run Code Online (Sandbox Code Playgroud)
-a  archive mode
-v  increase verbosity
-z  compress file data during the transfer
-P  same as --partial --progress
Run Code Online (Sandbox Code Playgroud)

  • `直到 rsync &lt;...&gt;;睡60分钟;完成` (3认同)
  • 一种快速且“干净”的方式。其他答案的脚本做了完全相同的事情,但有不必要的冗长。 (2认同)

小智 5

supervisor daemon(一个进程控制管理器)在创建双方的rsa证书后可以很好地工作,类似的配置如下:(/etc/supervisor/supervisord.conf是基于debian的系统上的配置文件路径)

[program:rsync-remoteserver]
command=rsync -avz --progress root@server.com:/destination /backup-path
stdout_logfile=/out-log-path  
stderr_logfile=/errlogpath
Run Code Online (Sandbox Code Playgroud)