Linux rsync simple application & rsync + inotify real-time application

Source: Internet
Author: User
Tags inotify rsync

System Centos6.0

Server :( Centos) 192.168.182.130

Client: lv2) 192.168.182.129

Rsync is a data image backup tool in unix-like systems. It can be seen from the software name-remote sync.

Its features are as follows: images can be used to save the entire directory tree and file system. It is easy to maintain the permissions, time, and soft links of the original file. Installation without special permissions. Fast: During the first synchronization, rsync copies all the content, but transfers only modified files in the next synchronization. Rsync can compress and decompress data during data transmission, so it can use less bandwidth. Security: You can use scp, ssh, and other methods to transmit files. Of course, you can also use a direct socket connection.

Supports anonymous transmission for website images.

Start usr/bin/rsync -- daemon -- config =/etc/rsyncd on the server. conf may require the root permission to run. /etc/rsyncd. conf is the rsyncd you just edited. conf location. you can also go to/etc/rc. d/rc. add the local file to enable the system to start automatically. client Synchronization

Rsync-parameter username @ sync Server IP: Module name in the square brackets in rsyncd. conf) Local Storage path for the content is as follows:

Rsync-vzrtopg-delete -- progress -- exclude "*. log" lansgg@192.168.182.130: lansggtest/root/backup -- password-file =/etc/rsyncd. pass

The command is interpreted as follows:
-V indicates that verbose displays the transmission details.
-Z: compress: compresses the file during transmission.
-R:-recursive: recursive processing of sub-Directories
-T indicates that-times stores the time information of the file.
-O is the-owner used to keep the owner information of the file.
-P is -- perms to keep the File Permission
-G indicates that-group maintains the file group information.

-A refers to the -- archive mode, indicating that the file is transmitted recursively and all file attributes are kept, equal to-rlptgoD.

-- Delete maintains data synchronization based on the rsync server. If the server deletes a file, the client also deletes the file.
-- Progress shows the detailed progress.
-- Exclude is used to exclude unnecessary file types.
Lansgg@192.168.182.130: lansggtest specifies the backup Server and related modules, lansgg for the Server above the rsync synchronization account set;
/Backup specifies the storage location of the backup files on the client.
-- Password-file =/etc/rsyncd. pass specifies the location where the client stores the password file.

Rsync simple application;

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/01120L047-0.jpg "title =" 1.jpg"/>

1. Install rsync on the Server;

[root@Centos ~]# yum install -y rsync

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/01120KH3-1.jpg "title =" 1.jpg"/>

Create a synchronization test directory and create a synchronization user;

[root@Centos ~]# mkdir /root/test[root@Centos ~]# vim /etc/rsyncd.passlansgg:lansgg123

Change Password File Permissions

[root@Centos ~]# chmod 600 /etc/rsyncd.pass

When you run the rpm-ql package, you will find that there are no configuration files. below is the configuration file you have written yourself;

[Root @ Centos ~] # Vim/etc/rsyncd. confuid = root # This option specifies the uid that the daemon should have when the module transfers files. The default value is "nobody ", gid = root # This option specifies the gid that the daemon should have when the module transfers files. The default value is "nobody ". Use chroot = no # Do not use chrootmax connections = 0 # set the maximum number of connections. The default value is 0, that is, no limit. You can set it at will. You can set strict mode = yes on both 10 and 20 # Set whether to check the permission of the password file pid file =/var/run/rsyncd. pid # specify the location of the pid file of the rysnc process lock file =/var/lock/rsync. lock # specify the lock file log file that supports max connections =/var/log/rsyncd. log # specify the rsync log output path [lansggtest] # Module name, you can use any name path =/root/test/# To synchronize the source directory path comment = lansgg test # module description ignore errors # When an I/O error occurs, ignore it. Read only = no # whether it is read-only. If it is set to no, the client can upload files. Write only = no # The client can download files when it is set to no. Hosts allow = 192.168.182.129 # allow access to the host hosts deny = * # reject access to the host, * indicates all hosts. List = false # Set whether to list this module during client requests. false indicates hiding. Auth users = lansgg # Set the user used for connection, that is, the user name defined in the password file. If this line does not exist, it indicates that it is an anonymous secrets file =/etc/rsyncd. pass # specifies the location of the password file

Start rsync

[root@Centos ~]# rsync --daemon

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/01120J456-2.jpg "title =" 1.jpg"/>

Create a test file in the test directory

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/01120L549-3.jpg "title =" 1.jpg"/>

2. Client operations;

Install rsync

[root@lv2 ~]# yum install rsync -y

Configure the password file, which is written with the same password as the Server, but no account name is required.

[root@lv2 ~]# vim /etc/rysncd.passlansgg123

Mkdir/root/backup # create a local backup directory

Start synchronization;

rsync -vzrtopg --delete --progress --exclude "*.log" lansgg@192.168.182.130::lansggtest /root/backup --password-file=/etc/rsyncd.pass

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/01120G001-4.jpg "title =" 1.jpg"/>

