Rsync+inotify Real-time monitoring backup

Source: Internet
Author: User
Tags inotify rsync


Note that the following inotify is built on the rsync configuration process

The premise is that the Rsync daemon is configured successfully, rsync configuration looks at the blog post, the client can pull the data, and then to configure the INotify service----inotify is installed on the client, listening to the directory that needs to be backed up, and then pushed to the server

See if the current system supports inotify

[Email protected] bier]# uname-r

2.6.32-431.el6.i686

[Email protected] bier]# ls-l/proc/sys/fs/inotify/

Xüóá0

-rw-r--r--1 root root 0 22:30 max_queued_events

-rw-r--r--1 root root 0 22:30 max_user_instances

-rw-r--r--1 root root 0 22:30 max_user_watches

#显示着三个文件证明支持


First, the installation of INotify services

1) Download INotify source Package

[Email protected] src]# CD/USR/LOCAL/SRC

Tip: Download Inotify-tools

Inotify-tools-3.14.tar.gz


2) Compile and install INotify

[Email protected] src]# TAR-XVF inotify-tools-3.14.tar.gz

[Email protected] src]# CD inotify-tools-3.14

[Email protected] inotify-tools-3.14]#/configure--prefix=/usr/local/inotify-tools-3.14

Make&make Install


[[email protected] local]# ls

Bin Games Lib Python share

etc include inotify-tools-3.14 libexec sbin src

[Email protected] local]# CD inotify-tools-3.14/


[email protected] inotify-tools-3.14]# LL

Total 16

Drwxr-xr-x. 2 root root 4096 03:21 bin = = "INotify Execute command

Drwxr-xr-x. 3 root root 4096 03:21 include = = "header file

Drwxr-xr-x. 2 root root 4096 03:21 lib = = "Dynamic link library file

Drwxr-xr-x. 4 root root 4096 03:21 share = = "Help document


INotify Common Parameters Command detailed

[Email protected] inotify-tools-3.14]#./bin/inotifywait–help



Second, test inotify

Test the Create File

[Email protected] local]#/usr/local/inotify-tools-3.14/bin/inotifywait-mrq--timefmt '%d/%m/%y%h:%m '--format '%T%w %f '-e create/data

10/11/15 03:35/data/backup.bat


Tip: This command is a inotify monitored command, M is continuous listening, R is recursive, Q is only the print event, followed by the time format

-E is the specified time to listen, and data is the monitored directory



Also open a new window in the data directory to create a file touch Backup.bat found above the monitoring of the backup directory changed


The following actions need to open another window to create, delete, modify the file test


Test Delete

[Email protected] local]#/usr/local/inotify-tools-3.14/bin/inotifywait-mrq--timefmt '%d/%m/%y%h:%m '--format '%T%w %f '-e delete/data


Test Close_write (Write a file to listen, that is, the prompt after writing)

[Email protected] ~]$/usr/local/inotify-tools-3.14/bin/inotifywait-mrq--timefmt '%d/%m/%y%h:%m '--format '%T%w%f '- E Close_write/data

10/11/15 04:04/data/aa.log


Remove the time format display to simplify

[Email protected] ~]$/usr/local/inotify-tools-3.14/bin/inotifywait–mrq--format '%w%f '-e cteate/data



Three, simple monitoring backup script

Vim backup.sh

Cmd= "/usr/local/inotify-tools-3.14/bin/inotifywait"

$cmd-mrq--format '%w%f '-e create,close_write,delete/data|\

While Read line

Do

Rsync-az $line [Email protected]::bier--password-file=/etc/rsync.password

Done


Give the line a row at the back of the pipeline, because if the backup changes is a path, the above experiment


Hint: Listen to the backup script in real time, for the files in the backup directory, this script can not be deleted, because he is not for the directory backup



Improved backup.sh

Vim backup1.sh

Cmd= "/usr/local/inotify-tools-3.14/bin/inotifywait"

$cmd-mrq--format '%w%f '-e create,close_write,delete/data|\

While Read line

Do

[!-e "$line"] && continue

Rsync-az--delete $line [email protected]::bier--password-file=/etc/rsync.password

Done


When the file does not exist, it continue and avoids errors, so this script can be used to create a simple file backup using


Four, according to the directory real-time monitoring backup

host=192.168.3.188

Src=/data

Dst=bier

User=rsync_backup

Rsync_passfile=/etc/rsync.password

inotify_home=/usr/local/inotify-tools-3.14/


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--format '%w%f '-e create,close_write,delete,attrib/$src |\

While Read line

Do

CD $src && rsync-aruz-r--delete/--timeout=100 [email protected] $host:: $dst--password-file=${rsync_passfile } >/dev/null 2>&1

Done

Exit 0


Increase key parameters

[Email protected] scripts]# cat/proc/sys/fs/inotify/max_user_watches

8192

[Email protected] scripts]# cat/proc/sys/fs/inotify/max_queued_events

16384



This article is from the "Bill Linux Operations Notes" blog, please be sure to keep this source http://chenshoubiao.blog.51cto.com/6159058/1881341

Rsync+inotify Real-time monitoring backup

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.