搭建rsync服務並同步重要資料

振宇要低調發表於2018-07-10

在主備機器上均安裝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

 

相關文章