File sync rsync + ssh + inotify + unison implementation in Linux

Source: Internet
Author: User
Tags inotify time 0 ocaml

1. Use rsync + ssh software for regular file backup and Incremental backup, but not for real-time backup, so the effect is not very good.
Server A (ip: 2.2.2.135 ):
[Root @ CentOS ~] # Ifconfig eth0 -- View IP addresses
Eth0 Link encap: Ethernet HWaddr 08: 00: 27: 56: AA
Inet addr: 2.2.2.135 Bcast: 2.20.255.255 Mask: 255.0.0.0
Inet6 addr: fe80: a00: 27ff: fe56: aaaa/64 Scope: Link
Up broadcast running multicast mtu: 1500 Metric: 1
RX packets: 13072 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 328 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 FIG: 1000
RX bytes: 1009592 (985.9 KiB) TX bytes: 31733 (30.9 KiB)
[Root @ centos ~] # Vim/etc/hosts -- Add the following lines
2.2.2.145 centos1
2.2.2.135 centos
[Root @ centos ~] # Yum install rsync -- install the rsync software. Remember to install all the two computers.
[Root @ centos ~] # Ssh-keygen-t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/. ssh/id_rsa ):
/Root/. ssh/id_rsa already exists.
Overwrite (y/n )? Y
Enter passphrase (empty for no passphrase ):
Enter same passphrase again:
Your identification has been saved in/root/. ssh/id_rsa.
Your public key has been saved in/root/. ssh/id_rsa.pub.
The key fingerprint is:
12: c6: 7b: 6a: 12: 4a: 15: c8: 8b: 0f: 90: 54: 02: fa: 08: 27 root @ centos
The key's randomart image is:
+ -- [RSA 2048] ---- +
| + =. O. |
| ++ O |
| E... + |
| + =... O |
|. Oo. o S |
|... + |
|. O |
| O |
|
+ ----------------- +
[Root @ centos ~] # Scp ~ /. Ssh/id_rsa.pub 2.2.2.145:/root/. ssh/authorized_keys -- put the public key on another server
[Root @ centos ~] # Ssh 2.2.2.145 -- no password for login
Last login: Tue Dec 17 09:46:05 2013 from centos
[Root @ centos1 ~] #