No log file;

Ps: You can write rsync -- daemon to the rc. local file,

You can write the preceding command to cron to implement timed synchronization;

The following describes how to synchronize data in real time using rsync + inotify;

As shown above, rsync cannot implement real-time synchronization; therefore, cron is implemented; this is also a deficiency of rsync;

Inotify is a Linux feature that monitors file system operations, such as reading, writing, and creating. Inotify is responsive, easy to use, and much more efficient than the busy polling of cron tasks. Inotify is a Linux kernel feature that monitors the file system and sends related event warnings to specialized applications in a timely manner, such as delete, read, write, and uninstall operations. You can also track the source and target details of an activity.

To use inotify, you must have a Linux machine with 2.6.13 or a newer kernel. Earlier Linux kernel versions use the lower-level File Monitor dnotify ). If you do not know the kernel version, go to shell and enter uname-:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/01120M206-5.jpg "title =" 1.jpg"/>

This is the diagram that rsync can send configuration files for multiple machines. In fact, the above example is the opposite: the client downloads the server for backup, in this case, the client uploads data to multiple servers, such as clusters;

On the Server side) Configure as above;

Uid = root # This option specifies the uid that the daemon should have when the module transfers files. The default value is "nobody ", gid = root # This option specifies the gid that the daemon should have when the module transfers files. The default value is "nobody ". Use chroot = no # Do not use chrootmax connections = 0 # set the maximum number of connections. The default value is 0, that is, no limit. You can set it at will. You can set strict mode = yes on both 10 and 20 # Set whether to check the permission of the password file pid file =/var/run/rsyncd. pid # specify the location of the pid file of the rysnc process lock file =/var/lock/rsync. lock # specify the lock file log file that supports max connections =/var/log/rsyncd. log # specify the rsync log output path [lansggtest] # Module name, you can use any name path =/root/test/# To synchronize the source directory path comment = lansgg test # module description ignore errors # When an I/O error occurs, ignore it. Read only = no # whether it is read-only. If it is set to no, the client can upload files. Write only = no # The client can download files when it is set to no. Hosts allow = 192.168.182.129 # allow access to the host hosts deny = * # reject access to the host, * indicates all hosts. List = false # Set whether to list this module during client requests. false indicates hiding. Auth users = lansgg # Set the user used for connection, that is, the user name defined in the password file. If this line does not exist, it indicates that it is an anonymous secrets file =/etc/rsyncd. pass # specifies the location of the password file

Next, install inotify-tools on the Client.

[root@Centos ~]# yum install inotify-tools -y
rpm -ql inotify-tools/usr/bin/inotifywait/usr/bin/inotifywatch

Inotifywait is used to wait for a specific event in a file or file set. It can monitor any file or directory settings and recursively monitor the entire directory tree;

Inotifywatch is used to collect statistics on the monitored file system, including the number of times each inotify event occurs.

mkdir /root/backup vim /etc/rsyncd.passlansgg123

Write a Real-Time Detection synchronization script;

vi  dataup.sh#!/bin/bashserver1=192.168.182.130server2=192.168.182.131server3=192.168.182.132src=/root/backupdstmode=mode1user=lansgg/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src| while read filesdorsync -vzrtopg --delete --progress  --password-file=/etc/rsyncd.pass  $src $user@$server1::$dstmodersync -vzrtopg --delete --progress  --password-file=/etc/rsyncd.pass  $src $user@$server2::$dstmodersync -vzrtopg --delete --progress  --password-file=/etc/rsyncd.pass  $src $user@$server3::$dstmodeecho "$files was rsynced" >> /tmp/rsync.log 2>&1done

Inotifywait parameters:
-M (-monitor) indicates that the event listening state is always maintained;
-R (-recursive) indicates the recursive query directory.
-Q:-quiet. The monitoring event is printed.
-- Timefmt: output format of the specified time
-- Format: Specifies the detailed information of the variable file.

Sh inotify. sh &

Echo "sh inotify. sh &">/etc/rc. local

You can now create and delete files on the client for testing;

Client execution:

[root@node1 ~]# ./dataup.sh &[1] 10461[root@node1 ~]# tail -f /tmp/rsync.log

When you run mkdir/root/backup/testtesttest on the Client to create a test directory)

You will find the Client log:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/01120JZ2-6.jpg "title =" 3.jpg"/>

Check the server;

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/01120KV6-7.jpg "style =" float: none; "title =" 1.jpg"/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/01120HL3-8.jpg "style =" float: none; "title =" 2.jpg"/>

All have been synchronized;

Ps: When executing search inotify, we found an incron. system INTRODUCTION: incron. i686: Inotify cron system; you can study it;


This article is from the Coffee _ Blue Mountains blog, please be sure to keep this source http://lansgg.blog.51cto.com/5675165/1227600

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.