ubuntu下使用rsync+inotify-tools实现文件自动备份之一

    现在有个需求,A服务器的某个路径下的文件,想备份到B服务器下面。假设A的ip是 192.168.1.75,B的ip是 192.168.1.85。在ubuntu下面可以使用rsync软件配合inotify工具一起来实现这个功能。

1 rsync主机端配置

    ubuntu16.03系统默认已经安装了rsync软件。需要配置rsync软件。A是数据源,在使用inotify-tools时需要将A配置成rsync从机。将B配置成rsync主机。在B的/etc路径下建立rsyncd.conf配置文件,文件内容如下:

# 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]//ftp这个模块名要记住

	comment = public archive
	path = /data    //这是同步路径
	use chroot = no
#	max connections=10
	lock file = /var/lock/rsyncd
# the default for read only is yes...
	read only = no    //这个地方要配置成no,不然往/data路径下写文件会发生错误
	list = yes
	uid = root
	gid = root
#	exclude = 
#	exclude from = 
#	include =
#	include from =
	auth users =lzj //lzj是rsyncd.secrets里面设置的用户
	secrets file = /etc/rsyncd.secrets
	strict modes = yes
	hosts allow =192.168.1.75    //允许A主机访问    
#	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

然后在/etc路径下建立密码文件rsyncd.secrets,内容为:

lzj:123

再修改/ect/default/rsync文件:

# 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.

设置同步路径/data的权限:

        chown -R root:root /data

其中的root:root分别是rsyncd.conf所设置的uid和gid。

现在B端配置完成,运行下列命令启动rsync服务:

    /etc/init.d/rsync start

查看rsync服务是否启动:

    ps -e|grep rsync

    9677 ?        00:00:00 rsync

2 A端配置

    在A端建立rsync秘钥文件/etc/rsync.pass,内容如下:

123

注意只要写密码就行了。

然后修改秘钥文件的权限:

    chmod 600 /etc/rsync.pass

建立同步文件夹,并对文件夹授权:

    mkdir /data

  chown -R root:root /data

在A端安装inotify-tools:

    sudo apt-get install inotify-tools

在A端建立shell脚本 /sync.sh,内容如下:

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

授权并运行 /sync.sh:

    chmod +x /sync.sh

    ./sync.sh

观察在A端的/data文件夹下新建文件或删除文件,B端的/data目录是否有文件变化。


猜你喜欢

转载自blog.csdn.net/liuzhijun301/article/details/80238634
今日推荐