rsync源码安装:https://rsync.samba.org/
#安装依赖
yum install -y openssl-devel zlib-devel zlib openssl make gcc libzstd-devel lz4-devel
wget https://download.samba.org/pub/rsync/src/rsync-3.2.3.tar.gz
tar zxvf rsync-3.2.3.tar.gz
cd rsync-3.2.3/
./configure --prefix=/usr/local/rsync --disable-ipv6 --disable-xxhash
make && make install
ln -s /usr/local/rsync/bin/rsync /usr/local/bin/rsync
# 编辑配置文件
vim /usr/local/rsync/rsyncd.conf
uid = rsync # rsync 服务用户,rsync 管理备份目录
gid = rsync # rsync 用户组
use chroot = no # 内网可以关闭chroot 机制
fake super = yes # 不以root 用户运行, CentOS 7 新加字段
max connections = 200 # 最大连接数;0 无限制,负值为关闭该模块
timeout = 600 # 默认0 ,不超时,建议300-600(5-10min)
pid file = /var/run/rsyncd.pid # pid 文件存放位置
lock file = /var/run/rsyncd.lock # lock 文件存放位置
log file = /var/log/rsyncd.log # log 文件存放位置
ignore errors # 忽略 I/O 错误
read only = false
list = false
hosts allow = 192.168.254.0/24 //允许进行同步的网络
# hosts deny = *
auth users = rsync_backup
secrets file =/etc/rsync.password //密码文件
[backup]
path = /backup
------------------------------------------------------------
uid = rsync
gid = rsync
use chroot = no
fake super = yes
max connections = 200
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.123.0/24
# hosts deny = *
auth users = rsync
secrets file =/etc/rsync.password
[backup]
path = /www/backup
[root@Krist /]# groupadd rsync
[root@Krist /]# useradd -M -g rsync -s /sbin/nologin rsync
[root@Krist /]# passwd rsync
[root@Krist /]# ln -s /usr/local/rsync/bin/rsync /usr/bin/rsync
[root@Krist /]# ln -s /usr/local/rsync/bin/rsync-ssl /usr/bin/rsync-ssl
以root方式启动rsync
/usr/bin/rsync --daemon
/usr/bin/rsync --daemon --config=/usr/local/rsync/rsyncd.conf #--config用于指定rsyncd.conf的位置,如果在/etc下可以不写
[root@Krist /]# lsof -i :873
#修改密码文件权限
[root@Krist /]# vim /etc/rsync.password
服务端使用 ‘用户名:密码’
客户端使用 ‘密码’
[root@Krist /]# chmod 600 /etc/rsync.password
#修改备份目录权限
[root@Krist /]# chown -R rsync:rsync /www/backup/
#修改rsync配置后服务器端需要重启
pkill rsync
rsync --daemon
lsof -i :873
在客户端测试:
[root@Krist2 /]# rsync -avzP --delete rsync@192.168.123.188::backup /www/backup/ --password-file=/etc/rsync.password
receiving incremental file list
./
krist.txt
1,223 100% 1.17MB/s 0:00:00 (xfr#1, to-chk=8/10)
test.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=7/10)
xaa
170 100% 166.02kB/s 0:00:00 (xfr#3, to-chk=6/10)
xab
182 100% 177.73kB/s 0:00:00 (xfr#4, to-chk=5/10)
xac
185 100% 180.66kB/s 0:00:00 (xfr#5, to-chk=4/10)
xad
195 100% 190.43kB/s 0:00:00 (xfr#6, to-chk=3/10)
xae
185 100% 180.66kB/s 0:00:00 (xfr#7, to-chk=2/10)
xaf
190 100% 185.55kB/s 0:00:00 (xfr#8, to-chk=1/10)
xag
116 100% 113.28kB/s 0:00:00 (xfr#9, to-chk=0/10)
sent 202 bytes received 789 bytes 1,982.00 bytes/sec
total size is 2,446 speedup is 2.47
[root@Krist2 /]# cd /www/backup/
[root@Krist2 backup]# ls
krist.txt test.txt xaa xab xac xad xae xaf xag
ssh配置免密码登入
在客户端生成ssh密钥
[root@Krist /]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #用户所在家目录下的.ssh目录生成id_rsa(私钥), id_rsa.pub(公钥)文件
Enter passphrase (empty for no passphrase): #如果要以ssh认证文件+密码的登录方式则可以输入密码
Enter same passphrase again: #同上
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:qDQZcu7fSoBHR9H7Sa0Zqmd/fAzGUWPsizm4YKfMk/o root@Krist.com
The key's randomart image is:
+---[RSA 2048]----+
| oo . |
| . . = |
| . + . . . + . |
| * + .. o o . |
| . B . S+ B + . |
| + + + B B . |
| o .= = + + |
| o..O . o o |
| +*Eo.. . |
+----[SHA256]-----+
#把公钥复制到服务端上
[root@Krist2 /]# ssh-copy-id root@192.168.123.188
#服务端上修改权限
[root@Krist /]# mkdir -p /www/.auth/.ssh/ && cp /root/.ssh/authorized_keys /www/.auth/.ssh/
[root@Krist .ssh]# chmod -R 700 /www/.auth/.ssh/
[root@Krist .ssh]# chmod 600 /www/.auth/.ssh/authorized_keys
#修改配置文件
[root@Krist /]# vim /usr/local/rsync/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
fake super = yes
max connections = 200
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.123.0/24
# hosts deny = *
[backup]
path = /www/backup
测试:
[root@Krist2 /]# rsync -avzP --delete rsync@192.168.123.188::backup /www/backup/
receiving incremental file list
sent 25 bytes received 267 bytes 584.00 bytes/sec
total size is 2,486 speedup is 8.51
在客户端生成shell脚本:
[root@Krist2 www]# vim rsyncsh.sh
#!/bin/bash
#backup /www/backup DIR
rsync -avzP --delete rsync@192.168.123.188::backup /www/backup --password-file=/etc/rsync.password
RSYNC_STAT=$?
if [ "$RSYNC_STAT" != 0 ];then
echo $(date +%Y"-"%m"-"%d" "%H":"%M":"%S) ' backup error' >> /var/log/rsyncd.log
else
echo $(date +%Y"-"%m"-"%d" "%H":"%M":"%S) ' backup ok' >> /var/log/rsyncd.log
fi
[root@Krist2 www]# chmod +x rsyncsh.sh
#crontab
[root@Krist2 www]# crontab -e
*/1 * * * * /www/rsyncsh.sh
[root@Krist2 www]# systemctl restart crond #重启crond服务
rsync基本操作:
从主机拉取数据:
rsync -avzP --delete root@{remoteHost}:{remoteDir} {localDir}
host1:
[root@Krist backup]# pwd
/www/backup
[root@Krist backup]# ls
krist.txt
host2:
[root@Krist2 backup]# pwd
/www/backup
[root@Krist2 backup]# ls
[root@Krist2 backup]# rsync -avzP --delete root@192.168.123.188:/www/backup/ /www/backup/
root@192.168.123.188's password:
receiving incremental file list
./
krist.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2)
sent 46 bytes received 105 bytes 23.23 bytes/sec
total size is 0 speedup is 0.00
[root@Krist2 backup]# ll
total 0
-rwxrw-r--. 1 root root 0 Sep 22 12:33 krist.txt
参数详解:
-a 参数,相当于-rlptgoD(-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件);
-z 传输时压缩;
-P 传输进度;
-v 传输时的进度等信息;
--delete 删除操作,如果不加--delete,只会同步增加的内容,新目录中删除的内容,并不会同步到旧目录中!
--exclude 忽略文件或目录
向备机推送数据:
rsync -avzP --delete {localDir} root@{remoteHost}:{remoteDir}
host1:
[root@Krist backup]# touch test.txt && chmod 755 test.txt
[root@Krist backup]# rsync -avzP --delete /www/backup/ root@192.168.123.189:/www/backup/
root@192.168.123.189's password:
sending incremental file list
./
krist.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=1/3)
test.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)
sent 176 bytes received 57 bytes 42.36 bytes/sec
total size is 0 speedup is 0.00
host2:
[root@Krist2 backup]# ll
total 0
-rwxrw-r--. 1 root root 0 Sep 22 12:33 krist.txt
-rwxr-xr-x. 1 root root 0 Sep 22 12:46 test.txt
一台服务器不同目录的备份:
rsync -avzP --delete 同步源目录 同步目标目录
[root@Krist backup]# mkdir -p /www/data/ && chmod 755 /www/data/
[root@Krist backup]# ls
krist.txt test.txt
[root@Krist backup]# rsync -avzP --delete /www/backup/ /www/data/
sending incremental file list
./
krist.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=1/3)
test.txt
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=0/3)
sent 172 bytes received 57 bytes 458.00 bytes/sec
total size is 0 speedup is 0.00
[root@Krist backup]# ll /www/data/
total 0
-rwxrw-r--. 1 root root 0 Sep 22 12:33 krist.txt
-rwxr-xr-x. 1 root root 0 Sep 22 12:46 test.txt
可以使用 --exclude 参数来实现对目录或文件的忽略。必须是忽略在前,删除在后,否则没有意义!
host1:
[root@Krist backup]# rsync -avzP --exclude "krist.txt" --delete /www/backup/ root@192.168.123.189:/www/backup/
root@192.168.123.189's password:
sending incremental file list
./
test.txt
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2)
sent 128 bytes received 38 bytes 36.89 bytes/sec
total size is 0 speedup is 0.00
host2:
[root@Krist2 backup]# ll
total 0
-rwxr-xr-x. 1 root root 0 Sep 22 12:46 test.txt
评论前必须登录!
立即登录 注册