Rsync + sersync for real-time file backup

Source: Internet
Author: User
Tags inotify custom name

Rsync + sersync for real-time file backup

Part 1: operate on the target server 192.168.0.217
1. Install Rsync on the OA file backup server
1. Disable SELINUX
Vi/etc/selinux/config # edit the firewall configuration file
The Code is as follows:

# SELINUX = enforcing # comment out
# SELINUXTYPE = targeted # comment out
SELINUX = disabled # Add
: Wq! # Save and exit

Setenforce 0 # effective immediately

2. Enable tcp port 873 of the firewall (default Rsync port)

3. Install Rsync server software
Yum install rsync xinetd # Installation
Vi/etc/xinetd. d/rsync # edit the configuration file and set rsync to start upon startup (version 7 servers are not configured)

The Code is as follows:

Service rsync

{

Disable = no

Flags = IPv6

Socket_type = stream

Wait = no

User = root

Server =/usr/bin/rsync

Server_args = -- daemo
N
Log_on_failure + = USERID

}

: Wq! # Save and exit

/Etc/init. d/xinetd start # start (in CentOS, xinetd is used to manage Rsync Services)

4. Configure rsync. The rsync configuration file is/etc/rsyncd. conf. The configuration is as follows:
5.
Vi/etc/rsyncd. conf # create a configuration file and add the following code

The Code is as follows:

Log file =/var/log/rsyncd. log # log file location. This file is automatically generated after rsync is started. You do not need to create it in advance.
Pidfile =/var/run/rsyncd. pid # storage location of the pid File
Lock file =/var/run/rsync. lock # lock files supporting the max connections Parameter
Secrets file =/etc/rsync. pass # The user authentication configuration file, which stores the user name and password. This file will be created later.
Motd file =/etc/rsyncd. Motd # Welcome to the file location on the page when rsync is started (custom file content)
Uid = root # Set rsync operation permission to root
Gid = root # Set rsync operation permission to root
Port = 873 # default port
Use chroot = no # The default value is true. Change it to no to add a backup for the soft connection to the directory file.
[Seeyon] # custom name
Path =/home/Seeyon/# rsync server data directory path
Comment = Seeyon # The Module name is the same as the custom name of [Seeyon]
Ignore errors # ignore errors

Read only = no # Set the rsync server file to read and write permissions
List = no # the rsync server resource list is not displayed.
Max connections = 200 # maximum number of connections
Timeout = 600 # Set the timeout value
Auth users = root # user name for data synchronization. Multiple user names can be set and separated by commas (,).
Hosts allow = * # IP address of the client that allows data synchronization. Multiple IP addresses can be set and separated by commas (*) (representing any address)
# Hosts deny = X. X # IP addresses of clients that disable data synchronization. You can set multiple IP addresses separated by commas (,).

: Wq! # Save and exit

Note: secrets file is the password file configured for synchronization. [Seeyon] This is the name of the synchronization module, path is the directory for synchronization configuration, hosts allow is the host that allows synchronization, hosts deny: the host that rejects Synchronization

5. Create a synchronized user and password file, which is included in the configuration option "secrets file" in step 1. /Etc/rsync. passwd. Set the permission of this file to 600

Echo "root: baidu. c0m">/etc/rsync. pass
Chmod 600/etc/rsync. pass

6. Create a synchronization Directory: the directory in the path configuration option in step 1.
Mkdir/home/Seeyon

7. Start rsync
Copy code
Rsync -- daemon -- config =/etc/rsyncd. conf (run rsync in the background)
Restart xinetd.

Systemctl restart xinetd (restart)
Systemctl stop xinetd (stop)
Systemctl start xinetd (start)

8. Check whether the startup is successful.
Netstat-anutp | grep 873
If the following data is displayed, the startup is successful:
Rsync + sersync for real-time file backup

9. Set startup
(Method 1)
Echo "rsync -- daemon -- config =/etc/rsyncd. conf" & gt;/etc/rc. d/rc. local

(Method 2)
/Usr/bin/rsync-daemon-config =/etc/rsyncd. conf

Part 2: operate on the source server 192.168.0.218

1. Install the Rsync Client

1. Disable SELINUX
Vi/etc/selinux/config # edit the firewall configuration file

The Code is as follows:

# SELINUX = enforcing # comment out
# SELINUXTYPE = targeted # comment out
SELINUX = disabled # Add
: Wq! Save and exit

Setenforce 0 takes effect immediately

2. Enable tcp port 873 of the firewall (Rsync default port, which can be used as the Rsync of the client without enabling port 873)

3. Install the Rsync client software

The Code is as follows:

Whereis rsync # Check whether rsync has been installed in the system. The following prompt is displayed, indicating that rsync has been installed.
Rsync:/usr/bin/rsync/usr/share/man/man1/rsync.1.gz

