rsync是一款快速增量备份工具
·Remote Sync,远程同步
·支持本地复制,或者与其他SSH、rsync主机同步
·官方网站:http://rsync.samba.org
rsync同步源
·指备份操作的远程服务器,也称为备份源
配置rsync源
基本思路
·建立rsyncd.conf配置文件、独立的账号文件
·启用rsync的–daemon模式(daemon是一个守护进程)
示例
·用户backuper,允许下行同步
·操作目录: /var/www/html
配置文件rsyncd.conf
·需手动建立,语法类似于Samba配置
·认证配置auth users、secrets file,不加则为匿名
rsync账号文件
·采用“用户名:密码”的记录格式,每行一个用户记录
·独立的账号数据,不依赖于系统账号
启用rsync服务
·通过- -daemon独自提供服务
关闭rsync服务
·执行kill $(cat /var/run/rsyncd.pid)关闭rsync服务
配置rsync源服务器
两台服务器:
一台源端 20.0.0.7
一台发起端 20.0.0.8
源端:
rpm -qa rsync (查看rsync包是否存在,一般centos会自带这个软件)
vim /etc/rsyncd.conf
配置内容:
vim /etc/rsyncd_users.db
backuper:abc123
chmod 600 /etc/rsyncd_users.db
yum -y install httpd
cd /var/www/html/
echo “this is test” > abc.txt
ls
启动服务:
rsync --daemon
查看状态:
netstat -antp | grep 873
发起端:
rpm -qa rsync (查看rsync包是否存在,一般centos会自带这个软件)
格式一:rsync -avz backuper@20.0.0.7::wwwroot /opt/
格式二:rsync -avz rsync://backuper@20.0.0.7/wwwroot /opt/
配置免交互(无需验证密码):
vim /etc/server.pass
abc123
chmod 600 /etc/server.pass
rsync -az --delete --password-file=/etc/server.pass backuper@20.0.0.7::wwwroot /opt/
rsync+inotif实时同步:
发起端:
yum -y install gcc gcc-c++ make pcre-devel expat-devel perl
yum -y install httph
chmod 777 /var/www/root
vim /etc/sysctl.conf
添加:
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p
(编译安装软件包inotify-tools-3.14.tar.gz)
tar zxvf inotify-tools-3.14.tar.gz -C /opt/
cd /opt/inotify-tools-3.14
./configure
make && make install
开启监控:
inotifywait -mrq -e modify,create,move,delete /var/www/html
此时,进入监控状态,没办法输入命令,可以在远程连接终端上重新建立一个连接:
在/var/www/html 下创建、删除文件,会在监控状态的终端窗口有提示信息。
回到监控状态的发起端:
ctrl+c
cd /opt
vim /opt/inotify.sh
编辑以下脚本:
chmod +x inotify.sh
到源端:
chmod 777 /var/www/html
vim /etc/rsyncd.conf
将
read only = yes 这一项
修改为
read only = no
重新启动rsync进程:
netstat -antp | grep rsync
kill -9 9268
rsync --daemon
这时候会提示报错:
这时操作:
cd /var/run
ls
cat rsyncd.pid
9268 (进程号跟刚刚是一样的,我们手动删除)
rm -rf rsyncd.pid
然后再开启rsync:
rsync --daemon
netstat -antp | grep rsync
9468是新的进程号,已经重启好了。
到发起端开启脚本:
./inotify.sh
这时候处于监控状态,不能输入命令,到另一个远程连接终端窗口操作
另一个远程理解终端窗口操作:
cd /var/www/html
echo “this is test” > test.txt
cat test.txt
这时监控窗口会有提示信息:
到源端看看刚刚在发起端创建的文件是否已经同步:
cd /var/www/html/
ls
cat test.txt
在发起端创建的文件被同步到源端了
回到发起端另外的远程连接终端窗口删除这个文件:
到源端:
ls /var/www/html/
文件被同步删掉了,完成rsync+inotify远程实时同步。