Server B (ip: 2.2.2.145 ):
[Root @ centos1 ~] $ Ifconfig eth0 -- View IP Address
Eth0 Link encap: Ethernet HWaddr 08: 00: 27: 56: AA
Inet addr: 2.2.2.145 Bcast: 2.2.2.255 Mask: 255.255.255.0
Inet6 addr: fe80: a00: 27ff: fe56: aaaa/64 Scope: Link
Up broadcast running multicast mtu: 1500 Metric: 1
RX packets: 14005 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 503 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 FIG: 1000
RX bytes: 1081748 (1.0 MiB) TX bytes: 46832 (45.7 KiB)
[Root @ centos1 ~] $ Vim/etc/hosts -- Add the following two lines
2.2.2.135 centos
2.2.2.145 centos1
[Root @ centos1 ~] $ Ping 2.2.2.135-c 1 -- connect
PING 2.2.2.135 (2.2.2.135) 56 (84) bytes of data.
64 bytes from 2.2.2.135: icmp_seq = 1 ttl = 64 time = 0.438 MS
--- 2.2.2.135 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0 ms
Rtt min/avg/max/mdev = 0.438/0.438/0.438/0.000 MS
[Root @ centos ~] # Yum install rsync
PING 2.2.2.135 (2.2.2.135) 56 (84) bytes of data.
64 bytes from 2.2.2.135: icmp_seq = 1 ttl = 64 time = 0.438 MS
--- 2.2.2.135 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0 ms
Rtt min/avg/max/mdev = 0.438/0.438/0.438/0.000 MS
[Root @ centos ~] # Yum install rsync -- install software
[Root @ centos1 ~] # Ssh-keygen-t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/. ssh/id_rsa ):
/Root/. ssh/id_rsa already exists.
Overwrite (y/n )? Y
Enter passphrase (empty for no passphrase ):
Enter same passphrase again:
Your identification has been saved in/root/. ssh/id_rsa.
Your public key has been saved in/root/. ssh/id_rsa.pub.
The key fingerprint is:
4b: 74: 44: ca: 38: 50: 17: 25: 3a: 1e: b0: ac: a4: 7d: f3: be root @ centos1
The key's randomart image is:
+ -- [RSA 2048] ---- +
| O... ++ |
|. + = + |
|. O * +. |
| +... =. |
|. O. S |
|. O... |
|... |
|. |
| E. |
+ ----------------- +
[Root @ centos1 ~] # Scp ~ /. Ssh/id_rsa.pub 2.2.2.135:/root/. ssh/authorized_keys
[Root @ centos1 ~] $ Rsync 2.2.2.135:/home/*. txt/home/-- remotely copy the file.-r is the directory.
[Root @ centos1 ~] $ Ll/home/*. txt -- copied successfully
-Rw-r -- 1 root 36 Dec 16 14:40/home/1.txt
-Rw-r -- 1 root 12 Dec 16 14:34/home/2.txt
-Rw-r -- 1 root 0 Dec 16 15:48/home/3.txt
[Root @ centos1 ~] $ Crontab-e
* 24 ***/usr/bin/rsync 2.2.2.135:/home/*. txt/home/-- backup at 24 o'clock every day

2. rsync + ssh + inotify for real-time file backup (inotify File System Monitoring)
Server A (ip: 2.2.2.135): Backup File

[Root @ centos1 samba] $ tar xvf inotify-tools-3.14.tar.gz
[Root @ centos1 samba] $ cd inotify-tools-3.14
[Root @ centos1 inotify-tools-3.14] $./configure -- prefix =/usr/local/inotify & make install -- install software
[Root @ centos inotify-tools-3.14] # cd/usr/local/inotify/
[Root @ centos inotify] # ll
Total 16
Drwxr-xr-x. 2 root 4096 Dec 16 16:08 bin
Drwxr-xr-x. 3 root 4096 Dec 16 16:08 include
Drwxr-xr-x. 2 root 4096 Dec 16 16:08 lib
Drwxr-xr-x. 4 root 4096 Dec 16 16:08 share
[Root @ centos inotify] # cd bin/
[Root @ centos bin] # ll
Total 72
-Rwxr-xr-x. 1 root 36447 Dec 16 16:08 inotifywait
-Rwxr-xr-x. 1 root 34961 Dec 16 16:08 inotifywatch
[Root @ centos bin] # vim start. sh
#! /Bin/sh
SRC =/home/
DST =/home/
IP = 2.2.2.145
/Usr/local/inotify/bin/inotifywait-mrq-e modify, delete, create, attrib $ {SRC} | while read status
Do
/Usr/bin/rsync-auvg -- delete $ DST $ IP: $ SRC & -- run in the background
If (echo "$? "=" 0 "); then
Echo 'date + % d': back OK>/home/1.log -- there is a single quotation mark before the date
Else
Echo 'date + % d': error>/home/1.log
Done
[Root @ centos1 bin] # chmod o + x start. sh
[Root @ centos1 bin] #./start. sh &
[2] 2915
[Root @ centos1 bin] # ll/home/1
Total 8
-Rw-r -- 1 root 36 Dec 16 14:40 1.txt
-Rw-r -- 1 root 12 Dec 16 14:34 2.txt
-Rw-r -- 1 root 0 Dec 17 10:58 3.txt
[Root @ centos1 bin] # mkdir/home/1/{1, 2, 3}
[Root @ centos1 bin] # ll/home/1
Total 20
Drwxr-xr-x 2 root 4096 Dec 17 :31 1
-Rw-r -- 1 root 36 Dec 16 14:40 1.txt
Drwxr-xr-x 2 root 4096 Dec 17 11: 31 2
-Rw-r -- 1 root 12 Dec 16 14:34 2.txt
Drwxr-xr-x 2 root 4096 Dec 17 11: 31 3
-Rw-r -- 1 root 0 Dec 17 10:58 3.txt
[Root @ centos1 bin] # cat/home/1.log -- logs are recorded and succeeded.
: Back OK
: Back OK
: Back OK
: Back OK
[Root @ centos1 bin] # -- go to 2.2.2.145 to check whether the file is synchronized.

Server B (ip: 2.2.2.145): used to read files
[Root @ centos1 1] # ifconfig eth0
Eth0 Link encap: Ethernet HWaddr 08: 00: 27: 56: AA
Inet addr: 2.2.2.145 Bcast: 2.2.2.255 Mask: 255.255.255.0
Inet6 addr: fe80: a00: 27ff: fe56: aaaa/64 Scope: Link
Up broadcast running multicast mtu: 1500 Metric: 1
RX packets: 38855 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 12226 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 FIG: 1000
RX bytes: 3390394 (3.2 MiB) TX bytes: 1557639 (1.4 MiB)
[Root @ centos1 1] # ll/home/1 -- server B can see
Total 20
Drwxr-xr-x 2 root 4096 Dec 17 :31 1
-Rw-r -- 1 root 36 Dec 16 14:40 1.txt
Drwxr-xr-x 2 root 4096 Dec 17 11: 31 2
-Rw-r -- 1 root 12 Dec 16 14:34 2.txt
Drwxr-xr-x 2 root 4096 Dec 17 11: 31 3
-Rw-r -- 1 root 0 Dec 17 10:58 3.txt

3. Two-way synchronization of unison + ssh files, unable to achieve real-time two-way Synchronization
1) install the unison Software
[Root @ centos1 samba] # tar xvf ocaml-3.12.1.tar.gz
[Root @ centos1 samba] # cd ocaml-3.12.1
[Root @ centos1 ocaml-4.01.0] #./configure & make install
[Root @ centos1 ocaml-4.01.0] # cd ..
[Root @ centos1 samba] # tar xvf unison-2.40.63.tar.gz
[Root @ centos1 samba] # cd unison-2.40.63
[Root @ centos1 unison-2.40.63] # make UISTYLE = text
[Root @ centos1 unison-2.40.63] # mkdir/root/bin
[Root @ centos1 unison-2.40.63] # make install
[Root @ centos1 unison-2.40.63] # cp/root/bin/unison/usr/bin

2) password-less ssh Login (Server 1, Server 2, and password-less)
[Root @ centos home] # ssh-keygen-t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/. ssh/id_rsa ):
/Root/. ssh/id_rsa already exists.
Overwrite (y/n )? Y
Enter passphrase (empty for no passphrase ):
Enter same passphrase again:
Your identification has been saved in/root/. ssh/id_rsa.
Your public key has been saved in/root/. ssh/id_rsa.pub.
The key fingerprint is:
E3: 70: 96: 80: 20: 8b: e8: a9: 6f: 7a: 00: 64: 5f: 8b: 86: 90 root @ centos
The key's randomart image is:
+ -- [RSA 2048] ---- +
|... |
| E =. o |
| B o + o |
| O... +... |
|. O. S |
| O =. |
|... |
|. O |
|. +. |
+ ----------------- +
[Root @ centos home] # scp ~ /. Ssh/id_rsa.pub 2.2.2.145:/root/. ssh/authorized_keys
Root@2.2.2.145's password:
Id_rsa.pub 100% 393 0.4KB/s
[Root @ centos home] # ssh 2.2.2.145 -- success, no password required
Last login: Tue Dec 17 13:25:15 2013 from centos
[Root @ centos1 ~] #

3) Server 2 Login Server 1 No Password
[Root @ centos1 unison-2.40.63] # ssh-keygen-t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/. ssh/id_rsa ):
/Root/. ssh/id_rsa already exists.
Overwrite (y/n )? Y
Enter passphrase (empty for no passphrase ):
Enter same passphrase again:
Your identification has been saved in/root/. ssh/id_rsa.
Your public key has been saved in/root/. ssh/id_rsa.pub.
The key fingerprint is:
Be: 43: 3a: 24: 4c: 83: fe: bd: 22: e2: ed: c2: 89: 7e: AB: c9 root @ centos1
The key's randomart image is:
+ -- [RSA 2048] ---- +
|
|
|. |
|. O |
|. O. S |
|. O... |
| O... + o. |
| ++. O +... |
| OE =... o... |
+ ----------------- +
[Root @ centos1 unison-2.40.63] # scp ~ /. Ssh/id_rsa.pub 2.2.2.135:/root/. ssh/authorized_keys
[Root @ centos1 unison-2.40.63] # ssh 2.2.2.135
Last login: Tue Dec 17 13:26:06 2013 from centos1
[Root @ centos ~] #

4) Server 1 and 2 file synchronization
[Root @ centos1 bin] # unison/home ssh: // root@2.2.2.145:/home -- file sync
[Root @ centos1 bin] # unison/home ssh: // root@2.2.2.145:/home -- path directory -- synchronize only the specified directory

Rsync details: click here
Rsync: click here

Recommended reading:

Monitor host files and directories using inotifywait

Using inotify + rsync for Linux File batch update

Inotify-tools + rsync real-time file synchronization installation and configuration

Complete rsync synchronization Configuration

Related Article

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.