Yum install xinetd # Only install xinetd. In RHEL7, xinetd is used to manage the rsync service.

Yum install rsync xinetd # If no rsync exists by default, run this command to install rsync and xinetd

Vi/etc/xinetd. d/rsync # edit the configuration file and set rsync to start at startup

The Code is as follows:

Service rsync

{

Disable = no

Flags = IPv6

Socket_type = stream

Wait = no

User = root

Server =/usr/bin/rsync

Server_args = -- daemo
N
Log_on_failure + = USERID

}

3.1 # Start (xinetd is used in RHEL7 to manage the rsync Service)
Systemctl start xinetd

4. Create a password file, which is the same as the backup server of the OA file. However, you only need to save a password for this file. Without the user name, the permission is 600.
Vi/etc/rsync. passwd # edit the file and add the following content:

The Code is as follows:

Baidu. c0m # Password
: Wq! Save and exit
Chmod 600/etc/rsync. passwd # Set the File Permission. Only set the file owner to have the read and write permissions.

5. test whether the OA Backup Server can be connected
Rsync-a Seeyon@192.168.0.217: (if the result is successful)

Rsync + sersync for real-time file backup

Ii. Install sersync to trigger rsync in real time for synchronization
1. Check whether the server kernel supports inotify.
Ll/proc/sys/fs/inotify # list the file directories. The following content indicates that the server kernel supports inotify (no need to copy the code for production)

The Code is as follows:
-Rw-r -- 1 root 0 Mar 7 0:17 max_queued_events
-Rw-r -- 1 root 0 Mar 7 max_user_instances
-Rw-r -- 1 root 0 Mar 7 max_user_watches
Note: in Linux, the minimum kernel that supports inotify is 2.6.13. You can run the following command: uname-a to view the kernel:
CentOS 5.X kernel 2.6.18. inotify is supported by default.

2. Modify the default inotify parameter (the default inotify Kernel Parameter Value is too small)

View the default system parameter values:
Sysctl-a | grep max_queued_events
The result is: fs. inotify. max_queued_events = 16384.
Sysctl-a | grep max_user_watches
The result is: fs. inotify. max_user_watches = 8192.
Sysctl-a | grep max_user_instances
Result: fs. inotify. max_user_instances = 128

Modify parameters:
The Code is as follows:

Sysctl-w fs. inotify. max_queued_events = "99999999"
Sysctl-w fs. inotify. max_user_watches = "99999999"
Sysctl-w fs. inotify. max_user_instances = "65535"

Parameter description:
Max_queued_events:
Maximum inotify Queue length. If the value is too small, the "Event Queue Overflow" error will occur, resulting in inaccurate monitoring files.
Max_user_watches:
For how many directories are contained in the file to be synchronized, use: find/home/Seeyon-type d | wc-l for statistics, make sure that the max_user_watches value is greater than the statistical result (here/home/Seeyon is the synchronization file directory)
Max_user_instances:
Maximum Value of inotify instance created by each user

3. Install sersync
Click sersync here.

Mkdir/usr/local/sersync
Mkdir/usr/local/sersync/conf
Mkdir/usr/local/sersync/bin
Mkdir/usr/local/sersync/log
Tar zxvf sersync2.5_32bit_binary_stable_final.tar.gz
Cd GNU-Linux-x86/
Cp confxml. xml/usr/local/sersync/conf
Cp sersync2/usr/local/sersync/bin

4. Configure sersync

The Code is as follows:
Edit vi confxml. xml and modify the following code.

The Code is as follows:

