rsync+inotify配置极同步脚本两个

300 阅读4分钟

rsync 为系统自带 不用安装
但是推荐升级一下 低版本的rsync会让操作系统负载变高或者其他一些稀奇古怪的问题

服务器端.
注:在/etc目录下创建一个rsyncd的目录,我们用来存放rsyncd.conf(rsync配置文件) 和rsyncd.secrets(rsync密钥文件)文件;

mkdir /etc/rsyncd  
cd /etc/rsyncd  
  
vi rsyncd.conf
# 配置文件内容极说明
# Minimal configuration file for rsync daemon  
# See rsync(1) and rsyncd.conf(5) man pages for help  

# This line is required by the /etc/init.d/rsyncd script  
pid file = /var/run/rsyncd.pid     #进程位置  
port = 873      开放端口  
address = 192.168.1.171         服务端IP  
#uid = nobody  
#gid = nobody  
uid = root        #执行用户UID  
gid = root        #执行用户GID  
  
use chroot = yes  
注:用chroot,在传输文件之前,服务器守护程序在将chroot 到文件系统中的目录中,这样做的好处是可能保护系统被安装漏洞侵袭的可能.缺点是需要超级用户权限.另外对符号链接文件,将会排除在外,也就是说,你在rsync服务器上,如果有符号链接,你在备份服务器上运行客户端的同步数据时,只会把符号链接名同步下来,并不会同步符号链接的内容;这个需要自己来尝试;  
read only = yes     属性只读  
  
  
#limit access to private LANs  
#指定单个IP,也可以指定整个网段,能提高安全性.  
#格式是ip 与ip 之间.ip和网段之间.网段和网段之间要用空格隔开;  
hosts allow=192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0  
hosts deny=*  
  
max connections = 5      #客户端最多连接数  
motd file = /etc/rsyncd/rsyncd.motd        #定义登陆提示信息文件位置  
  
#This will give you a separate log file  
#log file = /var/log/rsync.log      #日志文件位置  
  
#This will log every file transferred - up to 85,000+ per user, per sync  
#transfer logging = yes           # 是否开启日志  
  
# 传输文件的日志  
log format = %t %a %m %f %b  
syslog facility = local3  
timeout = 300  
  
[test1]     #[]内为模块名称,可自行更改  
path = /home      #该模块目录  
list=yes          #是否显示模块信息  
ignore errors     #忽略IO错误  
auth users = root       # 该服务器的用户(必须是系统存在的)  
secrets file = /etc/rsyncd/rsyncd.secrets        # 密码文件位置  
comment = Hello             # 该模块显示信息  
exclude = beinan/  samba/          #不备份 beinan和samba文件或文件夹  
read only = yes # 是否只读

密码文件 用户名:密码

vi rsyncd.secrets  
root:root

需要权限为600(或者更低) 不然会报错

chmod 600 /etc/rsyncd/rsyncd.secrets

这是个类似欢迎页的东西 不知道该叫啥.

vi /etc/rsyncd/rsyncd.motd  
++++++++++++++++++++++++++++++++++  
+             Hello              +  
++++++++++++++++++++++++++++++++++

服务端防火墙策略

iptables -A INPUT -p tcp -m state --state NEW  -m tcp --dport 873 -j ACCEPT

开启rsync

/usr/bin/rsync --daemon  --config=/etc/rsyncd/rsyncd.conf

关闭rsync


killall rsync

客户端使用方法

#列出192.168.0.1上rsync的模块(不需要密码)

rsync --list-only root@192.168.0.1::  
  
++++++++++++++++++++++++++++++++++  
+             Hello              +  
++++++++++++++++++++++++++++++++++  
  
test1        Hello

显示test1模块里的内容(需要内容)

rsync  --list-only  root@192.168.0.1::test1  
  
++++++++++++++++++++++++++++++++++  
+             Hello              +  
++++++++++++++++++++++++++++++++++  
  
test1        Hello  
Password:  
drwxr-xr-x          16 2009/11/11 00:02:18 .  
drwxr-xr-x          94 2009/11/11 16:31:18 web

rsync命令参数说明

-a 参数,相当于-rlptgoD  
-r 是递归  
-l 是链接文件,意思是拷贝链接文件;  
-p 表示保持文件原有权限;  
-t 保持文件原有时间;  
-g 保持文件原有用户组;  
-o 保持文件原有属主;  
-D 相当于块设备文件;  
-z 传输时压缩;  
-P 传输进度;  
-v 传输时的进度等信息,和-P有点关系,自己试试.  
--exclude # 排除文件或文件夹  
--exclude-from # 排除文本列表里的内容

用root用户,将192.168.0.1服务器上的test1模块相关的内容备份到本地的 /data/bak下 (需要密码)

rsync -avzP root@192.168.0.1::test1   /data/bak

