一、環境準備(一定要配置主機名和hosts記錄)
IP地址:
192.168.1.103
192.168.1.221
操作系統:
centos 6.5 64位
需求:
將103的/home/backup/同步到221的/home/backup/。
二、rsync部署(221)
1、關閉SELINUX
vi /etc/selinux/config
用 # 註釋掉下面兩行代碼:
#SELINUX=enforcing
#SELINUXTYPE=targeted
增加下面一行代碼:
SELINUX=disabled
運行此命令立即生效。
setenforce 0
2、開啓防火牆tcp 873端口、或關掉防火牆(rsync默認端口)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
或
service iptables stop
chkconfig iptables off
3、安裝rsync服務端軟件
yum install rsync xinetd
vi /etc/xinetd.d/rsync
修改disable的值爲no:
disable = no
啓動xinetd(CentOS中是以xinetd來管理Rsync服務的)
/etc/init.d/xinetd start
4、創建rsyncd.conf配置文件(假如需要同步多個目錄,注意加多個目錄)
創建配置文件:
vi /etc/rsyncd.conf
添加以下代碼
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsyncd.secret
motd file = /etc/rsyncd.motd
[syncdata]
path = /home/backup/
comment = syncdata
uid = gxm
gid = gxm
incoming chmod = Du=rwx,Dog=rx,Fu=rwx,Fgo=rx
port=873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = root
hosts allow = 192.168.1.103
hosts deny = *
5.創建用戶認證文件
配置文件
vi /etc/rsyncd.secret
root:123456
vi /etc/rsyncd.passwd
123456
保存退出
6.設置文件權限
設置文件所有者讀取、寫入權限
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsyncd.passwd
chmod 600 /etc/rsyncd.secret
7、操作系統建立gxm用戶和組
8.啓動rsync,並設置開機自啓動
/etc/init.d/rsync start
/etc/init.d/xinetd start
chkconfig rsync on
chkconfig xinetd on
三、rsync部署(103)
8.創建用戶認證文件
配置文件
vi /etc/rsyncd.secret
root:123456
vi /etc/rsyncd.passwd
123456
保存退出
9.設置文件權限
設置文件所有者讀取、寫入權限
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsyncd.passwd
chmod 600 /etc/rsyncd.secret
10、安裝rsync同步工具
yum install rsync
11、執行同步命令
rsync -azv --delete /home/backup/ [email protected]::syncdata --password-file=/etc/rsyncd.passwd
12、將上面的同步命令寫成腳本,然後定時運行。
腳本:
vi rsyncdata.sh
#!/bin/bash
rsync -azv --delete /home/backup/ [email protected]::syncdata --password-file=/etc/rsyncd.passwd
定時任務:
- 22 * sh /root/rsyncdata.sh