Build a real-time backup system using Inotify-tools and Rsync

Source: Internet
Author: User
Tags inotify rsync

Monitor file changes with inotifywait

Inotifywait is inotify-tools a tool provided in the package that uses inotify APIs to monitor changes in files/directories.

On ArchLinux, we can use the following command to install

sudo pacman-s--noconfirm inotify-tools

inotifywaitIt usually hangs there until the file/directory has an event to cause concern, it exits and outputs the location where the event occurred, the name of the event, and the file that caused the event (output when the event occurred on the directory).

inotifywaitThe most commonly used options are two, one is -r one, and the other is -e :

-R
Represents an event that occurs in a recursive monitoring subdirectory of a file
-E
Specifies the list of events to monitor. For a backup system, you only need to monitor modify, create, and delete three of events.

For example, we run

Inotifywait-r-E modify,create,delete/tmp

Represents /tmp three events for file modification, file creation, and file deletion in the monitoring directory and its subdirectories.

At this point the program has been suspended state

[Email protected] ~]$ Inotifywait-r-e modify,create,delete/tmpsetting up watches.  Beware:since-r is given, this could take a while! Watches established.

Then /tmp create a new file in the directory

Touch/tmp/newfile

The inotifywait process exits and outputs the following information

/tmp/create NewFile
Synchronizing changes with rsync

Rsync is a fast incremental backup tool. It has several features that make it ideal for use as a backup tool:

    • Incremental backup, only the modified content is transferred
    • Can be decompressed in real-time during transmission, reducing bandwidth consumption
    • Can keep the original file permissions, events, soft and hard links
    • Native replication supported, remote replication supported

The commonly used methods of rsync are:

Rsync-avz--delete  Src/foo:/data

which

-A
Represents archive mode, which is all content in the backup directory (including content in subdirectories), and maintains soft links, file attributes, file modification events, file owner and host information, and synchronizes character/block devices and special files named Sockets and FIFO.
-V
Indicates the details of the output backup
-Z
Indicates compression on transfer
–delete
Delete files that are not in SRC in the backup destination
src/
Indicates that you want to back up all the contents of the SRC directory, note that the last one here is not to be / removed, otherwise the SRC directory itself will be backed up in the past
Foo:/data
Indicates the destination of the
backup is the /data/ directory under the Foo host
Integrate them

And then we just need to use a while dead loop to put together two tools, very simple.

#!/bin/bashif [[$#-ne 2]];then    cat<<eofusage $ (basename $) source_dir [host:]dest_direof    exit] 0fisource_dir=$1dest_dir=$2while:d o    inotifywait-r-e modify,create,delete ${source_dir} && rsync-avz ${ source_dir}/${dest_dir}--deletedone

It is important to note that although inotifywait we can detect the specific changes in the file, we do not really need to know what the specific change is.

We just need to know that something has changed, and then we can change what we have rsync to deal with.

Original address: Https://lujun9972.github.io/blog.

Build a real-time backup system using Inotify-tools and Rsync

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.