我们引入一个 –delete 选项,表示客户端上的数据要与服务器端完全一致,如果 linuxsirhome目录中有服务器上不存在的文件,则删除.

最终目的是让linuxsirhome目录上的数据完全与服务器上保持一致;用的时候要小心点,最好不要把已经有重要数所据的目录,当做本地更新目录,否则会把你的数据全部删除;

rsync -avzP  --delete root@192.168.0.1::test1   /data/bak

rsync.password文件为密码文件 这样可以不用输入密码

要注意 密码文件只需要密码 并且权限也要是 600(或者更低)


touch rsync.password  
chmod 600 rsync.password  
rsync -avzP  --delete  --password-file=rsync.password root@192.168.0.1::test1   /data/bak

让rsync 客户端自动与服务器同步数据

写个小bash脚本 用于定时同步

mkdir /etc/cron.daily.rsync  
mkdir /etc/rsyncd/  
cd /etc/cron.daily.rsync  
  
vi rsync.test1.sh  
  
#!/bin/sh  
#test1 home backup  
/usr/bin/rsync -avzP --password-file=/etc/rsyncd/rsync.test1.password root@192.168.0.1::test1 /data/bak/$(date +'%m-%d-%y')  
  
chmod 755 /etc/cron.daily.rsync/rsync.test1.sh  
  
echo "passwd" > /etc/rsyncd/rsync.test1.password  
  
chmod 600 /etc/rsyncd/rsync.test1.password

下面这个脚本 是文件夹发生变动后 自动触发rsync命令 进行同步

需要安装 inotify 因为这个软件没什么说道 就不写安装步骤了


# rsync 为系统自带 不用安装  
# 但是推荐升级一下 低版本的rsync会让操作系统负载变高或者其他一些稀奇古怪的问题

# 服务器端.  
# 注:在/etc目录下创建一个rsyncd的目录,我们用来存放rsyncd.conf(rsync配置文件) 和rsyncd.secrets(rsync密钥文件)文件;

mkdir /etc/rsyncd  
cd /etc/rsyncd  
  
vi rsyncd.conf

# 配置文件内容极说明

# Minimal configuration file for rsync daemon  
# See rsync(1) and rsyncd.conf(5) man pages for help  
  
# This line is required by the /etc/init.d/rsyncd script  
pid file = /var/run/rsyncd.pid     #进程位置  
port = 873      开放端口  
address = 192.168.1.171         服务端IP  
#uid = nobody  
#gid = nobody  
uid = root        #执行用户UID  
gid = root        #执行用户GID  
  
use chroot = yes  
# 注:用chroot,在传输文件之前,服务器守护程序在将chroot 到文件系统中的目录中,这样做的好处是可能保护系统被安装漏洞侵袭的可能.缺点是需要超级用户权限.另外对符号链接文件,将会排除在外,也就是说,你在rsync服务器上,如果有符号链接,你在备份服务器上运行客户端的同步数据时,只会把符号链接名同步下来,并不会同步符号链接的内容;这个需要自己来尝试;  
read only = yes     属性只读  
  
  
#limit access to private LANs  
#指定单个IP,也可以指定整个网段,能提高安全性.  
#格式是ip 与ip 之间.ip和网段之间.网段和网段之间要用空格隔开;  
hosts allow=192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0  
hosts deny=*  
  
max connections = 5      #客户端最多连接数  
motd file = /etc/rsyncd/rsyncd.motd        #定义登陆提示信息文件位置  
  
#This will give you a separate log file  
#log file = /var/log/rsync.log      #日志文件位置  
  
#This will log every file transferred - up to 85,000+ per user, per sync  
#transfer logging = yes           # 是否开启日志  
  
# 传输文件的日志  
log format = %t %a %m %f %b  
syslog facility = local3  
timeout = 300  
  
[test1]     #[]内为模块名称,可自行更改  
path = /home      #该模块目录  
list=yes          #是否显示模块信息  
ignore errors     #忽略IO错误  
auth users = root       # 该服务器的用户(必须是系统存在的)  
secrets file = /etc/rsyncd/rsyncd.secrets        # 密码文件位置  
comment = Hello             # 该模块显示信息  
exclude = beinan/  samba/          #不备份 beinan和samba文件或文件夹  
read only = yes # 是否只读

# 密码文件 用户名:密码

vi rsyncd.secrets  
root:root

# 需要权限为600(或者更低) 不然会报错

chmod 600 /etc/rsyncd/rsyncd.secrets

# 这是个类似欢迎页的东西 不知道该叫啥.

vi /etc/rsyncd/rsyncd.motd  
++++++++++++++++++++++++++++++++++  
+             Hello              +  
++++++++++++++++++++++++++++++++++

# 服务端防火墙策略

iptables -A INPUT -p tcp -m state --state NEW  -m tcp --dport 873 -j ACCEPT

# 开启rsync

