BT6*_*643 42 bash shell rsync simultaneous ubuntu-12.04
我们需要尽快将15TB数据从一台服务器传输到另一台服务器.我们目前正在使用,但当我们的网络能够(经过测试)时,我们rsync只能获得速度.我已经完成了对磁盘,网络等的测试,并认为只是rsync一次只传输一个文件,导致速度减慢.150Mb/s900+Mb/siperf
我找到了一个脚本来为目录树中的每个文件夹运行不同的rsync(允许你限制为x号),但我无法使它工作,它仍然只是一次运行一个rsync.
我找到了script 这里(复制如下).
我们的目录树是这样的:
/main
- /files
- /1
- 343
- 123.wav
- 76.wav
- 772
- 122.wav
- 55
- 555.wav
- 324.wav
- 1209.wav
- 43
- 999.wav
- 111.wav
- 222.wav
- /2
- 346
- 9993.wav
- 4242
- 827.wav
- /3
- 2545
- 76.wav
- 199.wav
- 183.wav
- 23
- 33.wav
- 876.wav
- 4256
- 998.wav
- 1665.wav
- 332.wav
- 112.wav
- 5584.wav
Run Code Online (Sandbox Code Playgroud)
所以我想要发生的是为/ main/files中的每个目录创建一个rsync,一次最多为5个目录.所以在这种情况下,3个rsyncs将运行,为/main/files/1,/main/files/2和/main/files/3.
我试着这样,但它只是为/main/files/2文件夹一次运行1个rsync :
#!/bin/bash
# Define source, target, maxdepth and cd to source
source="/main/files"
target="/main/filesTest"
depth=1
cd "${source}"
# Set the maximum number of concurrent rsync threads
maxthreads=5
# How long to wait before checking the number of rsync threads again
sleeptime=5
# Find all folders in the source directory within the maxdepth level
find . -maxdepth ${depth} -type d | while read dir
do
# Make sure to ignore the parent folder
if [ `echo "${dir}" | awk -F'/' '{print NF}'` -gt ${depth} ]
then
# Strip leading dot slash
subfolder=$(echo "${dir}" | sed 's@^\./@@g')
if [ ! -d "${target}/${subfolder}" ]
then
# Create destination folder and set ownership and permissions to match source
mkdir -p "${target}/${subfolder}"
chown --reference="${source}/${subfolder}" "${target}/${subfolder}"
chmod --reference="${source}/${subfolder}" "${target}/${subfolder}"
fi
# Make sure the number of rsync threads running is below the threshold
while [ `ps -ef | grep -c [r]sync` -gt ${maxthreads} ]
do
echo "Sleeping ${sleeptime} seconds"
sleep ${sleeptime}
done
# Run rsync in background for the current subfolder and move one to the next one
nohup rsync -a "${source}/${subfolder}/" "${target}/${subfolder}/" </dev/null >/dev/null 2>&1 &
fi
done
# Find all files above the maxdepth level and rsync them as well
find . -maxdepth ${depth} -type f -print0 | rsync -a --files-from=- --from0 ./ "${target}/"
Run Code Online (Sandbox Code Playgroud)
Man*_*anu 36
这似乎更简单:
ls /srv/mail | parallel -v -j8 rsync -raz --progress {} myserver.com:/srv/mail/{}
Run Code Online (Sandbox Code Playgroud)
Stu*_*aie 28
rsync尽可能快地通过网络传输文件.例如,尝试使用它来复制目标上根本不存在的一个大文件.该速度是rsync可以传输数据的最大速度.将其与scp(例如)的速度进行比较.rsync当目标文件存在时,原始传输速度甚至更慢,因为双方必须进行双向聊天,以了解文件的哪些部分已更改,但通过识别不需要传输的数据来收回成本.
一种更简单的rsync并行运行方式就是使用parallel.下面的命令将rsync并行运行最多5 秒,每个复制一个目录.请注意,瓶颈可能不是您的网络,但CPU和磁盘的速度以及并行运行只会使它们变慢,而不是更快.
run_rsync() {
# e.g. copies /main/files/blah to /main/filesTest/blah
rsync -av "$1" "/main/filesTest/${1#/main/files/}"
}
export -f run_rsync
parallel -j5 run_rsync ::: /main/files/*
Run Code Online (Sandbox Code Playgroud)
Nic*_*lay 15
您可以使用xargs哪种支持一次运行多个进程.对于您的情况,它将是:
ls -1 /main/files | xargs -I {} -P 5 -n 1 rsync -avh /main/files/{} /main/filesTest/
Run Code Online (Sandbox Code Playgroud)
F. *_*uri 14
ssh!如果您将一台服务器本地复制到另一台服务器,则无需在传输过程中加密数据!
\n默认情况下,rsync 用于ssh通过网络传输数据。为了避免这种情况,您必须创建一个rsync server目标主机。您可以通过以下方式准时运行守护进程:
rsync --daemon --no-detach --config filename.conf\nRun Code Online (Sandbox Code Playgroud)\n其中最小配置文件可能如下所示:(请参阅man rsyncd.conf)
\n\n\n
filename.confRun Code Online (Sandbox Code Playgroud)\nport = 12345\n[data]\n path = /some/path\n use chroot = false\n
然后
\nrsync -ax rsync://remotehost:12345/data/. /path/to/target/.\nrsync -ax /path/to/source/. rsync://remotehost:12345/data/.\nRun Code Online (Sandbox Code Playgroud)\nrsyncd.conf限制连接的最小值。关于jeremyjjbrown 关于安全性的评论,这里是使用专用网络接口的最小配置示例:
\n\n主要公共服务器:
\neth0: 1.2.3.4/0 Public address Main\neth1: 192.168.123.45/30 Backup network\nRun Code Online (Sandbox Code Playgroud)\n30 位网络只能容纳两台主机。
\n\n\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x94\x8f\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xaf\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xaf\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xaf\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xaf\xe2\x94\x81\xe2\x94\x81\xe2\x94\x93\n\xe2\x94\x83 Netw. base \xe2\x94\x82192.168.123.44 \xe2\x94\x82 #0\xe2\x94\x8211000000 10101000 01111011 001011\xe2\x94\x8200\xe2\x94\x83\n\xe2\x94\x83 Mask \xe2\x94\x82255.255.255.252\xe2\x94\x82/30 \xe2\x94\x8211111111 11111111 11111111 111111\xe2\x94\x8200\xe2\x94\x83\n\xe2\x94\x83 Broadcast \xe2\x94\x82192.168.123.47 \xe2\x94\x82 #3\xe2\x94\x8211000000 10101000 01111011 001011\xe2\x94\x8211\xe2\x94\x83\n\xe2\x94\x83 Host/net \xe2\x94\x822 \xe2\x94\x82Class C\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x83\n\xe2\x94\xa0\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa8\n\xe2\x94\x83\xe2\x96\xb8First host \xe2\x94\x82192.168.123.45 \xe2\x94\x82 #1\xe2\x94\x8211000000 10101000 01111011 001011\xe2\x94\x8201\xe2\x94\x83\n\xe2\x94\x83 Last host \xe2\x94\x82192.168.123.46 \xe2\x94\x82 #2\xe2\x94\x8211000000 10101000 01111011 001011\xe2\x94\x8210\xe2\x94\x83\n\xe2\x94\x97\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xb7\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xb7\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xb7\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\x81\xe2\x94\xb7\xe2\x94\x81\xe2\x94\x81\xe2\x94\x9b\n
备份服务器:
\neth0: 1.2.3.5/0 Public address Backup\neth1: 192.168.123.46/30 Backup network\n\ncat >/etc/rsyncd.conf <<eof\naddress 192.168.123.46\n[main]\n path = /srv/backup/backup0\n comment = Backups\n read only = false\n uid = 0\n gid = 0\neof\nRun Code Online (Sandbox Code Playgroud)\n因此,rsync 将仅侦听进入192.168.123.46第二个网络接口的连接。
然后rsync从主服务器运行
rsync -zaSD --zc zstd --delete --numeric-ids /mnt/. rsync://192.168.123.46/main/.\nRun Code Online (Sandbox Code Playgroud)\n当然,在防火墙中添加一些规则可能并非完全无用。
\niptables -I INPUT -i eth0 -p tcp --dport 873 -j DROP\nRun Code Online (Sandbox Code Playgroud)\nzstd进行高速压缩Zstandard的速度比普通. 因此,使用这种更新的压缩算法将显着改善您的传输! gzip
rsync -axz --zc=zstd rsync://remotehost:12345/data/. /path/to/target/.\nrsync -axz --zc=zstd /path/to/source/. rsync://remotehost:12345/data/.\nRun Code Online (Sandbox Code Playgroud)\n也许有一些--exclude指令(参见这个答案的底部!)。
rsync以减少由于 浏览时间而导致的不活动因为这种优化是关于磁盘访问和文件系统结构的。CPU数量没有什么可看的!因此,即使您的主机使用单核 CPU,这也可以改善传输。如果您计划使用任何并行器工具,则必须告诉他不要考虑物理 CPU 的数量。
\n由于目标是确保最大数据使用带宽,而其他任务浏览文件系统,因此最合适的并发进程数量取决于存在的小文件数量。
\nwait -n -p PID:最近的 bash 添加了一个内置-p PID功能wait。此类工作的必备条件:
#!/bin/bash\n\nmaxProc=3\nsource=\'\'\ndestination=\'rsync://remotehost:12345/data/\'\n\ndeclare -ai start elap results order\nwait4oneTask() {\n local _i\n wait -np epid\n results[epid]=$?\n elap[epid]=" ${EPOCHREALTIME/.} - ${start[epid]} "\n unset "running[$epid]"\n while [ -v elap[${order[0]}] ];do\n _i=${order[0]}\n printf " - %(%a %d %T)T.%06.0f %-36s %4d %12d\\n" "${start[_i]:0:-6}" \\\n "${start[_i]: -6}" "${paths[_i]}" "${results[_i]}" "${elap[_i]}"\n order=(${order[@]:1})\n done\n}\nprintf " %-22s %-36s %4s %12s\\n" Started Path Rslt \'microseconds\'\nfor path; do\n rsync -axz --zc zstd "$source$path/." "$destination$path/." &\n lpid=$!\n paths[lpid]="$path" \n start[lpid]=${EPOCHREALTIME/.}\n running[lpid]=\'\'\n order+=($lpid)\n ((${#running[@]}>=maxProc)) && wait4oneTask\ndone\nwhile ((${#running[@]})); do\n wait4oneTask\ndone\nRun Code Online (Sandbox Code Playgroud)\n输出可能如下所示:
\nmyRsyncP.sh files/*/*\n Started Path Rslt microseconds\n - Fri 03 09:20:44.673637 files/1/343 0 1186903\n - Fri 03 09:20:44.673914 files/1/43 0 2276767\n - Fri 03 09:20:44.674147 files/1/55 0 2172830\n - Fri 03 09:20:45.861041 files/1/772 0 1279463\n - Fri 03 09:20:46.847241 files/2/346 0 2363101\n - Fri 03 09:20:46.951192 files/2/4242 0 2180573\n - Fri 03 09:20:47.140953 files/3/23 0 1789049\n - Fri 03 09:20:48.930306 files/3/2545 0 3259273\n - Fri 03 09:20:49.132076 files/3/4256 0 2263019\nRun Code Online (Sandbox Code Playgroud)\n快速检查:
\nprintf "%\'d\\n" $(( 49132076 + 2263019 - 44673637)) \\\n $((1186903+2276767+2172830+1279463+2363101+2180573+1789049+3259273+2263019))\n6\xe2\x80\x99721\xe2\x80\x99458\n18\xe2\x80\x99770\xe2\x80\x99978\nRun Code Online (Sandbox Code Playgroud)\n在最多三个子进程下处理 18,77 秒需要 6,72 秒。
\n注意:您可以使用musec2str来改进输出,方法是将第一行长printf行替换为:
musec2str -v elapsed "${elap[i]}"\n printf " - %(%a %d %T)T.%06.0f %-36s %4d %12s\\n" "${start[i]:0:-6}" \\\n "${start[i]: -6}" "${paths[i]}" "${results[i]}" "$elapsed"\nRun Code Online (Sandbox Code Playgroud)\nrsync --daemon --no-detach --config filename.conf\nRun Code Online (Sandbox Code Playgroud)\n更多:您可以通过在此脚本中进行一些编辑来添加总体统计行:
\nport = 12345\n[data]\n path = /some/path\n use chroot = false\nRun Code Online (Sandbox Code Playgroud)\n可以产生:
\nrsync -ax rsync://remotehost:12345/data/. /path/to/target/.\nrsync -ax /path/to/source/. rsync://remotehost:12345/data/.\nRun Code Online (Sandbox Code Playgroud)\nrsync测试此脚本时为假注意:为了测试这一点,我使用了一个假的 rsync:
\n## Fake rsync wait 1.0 - 2.99 seconds and return 0-255 ~ 1x/10\nrsync() { sleep $((RANDOM%2+1)).$RANDOM;exit $(( RANDOM%10==3?RANDOM%128:0));}\nexport -f rsync\nRun Code Online (Sandbox Code Playgroud)\n您可能需要花一些时间来充分配置避免同步无用数据的方式!
\n在手册页中搜索exclude和/或include:
\n\nRun Code Online (Sandbox Code Playgroud)\n--cvs-exclude, -C auto-ignore files in the same way CVS does\n --exclude=PATTERN exclude files matching PATTERN\n --exclude-from=FILE read exclude patterns from FILE\n --include=PATTERN don\'t exclude files matching PATTERN\n --include-from=FILE read include patterns from FILE\n
为了保存用户目录,我经常使用:
\nrsync -axz --delete --zc zstd --exclude .cache --exclude cache source/. target/.\nRun Code Online (Sandbox Code Playgroud)\n仔细阅读FILTER RULES手册页中的部分:
man -P\'less +/^FILTER\\ RULES\' rsync\nRun Code Online (Sandbox Code Playgroud)\n安静地阅读手册页!man rsync 和 man rsyncd.conf!!
dan*_*rba 13
您是否尝试过使用rclone.org?
有了rclone你可以做类似
rclone copy "${source}/${subfolder}/" "${target}/${subfolder}/" --progress --multi-thread-streams=N
Run Code Online (Sandbox Code Playgroud)
其中--multi-thread-streams=N表示您希望生成的线程数。
网上列出了许多替代工具和方法.例如:
我发现的最简单的方法是在 shell 中使用后台作业:
for d in /main/files/*; do
rsync -a "$d" remote:/main/files/ &
done
Run Code Online (Sandbox Code Playgroud)
请注意,它不会限制工作数量!如果您受到网络限制,这并不是真正的问题,但如果您正在等待旋转生锈,这将导致磁盘崩溃。
你可以添加
while [ $(jobs | wc -l | xargs) -gt 10 ]; do sleep 1; done
Run Code Online (Sandbox Code Playgroud)
在循环内进行作业控制的原始形式。
我开发了一个名为:parallel_sync 的 python 包
https://pythonhosted.org/parallel_sync/pages/examples.html
这是一个如何使用它的示例代码:
from parallel_sync import rsync
creds = {'user': 'myusername', 'key':'~/.ssh/id_rsa', 'host':'192.168.16.31'}
rsync.upload('/tmp/local_dir', '/tmp/remote_dir', creds=creds)
Run Code Online (Sandbox Code Playgroud)
默认并行度为 10;你可以增加它:
from parallel_sync import rsync
creds = {'user': 'myusername', 'key':'~/.ssh/id_rsa', 'host':'192.168.16.31'}
rsync.upload('/tmp/local_dir', '/tmp/remote_dir', creds=creds, parallelism=20)
Run Code Online (Sandbox Code Playgroud)
但是请注意,ssh 通常默认将 MaxSessions 设置为 10,因此要将其增加到 10 以上,您必须修改 ssh 设置。
| 归档时间: |
|
| 查看次数: |
121615 次 |
| 最近记录: |