rsync是一个开源的快速备份工具,支持增量备份,保持连接和权限,采用同步算法,传输前执行压缩。适合异地备份,镜像服务
A机器为rsync服务端 IP:A机器上的操作:192.168.200.101
B机器为rsync客户端 IP:B机器上的操作:192.168.200.102
A机器上的操作:192.168.200.101:
[root@crushlinux1~]# rpm -q rsync
rsync-2.6.8-3.1
[root@crushlinux1~]# yum -y install httpd
[root@crushlinux1~]# useraddrget
[root@crushlinux1~]# passwdrget
[root@crushlinux1~]# useraddrput
[root@crushlinux1~]# passwdrput
[root@crushlinux1~]# vi /etc/ssh/sshd_config
109UseDNS no
110AllowUsersrgetrput
----------------修改以上几行 ----------------
[root@crushlinux1~]# servicesshd restart
[root@crushlinux1html]# mkdir upload
[root@crushlinux1html]# chown -R rput:rput /var/www/html/
[root@crushlinux1html]# setfacl -R -m user:daemon:rwx /var/www/html/upload
----------------acl权限参数详解 ----------------
setfacl设置acl权限
getfacl查看acl权限
-R 递归
-m 制定权限
-x 个别删除
-b全部删除
setfacl-R -b /var/www/html
setfacl-R -x user:daemon /var/www/html/upload
---------------------------------------------
[root@crushlinux1html]# getfacl /var/www/html/upload/
[root@crushlinux1html]# ls -ld /var/www/html/upload/
drwxrwxr-x+2 rputrput 4096 08-09 09:23 /var/www/html/upload/
让以后在/var/www/html/upload/新建立的文档,daemon用于都具有rwx权限
[root@crushlinux1html]# setfacl -m default:user:daemon:rwx /var/www/html/upload
[root@crushlinux1html]# getfacl /var/www/html/upload |grep default
配置rsync备份源
1、建立/etc/rsyncd.conf配置文件
[root@crushlinux1html]# vi /etc/rsyncd.conf
uid= nobody
gid= nobody
usechroot= yes
address= A机器上的操作:192.168.200.101
port= 873
log file= /var/log/rsyncd.log
pid file= /var/run/rsyncd.pid
hostsallow = 192.168.200.0/24
[wwwroot]
path= /var/www/html
comment= Document Root os www.crushlinux.com
readonly = yes
dontcompress = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users= backuper
secrets file= /etc/rsyncd_users.db
2、为备份账户创建数据文件
[root@crushlinux1html]# vi /etc/rsyncd_users.db
backuper:pwd123
[root@crushlinux1html]# chmod 600 /etc/rsyncd_users.db
[root@crushlinux1html]# chmodo+r /var/www/html/
3、启动服务
[root@crushlinux1html]# rsync --daemon
[root@crushlinux1html]# netstat -anpt |greprsync
tcp 0 0 A机器上的操作:192.168.200.101:873 0.0.0.0:* LISTEN 5243/rsync
[root@crushlinux1html]# ps -aux |greprsync
Warning:bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.7/FAQ
root 5243 0.0 0.1 5248 464? Ss 10:02 0:00 rsync--daemon
root 5248 0.0 0.2 5020 680 pts/0R+ 10:02 0:00 greprsync
4、配置成只有客户机连接的时候才启动(交给xinetd管理)
[root@crushlinux1html]# cat /etc/xinetd.d/rsync
6 disable = no
11 server_args = --daemon
----------------修改以上几行 ----------------
[root@crushlinux1~]# yum -y install xinetd
[root@crushlinux1~]# servicexinetd start
---------------------------------------------------------------------------------
本地备份:
[root@crushlinux1~]# rsync /etc/fstab /opt/
[root@crushlinux1opt]# rsync -rl /etc/fstab /boot/grub/ /opt/
异地备份:
B机器上的操作:192.168.200.102:
---------------------------------------------------------------------------------
[root@crushlinux2~]# rsync -avzrget@A机器上的操作:192.168.200.101:/var/www/html/ /opt/
[root@crushlinux2~]# rsync -avzbackuper@A机器上的操作:192.168.200.101::wwwroot /root
[root@crushlinux2~]# rsync -avz rsync://backuper@A机器上的操作:192.168.200.101/wwwroot /root
1、下行同步ssh备份源
将服务器A机器上的操作:192.168.200.101 /var/www/html文件夹与本地/wwwroot文件夹同步(保持文件权限属性,
软硬连接,ACL属性,删除/wwwroot中多余文件,传输过程进行加密)
[root@crushlinux2~]# mkdir -p /wwwroot
[root@crushlinux2~]# rsync -avzH --delete rget@A机器上的操作:192.168.200.101:/var/www/html/ /wwwroot
[root@crushlinux2~]# ls /wwwroot/
在此执行自动做增量备份,同名的文件不复制
A机器上的操作:192.168.200.101:
[root@crushlinux1html]# rm -rf index.html upload/
[root@crushlinux1html]# for i in {1..10}; do touch $i.txt; done
B机器上的操作:192.168.200.102:
[root@crushlinux2~]# rsync -avzH --delete rget@A机器上的操作:192.168.200.101:/var/www/html/ /wwwroot
[root@crushlinux2~]# ls /wwwroot/
2、下行同步rsync备份源
B机器上的操作:192.168.200.102:
[root@crushlinux2~]# mkdir /myweb
[root@crushlinux2~]# rsync -avzH --delete backuper@A机器上的操作:192.168.200.101::wwwroot /myweb
3、上行同步ssh备份源
[root@crushlinux2~]# cd /usr/share/doc/HTML/
[root@crushlinux2HTML]# rsync -rlvz --delete zh-CN/ rput@A机器上的操作:192.168.200.101:/var/www/html
----------------------------------------------------------------------------------
1、ssh备份源的无交互验证
B机器上的操作:192.168.200.102:
[root@crushlinux2~]# ssh-keygen -t rsa
[root@crushlinux2~]# cd .ssh/
[root@crushlinux2.ssh]# cat id_rsa.pub >authorized_keys
[root@crushlinux2.ssh]# cd ../
[root@crushlinux2~]# scp -pr .ssh/ rget@A机器上的操作:192.168.200.101:/home/rget/
[root@crushlinux2~]# sshrget@A机器上的操作:192.168.200.101
[root@crushlinux2~]# rsync -avzH --delete rget@A机器上的操作:192.168.200.101:/var/www/html/ /wwwroot
2、rsync备份源的无交互验证
[root@crushlinux2~]# export RSYNC_PASSWORD=pwd123
[root@crushlinux2~]# rsync -avzH --delete backuper@A机器上的操作:192.168.200.101::wwwroot /wwwroot/
[root@crushlinux2~]# vi rsync_get_wwwroot.sh
----------------------------
#!/bin/bash
CMD="/usr/bin/rsync"
RSYNC_USER="backuper"
RSYNC_PASSWORD="pwd123"
ARGS="-az--delete"
SRC="192.168.4.200::wwwroot"
DST="/wwwroot"
mkdir-p $DST
$CMD $ARGS $RSYNC_USER@$SRC $DST
[root@crushlinux2~]# crontab -e
3020 * * * /root/rsync_get_wwwroot.sh
[root@crushlinux2~]# servicecrond restart
[root@crushlinux2~]# chkconfigcrond on
2
A机器上的操作:192.168.200.101:
[root@crushlinux1~]# cat /proc/sys/fs/inotify/max_queued_events
16384
[root@crushlinux1~]# cat /proc/sys/fs/inotify/max_user_instances
128
[root@crushlinux1~]# cat /proc/sys/fs/inotify/max_user_watches
8192
[root@crushlinux1~]# vi /etc/sysctl.conf
fs.inotify.max_queued_events= 16384
fs.inotify.max_user_instances= 1024
fs.inotify.max_user_watches= 1048576
[root@crushlinux1~]# sysctl -p
[root@crushlinux1~]# tar zxvf inotify-tools-3.14.tar.gz
root@crushlinux1~]# cd inotify-tools-3.14
[root@crushlinux1inotify-tools-3.14]# ./configure
[root@crushlinux1inotify-tools-3.14]# make&& make install
[root@crushlinux1inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete/var/www/html/
/var/www/html/
/var/www/html/CREATE,ISDIR a
/var/www/html/CREATE,ISDIR b
另一个终端:
[root@crushlinux1~]# mkdir /var/www/html/a
[root@crushlinux1~]# mkdir /var/www/html/b
[root@crushlinux1~]# vi inotify_rsync.sh
#!/bin/bash
INOTIFY_CMD="inotifywait-mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync-azH --delete /var/www/html/ rput@192.168.4.200:/var/www/html"
$INOTIFY_CMD| whilereadDIRECTORY EVENT FILE
do
if[ $(pgrep rsync| wc-l) -le0 ]
then
$RSYNC_CMD
fi
done
[root@crushlinux1~]# chmod +x inotify_rsync.sh
[root@crushlinux1~]# echo "/bin/bash /root/inotify_rsync.sh" /etc/rc.local
[root@crushlinux1~]# cat /etc/rc.local
转载于:https://blog.51cto.com/smallyue20/1280998