Ubuntu use Rsync+inotify-tools to implement one of the automatic file backups

Source: Internet
Author: User
Tags chmod file handling inotify cpu usage rsync

Now there is a need, a server under a path to the file, want to back up to the B server below. Suppose a IP is 192.168.1.75,b IP is 192.168.1.85. Under Ubuntu you can use the Rsync software to work with the INotify tool to implement this feature.

1 rsync host-side configuration

The ubuntu16.03 system already has the Rsync software installed by default. You need to configure rsync software. A is a data source that needs to be configured as an rsync machine when using Inotify-tools. Configure B to be an rsync host. The rsyncd.conf configuration file is established under the/etc path of B, and the contents of the file are as follows:

# sample rsyncd.conf configuration file # GLOBAL OPTIONS #motd FILE=/ETC/MOTD log file=/var/log/rsyncd # for PID file, D
o not use/var/run/rsync.pid If # and are going to run rsync out of the init.d script.
# The INIT.D script does its own PID file handling, # So omit the ' PID file ' line completely in. PID file=/var/run/rsyncd.pid syslog facility=daemon #socket options= # Module options [ftp]//ftp This module name to remember comment = Pub LIC Archive Path =/data//This is the sync path use chroot = no # max connections=10 lock file =/var/lock/rsyncd # the default fo 
R Read Only is yes ... read only = no//This place is to be configured no, otherwise to the/data path to write files will occur error List = yes uid = root gid = root # exclude = # Exclude from = # include = # include from = Auth users =lzj//lzj is the user rsyncd.secrets inside the secrets file =/etc/rsyncd.s Ecrets Strict modes = yes hosts allow =192.168.1.75//allow a host to access # hosts Deny = ignore errors = yes ignore Nonread Able = yes transfer logging = no # log format =%t:host%h (%a)%o%f (%L bytes).
	Total%b bytes. Timeout = refuse options = Checksum dry-run dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.TB Z

Then in the/ETC path to create a password file rsyncd.secrets, the content is:

Lzj:123

Then modify the/ect/default/rsync file:

# defaults file for rsync daemon mode # start rsync into daemon mode from INIT.D script? # only allowed values are ' true ', ' false ', and ' inetd ' # use ' inetd ' if you want ' to start ' rsyncd from inetd, # All This does was prevent the init.d script from printing a message # about not starting RSYNCD (for your still need to modify inet
d ' s config yourself).
Rsync_enable=true # which file should is used as the configuration file for rsync. 
# This file are used instead of the default/etc/rsyncd.conf # warning:this option has no effect if the daemon is accessed # using a remote shell.
 When the using a different file for # rsync and might want to symlink/etc/rsyncd.conf to # this file.
rsync_config_file=/etc/rsyncd.conf # What extra options to give Rsync--daemon? # that excludes the--daemon;
That ' s always do in the INIT.D script # possibilities are: #--address=123.45.67.89 (bind to a specific IP address) #--port=8730 (bind to specified port; Default 873) rsync_opts= ' # run RSYNCD at a nice level?  # The Rsync daemon can impact performance due to much I/O and CPU usage, # so/may want to run it at a nicer priority
than the default priority. # Allowed values are 0-19 inclusive;
is a reasonable value.
Rsync_nice= ' # run RSYNCD with Ionice?
# ' Ionice ' does for IO load what ' nice ' does for CPU load. # as Rsync is often used for backups which aren ' t all that time-critical, # reducing the rsync IO priority would benefit
The rest of the system.
# The manpage for Ionice for allowed options. #-C3 is recommended, this'll run rsync IO at "idle" priority.
Uncomment # The next line to activate this. # rsync_ionice= '-c3 ' # Don ' t forget to create a appropriate config file, # Else the daemon won't start.

Set permissions for synchronization path/data:

Chown-r Root:root/data

The root:root are the UID and GID set by rsyncd.conf.

Now that the B-terminal configuration is complete, run the following command to start the Rsync service:

/etc/init.d/rsync start

To see if the Rsync service starts:

Ps-e|grep rsync

9677? 00:00:00 rsync

2 A-terminal configuration

Set up the Rsync key file/etc/rsync.pass on a side, as follows:

123

Note that the password is all you need to write.

Then modify the permissions for the secret key file:

chmod 600/etc/rsync.pass

Establish a Sync folder and authorize the folder:

Mkdir/data

Chown-r Root:root/data

Install Inotify-tools on a end:

sudo apt-get install Inotify-tools

Set up shell script/sync.sh on a side, as follows:

Src=/data
dst=lzj@192.168.1.85::ftp/usr/bin/inotifywait-mr--timefmt '%d/%m/%y%h:%m '--format '%T '
 
- E Modify,delete,create,attrib $src | While read the
	rsync-vzrtopg--delete  --progress--password-file=/etc/rsync.pass $src $dst	
        Done
Exit 0

Authorize and run/sync.sh:

chmod +x/sync.sh

./sync.sh

Observe a new file or delete file in the/data folder on the end of a, and the/data directory on end B has a file change.


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.