Despliegue del entorno
- Versión del sistema
CentOS Linux, versión 7.5.1804 (Core) - Versión de Kernel
3.10.0-862.el7.x86_64 - Cortafuegos y selinux cerrados
- Asignación de funciones
192.168.153.179 esclavo (lado de respaldo) servidor de respaldo
192.168.153.178 maestro (origen de datos) servidor de datos
Iniciar implementación:
inotificar una operación de servidor de respaldo
1. Instale el servicio rsync
yum -y install rsync
2. Agregar usuario de rsync
useradd rsync -s /sbin/nologin -M
3. Cree un directorio de módulos para el modo de trabajo del demonio rsync
mkdir /backup
4. Cambie el grupo de usuarios del directorio del módulo.
chown rsync.rsync /backup/
5. Modifique el archivo de configuración
[root @ slave backup] # cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
#工作中指定用户(需要指定用户)使用root用户,防止权限问题
#指定转换用户身份/备份目录管理用户
uid = root
#指定转换用户组身份/备份目录管理用户组
gid = root
##相当于黑洞.出错定位
use chroot = no
##有多少个客户端同时传文件
max connections = 200
##超时时间
timeout = 300
##进程号文件
pid file = /var/run/rsyncd.pid
##控制最大连接数量
lock file = /var/run/rsync.lock
##日志文件
log file = /var/log/rsyncd.log
##模块开始
##这个模块对应的是推送目录
##模块名称随便起
[backup]
##需要同步的目录
path = /backup/
##表示出现错误忽略错误
ignore errors
##表示网络权限可写(本地控制真正可写)
read only = false
##这里设置IP或让不让同步
list = false
##指定允许的网段
hosts allow = 192.168.153.0/24
##拒绝链接的地址,一下表示没有拒绝的链接。
hosts deny = 0.0.0.0/32
##不要动的东西(默认情况)
##虚拟用户
auth users = rsync_backup
##虚拟用户的密码文件
secrets file = /etc/rsync.password
##配置文件的结尾
##rsync_config_______________end
6. Configure el archivo
de contraseña del usuario virtual. Nota: rsync_backup es el usuario virtual y leesir es la contraseña del usuario virtual.
echo "rsync_backup:leesir" >/etc/rsync.password
cat /etc/rsync.password
rsync_backup:leesir
#Elevar los derechos del archivo de contraseña para aumentar la seguridad
chmod 600 /etc/rsync.password
7. Inicie el servicio rsync
rsync --daemon
8. Ver el proceso y el puerto de rsync
[root@slave backup]# ps -ef |grep rsync
root 11119 1 0 14:49 ? 00:00:00 rsync --daemon
root 20861 11105 0 15:30 pts/0 00:00:00 grep --color=auto rsync
[root@slave backup]# netstat -lnutp |grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 11119/rsync
tcp6 0 0 :::873 :::* LISTEN 11119/rsync
inotificar el funcionamiento del servidor de datos B
Prueba de empuje a través de inotify-master
echo "leesir" >/etc/rsync.password
Nota: Simplemente escriba la contraseña aquí, recuerde.
cat /etc/rsync.password
leesir
chmod 600 /etc/rsync.password
echo "hello leesir">test.txt
cat test.txt
hello leesir
IP es la dirección IP del servidor de respaldo
rsync -avz test.txt [email protected]::backup --password-file=/etc/rsync.password
sending incremental file list
inotificar una operación de servidor de respaldo
Compruebe si el archivo se transfiere
ll /backup/
总用量 4
-rw-r--r--. 1 rsync rsync 13 4月 22 14:34 test.txt
cat /backup/test.txt
hello leesir
inotificar el funcionamiento del servidor de datos B
Descargue el paquete fuente inotify, compile e instale
1, instale dependencias
yum -y install gcc gcc-c++ wget
2. Descargue el paquete fuente de inotify
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
3. Descomprimir
tar zxf inotify-tools-3.14.tar.gz
4. Mover y cambiar el nombre
mv inotify-tools-3.14 /usr/local/inotify
cd /usr/local/inotify/
5. Compile e instale
./configure --prefix=/usr/local/inotify&& make && make install
6. Scripting
[root@localhost backup]# cat /usr/local/inotify/scripts/inotify.sh
#!/bin/bash
#para
host01=192.168.153.179 #inotify-slave的ip地址
src=/backup/ #本地监控的目录
dst=backup #inotify-slave的rsync服务的模块名
user=rsync_backup #inotify-slave的rsync服务的虚拟用户
rsync_passfile=/etc/rsync.password #本地调用rsync服务的密码文件
inotify_home=/usr/local/inotify #inotify的安装目录
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
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
# rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0
7. Ejecute el script en segundo plano.
[root@localhost backup]# cd /usr/local/inotify/scripts/
[root@localhost scripts]# pwd
/usr/local/inotify/scripts
[root@localhost scripts]# sh inotify.sh &
[2] 15392
Prueba de sincronización en tiempo real
inotificar el funcionamiento del servidor de datos B
[root@localhost backup]# pwd
/backup
[root@localhost backup]# ll
总用量 0
[root@localhost backup]# touch a
[root@localhost backup]# ll
总用量 0
-rw-r--r--. 1 root root 0 10月 27 15:55 a
inotificar una operación de servidor de respaldo
[root@slave backup]# ll
总用量 0
-rw-r--r--. 1 root root 0 10月 27 15:55 a