Get the App
SLTechnology News&Howtos  ›  Servers  › 

Rsync+inotify script Note the port number of the-e option

Shulou Source: shulou.com Published: 2022-06-02 22:19:57 12月01日 Update

#! / bin/bash

Src=/apps/dockerfiles/lnmp/nginx/html/aiitle/aiitle_backend/public/uploads/ # Source path that requires synchronization

The name of rsync-daemon published on the target server of des=upload/ # will not be introduced here by rsync-daemon. It is relatively simple to search it on the Internet.

Rsync_passwd_file=/etc/rsyncd.passwd # password file verified by rsync

Ip1=192.168.8.10 # Target Server 1

# ip2=192.168.0.19 # Target Server 2

User=root # rsync-the authenticated user name defined by daemon

Cd ${src} # in this method, because of the feature of rsync synchronization, you must first cd to the source directory before inotify listens. / before the directory structure is consistent after rsync synchronization, interested students can enter

Try to watch its effect.

/ usr/local/bin/inotifywait-mrq-- format'% Xe% w% f'-e modify,create,delete,attrib,close_write,move. / | while read file # monitors the "File path > path list" loop where changes have occurred

Do

INO_EVENT=$ (echo $file | awk'{print $1}') # splits the inotify output and assigns the event type to INO_EVENT

INO_FILE=$ (echo $file | awk'{print $2}') # cuts the inotify output and assigns the file path portion to INO_FILE

Echo "--$(date)--" > > / var/log/rsync.log 2 > & 1

Echo $file > > / var/log/rsync.log 2 > & 1

# add, modify, write complete, move into events

# add and change to the same judgment, because they are definitely aimed at the operation of files. Even if you create a new directory, only an empty directory will be synchronized, which will not affect the speed.

If [[$INO_EVENT = ~ 'CREATE']] | | [$INO_EVENT = ~' MODIFY']] | | [$INO_EVENT = ~ 'CLOSE_WRITE']] | | [[$INO_EVENT = ~' MOVED_TO']] # judge the type of event > case

Then

Echo 'CREATE or MODIFY or CLOSE_WRITE or MOVED_TO' > > / var/log/rsync.log 2 > & 1

Rsync-avzcR-e 'ssh-p 10222'-password-file=$ {rsync_passwd_file} $(dirname ${INO_FILE}) ${user} @ ${ip1}:: ${des} & & # INO_FILE variable represents the path

Path oh-c check the contents of the file

Rsync-avzcR-e 'ssh-p 10222'-- password-file=$ {rsync_passwd_file} $(dirname ${INO_FILE}) ${user} @ ${ip2}:: ${des} > > / var/log/rsync.log 2 > & 1

# take a closer look at the rsync synchronization command source above using the $(dirname ${INO_FILE}) variable, that is, only synchronizing the directory of the changed files each time (the method of synchronizing only the target files will leak the files in some extreme > end environments of the production environment. Now you can balance the files without leaking files) and then use the-R parameter to pass the directory structure of the source. Return to the target to ensure the consistency of the directory structure

Fi

# Delete and move out events

If [[$INO_EVENT = ~ 'DELETE']] | | [$INO_EVENT = ~' MOVED_FROM']]

Then

Echo 'DELETE or MOVED_FROM' > > / var/log/rsync.log 2 > & 1

Rsync-avzR-e 'ssh-p 10222'-delete-- password-file=$ {rsync_passwd_file} $(dirname ${INO_FILE}) ${user} @ ${ip1}:: ${des} & &

Rsync-avzR-e 'ssh-p 10222'-delete-- password-file=$ {rsync_passwd_file} $(dirname ${INO_FILE}) ${user} @ ${ip2}:: ${des} > > / var/log/rsync.log 2 > & 1

# look at the rsync command, if you directly synchronize the deleted path ${INO_FILE}, you will report a no such or directory error, so the source of synchronization here is the path above the deleted file or directory, and add-- delete to delete files that are on the destination but not in the source. The specified file cannot be deleted here. If the deleted path is closer to the root, the directory will be synchronized for more months, and the synchronous deletion operation will take more time. Here is a better way for students, welcome to communicate.

Fi

# modify attribute event refers to touch chgrp chmod chown and other operations

If [[$INO_EVENT = ~ 'ATTRIB']]

Then

Echo 'ATTRIB' > > / var/log/rsync.log 2 > & 1

If [!-d "$INO_FILE"] # if you modify the property of a directory, it is not synchronized, because the synchronized directory will be scanned recursively, and when the files in this directory are synchronized, rsync will update accordingly

This directory.

Then

Rsync-avzcR-e'ssh-p 10222'-- password-file=$ {rsync_passwd_file} $(dirname ${INO_FILE}) ${user} @ ${ip1}:: ${des} & &

Rsync-avzcR-e 'ssh-p 10222'-- password-file=$ {rsync_passwd_file} $(dirname ${INO_FILE}) ${user} @ ${ip2}:: ${des} > > / var/log/rsync.log 2 > & 1

Fi

Fi

Done

Tags: Synchronization file directory target path event server structure service consistency variable classmate command property method environment type speed part recursion Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology OPPO Reno Docker Huawei