rsync /etc/fstab /opt rsync -rl /etc/fstab /boot/grub /opt
常用命令格式及备份选项: -v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-z, --compress 对备份的文件在传输时进行压缩处理
-delete 删除那些DST中SRC没有的文件,保证两边文件同步对齐
rsync同步资源的表示方式为 “用户名@主机地址::共享模块名” 或者,“rsync://用户名@主机地址/共享模块名” 前者为两个冒号分割形式,后者为URL地址形式,例如 执行以下操作将访问rsync同步源,并下载到本地/root目录下进行备份 [root@localhost~]# rsync -avz backuper@192.168.144.110::abc /root 或者 [root@localhost~]# rsync -avz rsync://backuper@192.168.144.110/abc /root
执行以下操作访问源服务器中的abc共享模块,并下载到本地的/opt目录下: [root@localhost~]# rsync -avzH --delete backuper@192.168.144.110::abc /opt
实际生产环境中通常都是计划性重复执行的,为了在周期性计划任务中不用输入密码交互式执行, 需要创建一个密码文件,保存用户的密码,如/etc/server.pass,在执行rsync同步时使用选项 --password-file=/etc/server.pass,指定即可。
crontab -e 30 22 * * * /usr/bin/rsync --delete --password-file=/etc/server.pass backuperA@192.168.144.110::abc /opt //每天22:30执行脚本 servevice crond restart chkconfig crond on
uname -r
主机 | 操作系统 | IP地址 | 所需软件 |
---|---|---|---|
rsync源服务器 | CentOS 7.0 x86_64 | 192.168.144.110 | rsync+inotify-tools-3.14.tar.gz |
rsync备份服务器 | CentOS 7.0 x86_64 | 192.168.144.120 | rsync+inotify-tools-3.14.tar.gz |
rpm -q rsync //查询rsync是否安装,一般为系统自带安装 yum install rsync -y //若没有安装,使用yum安装
tar zxzvf notify-tools-3.14.tar.gz -C /opt cd /opt/inotify-tools-3.14 ./configuer make && make install
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384 //监控事件队列数 fs.inotify.max_user_instances = 1024 //最多监控实例数 fs.inotify.max_user_watches = 1048576 //实例最多监控文件数
vim /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example: uid = nobody //rsync服务uid gid = nobody //rsync服务gid use chroot = yes //禁锢家目录 pid file = /var/run/rsyncd.pid //进程文件目录 address = 192.168.144.110 //源服务器监听地址 port = 873 //服务端口号 log file = /var/log/rsyncd.log //指定日志文件位置 hosts allow = * //允许任意网段服务器可与之同步 # max connections = 4 //最大链接数 # # exclude = lost+found/ # # transfer logging = yes # # timeout = 900 # # ignore nonreadable = yes # # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # # # [ftp] # # path = /home/ftp # # comment = ftp export area # # [abc] //共享模块名称,两台服务器可以不同 path = /var/www/html //共享模块路径,两台服务器可以不同 comment = www.linuxidc.com //模块描述,两台服务器可以不同 read only = no //关闭只读权限,不写或者注释为默认选择只读 write only = no //关闭可写权限,不写或者注释为默认选择不可写 uid = root //指定模块uid gid = root //指定模块gid hosts allow = * //允许任意网段服务器访问此模块 auth users=test //模块访问验证用户名称 secrets file = /etc/rsyncd_users.db //模块验证密码文件,键值对形式 dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 //同步过程中遇到此类文件不进行压缩
rsync --daemon
vim /etc/rsyncd_users.db
test:abc123 //"用户名:密码"的形式
chmod 600 /etc/rsyncd_users.db
vim /etc/server.pass
abc123 //只含密码的文件
chmod 600 /etc/server.pass
vim /opt/ino.sh
#!/bin/bash DESIP=192.168.144.120 //设置源服务器需要将文件上行(发送)到备份服务器IP地址 INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/" RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ test@$DESIP::abc/" $INOTIFY_CMD | while read DIRECTORY EVENT FILE do if [ $(pgrep rsync | wc -l) -gt 0 ] ; then $RSYNC_CMD fi done
chmod +x /opt/ino.sh