在主备机器上均安装rsync,在主机上以daemon的模式启动,在备机上定时执行同步命令。安装rsync的命令如下:
1、下载安装包(主备机均执行)
[root@localhost home]# wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.4.tar.gz
---- ::-- 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:::::
Connecting to rsync.samba.org (rsync.samba.org)|144.76.82.156|:... connected.
HTTP request sent, awaiting response... OK
Length: (755K) [application/gzip]
Saving to: ‘rsync-3.0..tar.gz’ %[====================================================================================================================================================================================================================================>] , 434KB/s in .7s -- :: ( KB/s) - ‘rsync-3.0..tar.gz’ saved [/]
2、编译安装(主备机均执行)
[root@localhost home]# tar -xvzf rsync-3.0..tar.gz
[root@localhost home]# cd rsync-3.0.
[root@localhost rsync-3.0.]# ./configure --prefix=/usr/local/rsync
[root@localhost rsync-3.0.]# make && make install
3、配置(主机执行)
[root@localhost rsync-3.0.]# mkdir /usr/local/rsync/logs
[root@localhost rsync-3.0.]# mkdir /usr/local/rsync/etc
[root@localhost rsync-3.0.]# mkdir /usr/local/rsync/run
[root@localhost rsync-3.0.]# vim /usr/local/rsync/etc/rsyncd.conf
[root@localhost rsync-3.0.]# cat /usr/local/rsync/etc/rsyncd.conf uid = root
gid = root
port =
use chroot = no
hosts allow = 10.0.251.159 #允许连接的主机IP,在我们的环境中,该IP为备机IP
#hosts deny = 192.168.10.0/
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.]# vim /usr/local/rsync/rsyncd.pass
root:密码 [root@localhost rsync-3.0.]# chmod /usr/local/rsync/rsyncd.pass
主机上以daemon的形式启动rsync server,命令如下(主机执行):
[root@localhost rsync-3.0.]# /usr/bin/rsync --daemon --config=/usr/local/rsync/etc/rsyncd.conf &
[]
在备机上手动执行同步命令,测试是否能够将主机的数据同步到备机之上(备机执行):
[root@host---- 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---.sql
scriptstranslation---.sql
scriptstranslation---.sql
scriptstranslation---.sql
scriptstranslation---.sql
scriptstranslation---.sql
scriptstranslation---.sql sent bytes received bytes 2780806.80 bytes/sec
total size is speedup is 6.01
[root@host---- 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---- ltp_bak_data]# crontab -l
*/ * * * * /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/
*/ * * * * /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=$
/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
* * * /home/data/mysqldumpdata/dump.sh 192.168.*.* #此处要配置服务器IP