Rsync的三种工作模式:
1.本地模式
Local: rsync [OPTION...] SRC... [DEST]
2.远程shell访问模式
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
3.守护进程daemon模式/端口:873
Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
本地模式与cp命令很相似
文件拷贝:
rsync -vzrtopg /etc/test.txt /tmp
shell模式可以进行远程同步(通过-e参数指定ssh与22端口)
rsync -avz test.txt -e 'ssh -p 22' root@server02:/tmp/
对于rsync的daemon模式,可以通过配置定时任务来进行文件的日常备份。
=============================服务端配置==================================
服务端主机:server01
1.创建rsync进程账号
groupadd rsync
useradd -g rsync -M -s /sbin/nologin -M rsync
2.配置文件(默认不存在,需要创建)
/etc/rsyncd.conf
uid = rsync
gid = rsync
max connections = 6
timeout = 300
pid file = /var/run/rsync.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsync.log
ignore errors
read only = false
hosts allow = 192.168.1.0/24
list = false
auth users = rsyncer
secrets file = /etc/rsync.password
[backup]
path = /backup
comment = data backup
3.服务端虚拟账号设置
创建 /etc/rsync.password文件
格式:
虚拟账户名:密码
rsyncer:123456
4.修改密码文件权限
chmod 600 /etc/rsync.password
5.修改备份目录权限
chown rsync.rsync /backup/
6.启动进程
/usr/bin/rsync –daemon
=============================客户端配置==================================
客户端主机:client01/02
1.客户端创建虚拟账号密码文件/etc/rsync.password且密码与服务端虚拟账号密码相同
2.修改密码文件权限
chmod 600 /etc/rsync.password
3.同步测试
rsync -avz /backup/ rsyncer@server01::backup/ --password-file=/etc/rsync.password
[root@client01]# rsync -avz /backup/ rsyncer@server01::backup/ --password-file=/etc/rsync.password
sending incremental file list
./
goo0001.txt
goo0002.txt
goo0003.txt
goo0004.txt
goo0005.txt
goo0006.txt
goo0007.txt
goo0008.txt
goo0009.txt
goo0010.txt
goo0011.txt
goo0012.txt
goo0013.txt
goo0014.txt
goo0015.txt
goo0016.txt
goo0017.txt
goo0018.txt
goo0019.txt
goo0020.txt
sent 1746 bytes received 391 bytes 1424.67 bytes/sec
total size is 1851 speedup is 0.87
=============================定时任务==================================
1.测试通过后将同步命令放入脚本backup.sh
echo "rsync -avz /backup/ rsyncer@server01::backup/ --password-file=/etc/rsync.password" >> /opt/backup.sh
2.配置计划任务每天23:30进行备份:
crontab -e
30 23 * * * /bin/bash /opt/backup.sh > /dev/null 2>&1