Using rsync for data backup and synchronization under UbuntuLinux

Source: Internet
Author: User
Background: the data of 0 servers 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. 1rsync is a good backup tool. the following uses Ubuntu as an example to describe its preparation. 2 suppose

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 portport = 873 logfile = /var/log/rsyncd.logpidfile = /var/run/rsyncd.pid  max connections = 4 auth users = backup, usersecrets 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@#$^&*()googleuser: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.conffunction 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

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.