<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<Head version = "2.5">
# Set the local IP address and port
<Host hostip = "localhost" port = "8008"> # Enable DUBUG Mode
<Debug start = "false"/>
# Enable the xfs File System
<FileSystem xfs = "false"/>
# Ignore the pushed files (regular expressions) during synchronization, Which is disabled by default.
<Filter start = "false">
<Exclude expression = "(. *) \. svn"> </exclude>
<Exclude expression = "(. *) \. gz"> </exclude>
<Exclude expression = "^ info/*"> </exclude>
<Exclude expression = "^ static/*"> </exclude>
</Filter>
<Inotify>
# Set the event to be monitored
<Delete start = "true"/>
<CreateFolder start = "true"/>
<CreateFile start = "true"/>
<CloseWrite start = "true"/>
<MoveFrom start = "true"/>
<MoveTo start = "true"/>
<Attrib start = "true"/>
<Modify start = "true"/>
</Inotify>
<Sersync>
# Directory path for local Synchronization
<Localpath watch = "/home/Seeyon">
# Remote IP address and rsync Module name
<Remote ip = "192.168.0.217" name = "Seeyon"/>
<! -- <Remote ip = "192.168.8.39" name = "tongbu"/> -->
<! -- <Remote ip = "192.168.8.40" name = "tongbu"/> -->
</Localpath>
<Rsync>
# Rsync Command Parameters
<CommonParams params = "-auvzP"/>
# Rsync synchronous Authentication
<Auth start = "true" users = "root" passwordfile = "/etc/rsync. passwd"/>
# Set the remote service port of rsync. If the remote port is not the default port, you must enable the custom port.
<UserDefinedPort start = "false" port = "873" type = "codeph" text = "codeph"/> <! -- Port = 873 -->
# Set timeout
<Timeout start = "true" time = "100"/> <! -- Timeout = 100 -->
# Set rsync + ssh encrypted transmission mode, which is disabled by default. To enable this mode, you must set an SSH encrypted certificate.
<Ssh start = "false"/>
</Rsync>
# Path of the script for failed sersync transmission. The script will be re-executed every 60 days. After the script is executed, it will be automatically cleared.
<FailLog path = "/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute = "60"/> <! -- Default every 60 mins execute once -->
# Set rsync + crontab for scheduled transmission, which is disabled by default
<Crontab start = "false" schedule = "600"> <! -- 600mins -->
<Crontabfilter start = "false">
<Exclude expression = "*. php"> </exclude>
<Exclude expression = "info/*"> </exclude>
</Crontabfilter>
</Crontab>
# Call the plug-in script specified by name after sersync transmission is set. It is disabled by default.
<Plugin start = "false" name = "command"/>
</Sersync>
# Plug-in script example
<Plugin name = "command">
<Param prefix = "/bin/sh" suffix = "" ignoreError = "true"/> <! -- Prefix/opt/tongbu/mmm. sh suffix -->
<Filter start = "false">
<Include expression = "(. *) \. php"/>
<Include expression = "(. *) \. sh"/>
</Filter>
</Plugin>
# Plug-in script example
<Plugin name = "socket">
<Localpath watch = "/opt/tongbu">
<Deshost ip = "192.168.138.20" port = "8009"/>
</Localpath>
</Plugin>
<Plugin name = "refreshCDN">
<Localpath watch = "/data0/htdocs/cms.xoyo.com/site/">
<Cdninfo domainname = "ccms.chinache.com" port = "80" username = "xxxx" passwd = "xxxx"/>
<Sendurl base = "http://pic.xoyo.com/cms"/>
<Regexurl regex = "false" match = "cms.xoyo.com/site ([/a-zA-Z0-9] *) .xoyo.com/images"/>
</Localpath>
</Plugin>
</Head>

: Wq! # Save and exit

Parameter description:
Localpath watch = "/home/Seeyon.": # synchronization directory of the source server
192.168.0.217: # IP address of the target server
Name = "Seeyon": # name of the rsync synchronization directory module on the target server
Users = "root": # target server rsync User Name
Passwordfile = "/etc/rsync. passwd": # the target server rsync synchronizes the user's password to the storage path of the source server
Remote ip = "192.168.0.217": # the ip address of the target server.
FailLog path = "/tmp/rsync_fail_log.sh" # log of script running failure
Start = "true" # Set to true to execute full synchronization every 600 minutes
6. Set environment variables:
# Echo "export PATH = $ PATH:/usr/local/sersync/bin/">/etc/profile
# Source/etc/profile

7. Start sersync
Sersync2-r-d-o/usr/local/sersync/conf/confxml. xml
Note: The restart operation is as follows:
Killall sersync2 & sersync2-r-d-o/usr/local/sersync/conf/confxml. xml


8. Set sersync to monitor Automatic execution upon startup
Vi/etc/rc. d/rc. local # edit and add the last line

The Code is as follows:
/Usr/local/sersync/bin/sersync2-d-r-o/usr/local/sersync/conf/confxml. xml # Set the script for Automatic startup
: Wq! # Save and exit
9. Add a script to monitor whether sersync runs properly
Edit vi/home/crontab/check_sersync.sh and add the following code:

The Code is as follows:

#! /Bin/sh
Sersync = "/usr/local/sersync/bin/sersync2"
Confxml = "/usr/local/sersync/conf/confxml. xml"
Status = $ (ps aux | grep 'sersync2' | grep-v 'grep' | wc-l)
If [$ status-eq 0];
Then
$ Sersync-d-r-o $ confxml &
Else
Exit 0;
Fi

: Wq! # Save and exit
Chmod + x/home/crontab/check_sersync.sh # Add the script execution permission
Vi/etc/crontab # edit and add the following line at the end

*/1 ***** root/home/crontab/check_sersync.sh>/dev/null 2> & 1 # execute the script once every 1 minute
Service crond reload # reload the service

This article permanently updates link: https://www.bkjia.com/Linux/2018-02/151051.htm

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.