搭建rsync服务并同步重要数据
在主备机器上均安装rsync,在主机上以daemon的模式启动,在备机上定时执行同步命令。安装rsync的命令如下:
1、下载安装包(主备机均执行)
[root@localhost home]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.4.tar.gz --2017-09-30 02:11:14-- http://rsync.samba.org/ftp/rsync/src/rsync-3.0.4.tar.gz Resolving rsync.samba.org (rsync.samba.org)... 144.76.82.156, 2a01:4f8:192:486::443:2 Connecting to rsync.samba.org (rsync.samba.org)|144.76.82.156|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 773083 (755K) [application/gzip] Saving to: ‘rsync-3.0.4.tar.gz’ 100%[====================================================================================================================================================================================================================================>] 773,083 434KB/s in 1.7s 2017-09-30 02:11:18 (434 KB/s) - ‘rsync-3.0.4.tar.gz’ saved [773083/773083]
2、编译安装(主备机均执行)
[root@localhost home]# tar -xvzf rsync-3.0.4.tar.gz [root@localhost home]# cd rsync-3.0.4 [root@localhost rsync-3.0.4]# ./configure --prefix=/usr/local/rsync [root@localhost rsync-3.0.4]# make && make install
3、配置(主机执行)
[root@localhost rsync-3.0.4]# mkdir /usr/local/rsync/logs [root@localhost rsync-3.0.4]# mkdir /usr/local/rsync/etc [root@localhost rsync-3.0.4]# mkdir /usr/local/rsync/run [root@localhost rsync-3.0.4]# vim /usr/local/rsync/etc/rsyncd.conf [root@localhost rsync-3.0.4]# cat /usr/local/rsync/etc/rsyncd.conf uid = root gid = root port = 873 use chroot = no hosts allow = 10.0.251.159 #允许连接的主机IP,在我们的环境中,该IP为备机IP #hosts deny = 192.168.10.0/24 pid file = /usr/local/rsync/run/rsyncd.pid lock file = /usr/local/rsync/run/rsync.lock log file = /usr/local/rsync/logs/rsyncd.log ignore errors
#配置待同步的目录,以及同步时所需的认证信息 [mysql_dump] path = /home/data/mysqldumpdata/ auth users = root secrets file = /usr/local/rsync/rsyncd.pass read only = no [fdfs_data] path = /home/log_data/fdfs auth users = root secrets file = /usr/local/rsync/rsyncd.pass read only = no [root@localhost rsync-3.0.4]# vim /usr/local/rsync/rsyncd.pass root:密码 [root@localhost rsync-3.0.4]# chmod 600 /usr/local/rsync/rsyncd.pass
主机上以daemon的形式启动rsync server,命令如下(主机执行):
[root@localhost rsync-3.0.4]# /usr/bin/rsync --daemon --config=/usr/local/rsync/etc/rsyncd.conf & [1] 108633
在备机上手动执行同步命令,测试是否能够将主机的数据同步到备机之上(备机执行):
[root@host-10-0-251-159 ltp_bak_data]# /usr/bin/rsync -avz --delete --password-file=/usr/local/rsync/rsyncd.pass root@192.168.32.3::mysql_dump /home/ltp_bak_data/mysql_dump/ receiving incremental file list ./ dump.sh scriptstranslation-2017-09-24.sql scriptstranslation-2017-09-25.sql scriptstranslation-2017-09-26.sql scriptstranslation-2017-09-27.sql scriptstranslation-2017-09-28.sql scriptstranslation-2017-09-29.sql scriptstranslation-2017-09-30.sql sent 215 bytes received 6951802 bytes 2780806.80 bytes/sec total size is 41810363 speedup is 6.01 [root@host-10-0-251-159 ltp_bak_data]# /usr/bin/rsync -avz --delete --password-file=/usr/local/rsync/rsyncd.pass root@192.168.32.3::fdfs_data /home/ltp_bak_data/fdfs_data/
在备机上,将同步命令加到cron表达式中,设置为每十分钟执行一次(备机执行)
[root@host-10-0-251-159 ltp_bak_data]# crontab -l */10 * * * * /usr/bin/rsync -avz --delete --password-file=/usr/local/rsync/rsyncd.pass root@192.168.32.3::mysql_dump /home/ltp_bak_data/mysql_dump/ */10 * * * * /usr/bin/rsync -avz --delete --password-file=/usr/local/rsync/rsyncd.pass root@192.168.32.3::fdfs_data /home/ltp_bak_data/fdfs_data/
附,数据库每日备份,并清理七天前数据脚本
[root@localhost ~]# vi /home/data/mysqldumpdata/dump.sh #!/bin/bash #dump sql data from database hostIP=$1 /usr/bin/mkdir -p /home/data/mysqldumpdata dataStr=`/usr/bin/date +%Y-%m-%d` destFile="/home/data/mysqldumpdata/scriptstranslation-"${dataStr}".sql" /usr/bin/mysqldump -u root -p123456 -h $hostIP scriptsTranslation > $destFile #clear seven days age dump data sevenDayAgoStr=`date -d '7 day ago' +%Y-%m-%d` destRmFile="/home/data/mysqldumpdata/scriptstranslation-"${sevenDayAgoStr}".sql" /usr/bin/rm -rf $destRmFile [root@localhost ~]# crontab -l 1 0 * * * /home/data/mysqldumpdata/dump.sh 192.168.*.* #此处要配置服务器IP