简体   繁体   English

rsync-将文件复制到另一台服务器

[英]rsync - copy files to another server

I have more than 500 Mp4 files in my server 1 so i want half of them to send to server 2 and half of them to server 3 我的服务器1中有500多个Mp4文件,因此我希望其中一半发送到服务器2,另一半发送到服务器3

but i dont know how to make this 但是我不知道怎么做

Is there a way to select files by alphabet or maybe date or something else example videos that start with 有没有一种方法可以按字母,日期或其他开头的示例视频来选择文件

 a,c,e*.mp4  

will send to server 2 and videos that start with 将发送到服务器2和以以下内容开头的视频

 b,d,f*.mp4 

will send to server 3 将发送到服务器3

or is there any other way you think is better 还是您认为有其他更好的方法

rsync -avzP /home/user/public_html/domain.com/ ip:/home/user2/public_html/domain.com/ 

1) use find to make a list of all the files 1)使用find列出所有文件

find /opt/mymp3folder -print > /tmp/foo

2) find the count of lines and split the list in two 2)找到行数并将列表分成两部分

cd /tmp
wc -l /tmp/foo
387
split -l 200 /tmp/foo

3) split by default makes a set of files called xaa xab xac etc. So use xaa to copy to one server and xab to copy to the other 3)默认情况下,分割会生成一组名为xaa xab xac等的文件。因此,请使用xaa复制到一台服务器,使用xab复制到另一台服务器

rsync -av --files-from=/tmp/xaa . server1:/opt/newmp3folder/
rsync -av --files-from=/tmp/xab . server2:/opt/newmp3folder/

'.' '。' in the above is the "source" path and allows the use of relative paths in the "files-from" You either need to be in the same path that the find command is run from and use . 上面的是“源”路径,并允许在“ files-from”中使用相对路径。您要么需要与find命令从中运行并使用的路径相同。 or set it to an absolute value 或将其设置为绝对值

Obviously if you wanted to do this on a regular basis probably want to script it properly 显然,如果您想定期执行此操作,则可能需要正确编写脚本

1) use find to make a list of all the files 1)使用find列出所有文件

find /opt/mymp3folder -print > /tmp/foo

2) find the count of lines and split the list in two 2)找到行数并将列表分成两部分

wc -l /tmp/foo

387 split -l 200 /tmp/foo 387分割-l 200 / tmp / foo

mv xaa xaa.txt

and then rsync like this 然后像这样rsync

 rsync -avzP -e ssh `cat xaa.txt` root@0.0.0.0:/var/www/

I think that is better to split files by size than for numbers (I assume that you have several file sizes in your mp4). 我认为按文件大小分割要比对数字分割好(我假设mp4中有多个文件大小)。

#!/bin/bash
FOLDER=$1
TMP_FILE=$(mktemp)

find  $FOLDER -type f -exec stat -c "%s;%n" {} \; | sort -t ';' -k 2 | awk 'BEGIN{ sum=0; FS=";"} { sum += $1; print sum";"$1";"$2 }' > $TMP_FILE
TOTAL_SIZE=$(tail -n 1 $TMP_FILE | cut -f 1 -d ';')
HALF_SIZE=$(echo $TOTAL_SIZE / 2 | bc)
echo $TOTAL_SIZE $HALF_SIZE


# split part
IFS=';'
while read A B C ; do
    [ $A -lt $HALF_SIZE ] && echo "$C" >> lst_files_1.txt || echo "$C" >> lst_files_2.txt
done  < $TMP_FILE
rsync -avzP 
rm $TMP_FILE

After execution you have list_files_1.txt and list_files_2.txt that contains half of files depending of size. 执行后,您将拥有list_files_1.txtlist_files_2.txt ,其中一半包含文件,具体取决于大小。

You can send this files to each server using rsync: 您可以使用rsync将此文件发送到每个服务器:

rsync -avzP $(cat list_files_1.txt) ip:/home/user2/public_html/domain.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM