One of the automatic file backups using rsync+inotify-tools under ubuntu

    Now there is a requirement. The files in a certain path of server A want to be backed up to server B. Suppose A's ip is 192.168.1.75 and B's ip is 192.168.1.85. Under ubuntu, you can use the rsync software with the inotify tool to achieve this function.

1 rsync host configuration

    The ubuntu16.03 system has already installed the rsync software by default. Need to configure rsync software. A is the data source. When using inotify-tools, you need to configure A as an rsync slave. Configure B as the rsync host. Create the rsyncd.conf configuration file in the /etc path of B. The content of the file is as follows:

# sample rsyncd.conf configuration file

# GLOBAL OPTIONS

#motd file=/etc/motd
log file=/var/log/rsyncd
# for pid file, do not use /var/run/rsync.pid if
# you 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 that case.
pid file=/var/run/rsyncd.pid
syslog facility=daemon
#socket options=

# MODULE OPTIONS

[ftp]//The module name of ftp should be remembered

	comment = public archive
	path = /data //this is the sync path
	use chroot = no
#	max connections=10
	lock file = /var/lock/rsyncd
# the default for read only is yes...
	read only = no //This place should be configured as no, otherwise an error will occur when writing files to the /data path
	list = yes
	uid = root
	gid = root
#	exclude =
#	exclude from =
#	include =
#	include from =
	auth users =lzj //lzj is the user set in rsyncd.secrets
	secrets file = /etc/rsyncd.secrets
	strict modes = yes
	hosts allow =192.168.1.75 //Allow host A to access    
#	hosts deny =
	ignore errors = yes
	ignore nonreadable = yes
	transfer logging = no
#	log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
	timeout = 600
	refuse options = checksum dry-run
        dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

Then create a password file rsyncd.secrets in the /etc path with the following contents:

lzj: 123

Then modify the /ect/default/rsync file:

# defaults file for rsync daemon mode

# start rsync in daemon mode from init.d script?
#  only allowed values are "true", "false", and "inetd"
#  Use "inetd" if you want to start the rsyncd from inetd,
#  all this does is prevent the init.d script from printing a message
#  about not starting rsyncd (you still need to modify inetd's config yourself).
RSYNC_ENABLE=true

# which file should be used as the configuration file for rsync.
# This file is used instead of the default /etc/rsyncd.conf
# Warning: This option has no effect if the daemon is accessed
#          using a remote shell. When using a different file for
#          rsync you might want to symlink /etc/rsyncd.conf to
#          that file.
 RSYNC_CONFIG_FILE=/etc/rsyncd.conf

# what extra options to give rsync --daemon?
#  that excludes the --daemon; that's always done 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 you may want to run it at a nicer priority than the default priority.
#  Allowed values are 0 - 19 inclusive; 10 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 will benefit the rest of the system.
#  See the manpage for ionice for allowed options.
#  -c3 is recommended, this will run rsync IO at "idle" priority. Uncomment
#  the next line to activate this.
# RSYNC_IONICE='-c3'

# Don't forget to create an appropriate config file,
# else the daemon will not start.

Set permissions for sync path /data:

        chown -R root:root /data

The root: root is the uid and gid set by rsyncd.conf respectively.

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

    /etc/init.d/rsync start

Check whether the rsync service is started:

    ps -e|grep rsync

    9677 ?        00:00:00 rsync

2 A-side configuration

    Create the rsync key file /etc/rsync.pass on the A side, with the following contents:

123

Note that you only need to write the password.

Then modify the permissions of the key file:

    chmod 600 /etc/rsync.pass

Create a sync folder and authorize the folder:

    mkdir /data

  chown -R root:root /data

Install inotify-tools on the A side:

    sudo apt-get install inotify-tools

Create a shell script /sync.sh on the A side, the content is as follows:

src=/data
[email protected]::ftp
 
/usr/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files
        do
	    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 whether there are file changes in the /data directory on the B side when creating or deleting files in the /data folder on the A side.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325984562&siteId=291194637