Use rsync in Ubuntu Linux for data backup and Synchronization

Source: Internet
Author: User
Tags inotify

Background:

0. server data is very important. If the disk is damaged, it will be troublesome. As a manager, it is necessary to synchronize and back up data in real time.

1. rsync is a good backup tool. The following uses Ubuntu as an example to describe its preparation.

2. If the ip address of my server A is 10.141.247.12, the ip address of my backup client B is 10.141.247.13

Server preparation:

1. Enable rsync as the server and modify the following line in the/etc/default/rsync file (for the entire file, see the link)

RSYNC_ENABLE = true

2. Create the configuration file/etc/rsyncd. conf as follows (or see the link)

# [Globale]
Strict modes = yes
# Rsync default port
Port = 873
Logfile =/var/log/rsyncd. log
Pidfile =/var/run/rsyncd. pid
Max connections = 4
Auth users = backup, user
Secrets file =/etc/rsyncd. scrt

# [Modules] each path responding to a module
[Appbackup]
Path =/home/aborn/backup
# Hosts allow = 9.4.122.24

[Databackup]
Path =/home/data

Note: a) the auth users configuration must be consistent with the username in/etc/rsyncd. scrt, but not necessarily in the system.

 

B) each path to be backed up is a module. The two paths correspond to [appbackup] and [databackup] respectively.

3. Create a new password file/etc/rsyncd. scrt. The corresponding content is as follows:

Backup: configurebackup @ # $ ^ & * () google
User: passwordpassword

Here there are two users: backup and user. The colon corresponds to the password. Note that the attribute of this file is 600 (other users do not have the read/write execution permission)

 

4. Enable the backup service. After opening an account, use netstat-tupln to check whether port 873 is enabled. If yes, the backup service is successfully enabled)

Sudo/etc/init. d/rsync start

Client preparation

 

1. Assume that the current working directory is ~ /Backup

2. Create the password file rsyncd. scrt in the current working directory. The content is the same as that on the server side and the attribute is 600.

3. Create the configuration file client. conf in the current working directory. The content is as follows:

BACKUPPATH = "/home/aborn/backup /";
SERVERIP = "10.141.247.12"
MODULE = "appbackup databackup"
# OPTIONS = "-vazu -- progress -- delete"
OPTIONS = "-vazu -- progress"

Description: BACKUPPATH is the data storage path of the client.

 

SERVERIP is the IP address of server.

MODULE is the module corresponding to/etc/rsyncd. conf on the server. Multiple modules are separated by spaces.

4. Run the backup script rsyncclient. sh with the following content:

#! /Bin/bash
######################################## ##########################
# NAME
# Rsyncclient. sh ---- running in client machine, which
# Is used to backup data in client machine
#
# USAGE
#./Rsyncclient. sh
#
# AUTHOR
# Aborn Jiang (aborn.jiang@gmail.com)
#
# NOTE
# Pls configure the file client. conf and rsyncd. scrt
#
######################################## ##########################
ABSPATH = $ (dirname $0)
Source $ {ABSPATH}/client. conf

Function get-user-pwd ()
{
# Obtain usrname and password
IUSR = $ (cat $ {ABSPATH}/rsyncd. scrt | tr-d ''| grep-v" ^ $ "| \
Grep-v "^ #" | head-n 1 | awk-F: '{print $1 }')
IPWD = $ (cat $ {ABSPATH}/rsyncd. scrt | tr-d ''| grep-v" ^ $ "| \
Grep-v "^ #" | head-n 1 | awk-F: '{print $2 }')
If [-z $ {iUSR}] | [-z $ {iPWD}]; then
Echo "iUSR = $ iUSR iPWD = $ iPWD"
Echo "rsyncd. scrt format illegal, please check! ";
Exit-1;
Fi

# Produce password file
Echo "$ iPWD" >$ {ABSPATH}/. pass
Chmod 600 $ {ABSPATH}/. pass
[! -D $ BACKUPPATH] & mkdir-p $ {BACKUPPATH}
}


Function backup-module ()
{
# Print key information
IModule = $1
Echo
Echo "---------------------------------------------------"
Echo "---- backup module $ {iModule }@$ {SERVERIP} begin"
Echo "---- TIME = 'date '----"
Echo "ABSPATH =$ {ABSPATH }"
Echo "BACKUPPATH =$ {BACKUPPATH }"
Echo "iUSR = $ iUSR iPWD = $ iPWD"
Echo "OPTIONS =$ {OPTIONS }"
IModuleBackpath =$ {BACKUPPATH}/$ {iModule };
[! -D $ {iModuleBackpath}] & mkdir-p $ {iModuleBackpath}

# Begin backup
Rsync $ {OPTIONS }$ {iUSR }@$ {SERVERIP }::: {iModule }$ {iModuleBackpath }\
-- Password-file =$ {ABSPATH}/. pass
If [$? ! = 0]; then
Echo "---- backup module $ {iModule }@$ {SERVERIP} failed ."
Else
Echo "---- backup module $ {iModule }@$ {SERVERIP} succuess ."
Fi
Echo "---- TIME = 'date '----"
Echo "---------------------------------------------------"
Echo
}

Function _ main __()
{
Get-user-pwd
For md in $ MODULE
Do
Backup-module $ md
Done
}

_ Main __

NOTE: For the entire project file, see my GitHub link configure. rsync.

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

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.