Crontab +RSYCNC Single-Process low-latency file synchronization

Source: Internet
Author: User
Tags inotify rsync

1. Business Requirements: Files uploaded by the website need to be synchronized across the country's servers.
2. Implementation methods
1). Inotify+rsync. INotify Monitor file changes. Then call rsync to synchronize the changes to the directory. Advantages: Synchronization of file changes, reduce the number of file synchronization, because each synchronization needs to compare the server file list, so this method can reduce the bandwidth consumption. Cons: In a scenario where both servers are frequently uploaded. Uploading multiple files in the same directory will result in more repetitive calls and greatly reduce the efficiency. Of course, you can create a synchronization directory queue. Filter duplicate sync directories to address this disadvantage
2). Crontab+rsync. Timed synchronization. Advantages: Simple implementation, a few lines of code can be achieved, suitable for more file changes in the scene. Cons: Regardless of whether the file has changed or not has been synchronized. Will waste some network bandwidth. Need to avoid concurrency. Synchronization of files is repeated concurrently.

3. Business implementation
1) rsync.sh synchronization script. The script implements checking for a synchronization process every 10 seconds. If not, start the synchronization process, or wait for the synchronization process to be in progress.

#!/bin/bash
#rsync All
#每10秒检查一次, if there is no synchronization process, start the synchronization
Idx=1
While [$idx-le 6];d o
echo "Idx= $idx"
Date
#判断是否有正在同步的进程.
Pid=ps -ef |grep rsync|grep rsyncd.pw|grep imgrsync|awk ‘{print $2}‘

if [ "$pid" ];then    echo "rsync is running,pid:$pid"    sleep 10s     idx=$((idx+1))else    rsync -avzrt --delete  --exclude-from=/opt/rsync/exclude-list.txt --password-file=/etc/rsyncd.pw /path-to-rsync/ rsync://[email protected]{host}:{port}/images    exit 1fi

Done

2)crontab  crontab 保证每1分钟启动一次rsync.sh    `*/1 * * * * /opt/rsync/rsync.sh>/var/log/rsync/rsync-all.log`

Crontab +RSYCNC Single-Process low-latency file synchronization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.