/usr/bin/rsync --daemon  --config=/etc/rsyncd/rsyncd.conf

# 关闭rsync

killall rsync

# 客户端使用方法

#列出192.168.0.1上rsync的模块(不需要密码)

rsync --list-only root@192.168.0.1::  
  
++++++++++++++++++++++++++++++++++  
+             Hello              +  
++++++++++++++++++++++++++++++++++  
  
test1        Hello

# 显示test1模块里的内容(需要内容)

rsync  --list-only  root@192.168.0.1::test1  
  
++++++++++++++++++++++++++++++++++  
+             Hello              +  
++++++++++++++++++++++++++++++++++  
  
test1        Hello  
Password:  
drwxr-xr-x          16 2009/11/11 00:02:18 .  
drwxr-xr-x          94 2009/11/11 16:31:18 web

# rsync命令参数说明

-a 参数,相当于-rlptgoD  
-r 是递归  
-l 是链接文件,意思是拷贝链接文件;  
-p 表示保持文件原有权限;  
-t 保持文件原有时间;  
-g 保持文件原有用户组;  
-o 保持文件原有属主;  
-D 相当于块设备文件;  
-z 传输时压缩;  
-P 传输进度;  
-v 传输时的进度等信息,和-P有点关系,自己试试.  
--exclude # 排除文件或文件夹  
--exclude-from # 排除文本列表里的内容

# 用root用户,将192.168.0.1服务器上的test1模块相关的内容备份到本地的 /data/bak下 (需要密码)

rsync -avzP root@192.168.0.1::test1   /data/bak

# 我们引入一个 –delete 选项,表示客户端上的数据要与服务器端完全一致,如果 linuxsirhome目录中有服务器上不存在的文件,则删除.  
最终目的是让linuxsirhome目录上的数据完全与服务器上保持一致;用的时候要小心点,最好不要把已经有重要数所据的目录,当做本地更新目录,否则会把你的数据全部删除;

rsync -avzP  --delete root@192.168.0.1::test1   /data/bak

# rsync.password文件为密码文件 这样可以不用输入密码  
# 要注意 密码文件只需要密码 并且权限也要是 600(或者更低)

touch rsync.password  
chmod 600 rsync.password  
rsync -avzP  --delete  --password-file=rsync.password root@192.168.0.1::test1   /data/bak

# 让rsync 客户端自动与服务器同步数据  
# 写个小bash脚本 用于定时同步

mkdir /etc/cron.daily.rsync  
mkdir /etc/rsyncd/  
cd /etc/cron.daily.rsync  
  
vi rsync.test1.sh  
  
#!/bin/sh  
#test1 home backup  
/usr/bin/rsync -avzP --password-file=/etc/rsyncd/rsync.test1.password root@192.168.0.1::test1 /data/bak/$(date +'%m-%d-%y')  
  
chmod 755 /etc/cron.daily.rsync/rsync.test1.sh  
  
echo "passwd" > /etc/rsyncd/rsync.test1.password  
  
chmod 600 /etc/rsyncd/rsync.test1.password

# 下面这个脚本 是文件夹发生变动后 自动触发rsync命令 进行同步  
# 需要安装 inotify 因为这个软件没什么说道 就不写安装步骤了

#!/bin/bash  
  
TEMP=`cat /proc/sys/fs/inotify/max_user_watches`  
if [ "$TEMP" != "10485760" ];  
then  
echo "root run:"  
echo "echo 10485760 > /proc/sys/fs/inotify/max_user_watches"  
exit 9  
fi  
  
# 服务器地址  
host_1=192.168.0.1  
  
# 本地文件夹  
src=/data/wwwroot/  
  
# 模块名称  
dst=www  
  
# 用户名  
user=root  
  
# rsync 密码文件  
rsync_passfile=/etc/rsyncd.secrets  
  
# inotify 文件夹  
inotify_home=/data/soft/inotify-tools-3.13  
  
# 这个判断是用于检查参数是否都存在  
if [ ! -e "$src" ] \  
|| [ ! -e "${rsync_passfile}" ] \  
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \  
|| [ ! -e "/usr/bin/rsync" ];  
then  
echo "Check File and Folder"  
exit 9  
fi  
  
# 建立log文件夹 用于查询rsync同步的内容  
if [ ! -e "${inotify_home}/log/" ];  
then  
mkdir -p ${inotify_home}/log/  
fi  
  
# 循环体部分 每次文件夹发生变动 进行同步  
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \  
| while read file  
do  
# log文件  
log_file=${inotify_home}/log/`date "+%F"`-${dst}.log  
echo "${dst} `date "+%F %H:%M:%S"`" >> ${log_file}  
rsync -avzP --delete --progress --password-file=${rsync_passfile} $src $user@$host_1::${dst} >> ${log_file}  
echo "-------------------end---------------------" >> ${log_file}  
done  
exit 0