[NFS Project Actual Warfare II] Time-synchronous push backup of shared data in NFS

Keywords: Linux rsync inotify Web Server yum

[NFS Project Actual Warfare II] Time-synchronous push backup of shared data in NFS

Label (Space Separation): Linux Service Architecture - Chen Siqi

This teaching note is a summary of my study and work career, which is the first draft (there are many imperfections), the original work, allowed to be reproduced, when reproduced, please be sure to indicate the original source of the article, author information and this statement in the form of hyperlinks. Otherwise, legal liability will be pursued. http://www.cnblogs.com/chensiqiqi/

[TOC]

Preface

"Project Practice" series is a series of synchronous teaching practice for the second stage of "Linux Practical Teaching Notes". Each project will eventually form a cluster of 10 basic core service architectures for the core teaching objectives of the second stage of "Linux Practical Teaching Notes". This article is attached to http://www.cnblogs.com/chensiqiqi/p/6531003.html Project I.

Enterprise case

The company has two web servers that have been providing services to the outside world, but with the development of business, more and more users, more and more powerful functions of the website, various pictures, videos and other occupied more and more hard disk space. Therefore, the leader stores the data of the web server directly on the NFS server for storage, and in order to prevent a single point of failure of the NFS server, the leader hopes to synchronize the content stored by the web server to the Rsync backup server in real time. Now it's up to you to plan and fulfill the leadership's needs.

Specific requirements are as follows:

  • [x] The requirements of the NFS server are as follows:
    • The shared directory of the server is called / data directory.
    • Privilege requirements can only be accessed by intranet segments and can be read and written, synchronized from time to time.
    • In order to facilitate management, it is necessary to designate the NFS virtual account as chensiqi, uid=12306, gid=12306.
    • The identity of all visitors is compressed to a minimum
    • When synchronizing the contents in the / data directory, push them to the / data directory of the backup server (inotify+rsync)
  • [x] web server mounts NFS shared directory uniformly to / var/html/www directory

Environmental preparation

System version

[root@nfs01 ~]# cat /etc/redhat-release 
CentOS release 6.8 (Final)

Kernel parameters

[root@nfs01 ~]# uname -r
2.6.32-642.el6.x86_64

Host network parameter setting

host name External network adapter Intranet NIC purpose
web02 10.0.0.7/24 172.16.1.7/24 B1-apache web server
web01 10.0.0.8/24 172.16.1.8/24 B2-nginx web server
nfs01 10.0.0.31/24 172.16.1.31/24 C1-NFS Storage Server
backup 10.0.0.41/24 172.16.1.41/24 C2-rsync Backup Server

First, start deploying NFS server-side NFS sharing

Step 1: NFS package installation

yum -y install nfs-utils rpcbind

Step 2: Create user chensiqi for uid=12306, gid=12306

[root@nfs01 ~]# useradd -u 12306 -s /sbin/nologin -M chensiqi
[root@nfs01 ~]# id chensiqi
uid=12306(chensiqi) gid=12306(chensiqi) group=12306(chensiqi)

Step 3: Modify the / etc/exports configuration file

[root@nfs01 ~]# echo "/data 172.16.1.0/24(rw,sync,all_squash,anonuid=12306,anongid=12306)" >> /etc/exports 
[root@nfs01 ~]# cat /etc/exports
/data 172.16.1.0/24(rw,rsync,all_squash,anonuid=12306,anongid=12306)

Step 4: Start NFS-related services

Start rpcbind service first; then start nfs service

[root@nfs01 ~]# /etc/init.d/rpcbind start
 rpcbind: [OK]
[root@nfs01 ~]# /etc/init.d/nfs start
 Start NFS services: [OK]
Turn off the NFS quota: [confirm]
Start NFS mountd: [Determine]
Start the NFS daemon: [Identify]
Starting RPC idmapd:                                       [  OK  ]
[root@nfs01 ~]# 

Step 5: Set the owner and owner groups of shared directory / data as specified users

[root@nfs01 ~]# chown -R chensiqi.chensiqi /data
[root@nfs01 ~]# ll -d /data
drwxr-xr-x. 2 chensiqi chensiqi 4096 3 Month 1400:14 /data

Step 6: Local mount testing

[root@nfs01 ~]# showmount -e  
Export list for nfs01:
/data 172.16.1.0/24
[root@nfs01 ~]# hostname -I
10.0.0.31 172.16.1.31 
[root@nfs01 ~]# mount 172.16.1.31:/data /mnt
[root@nfs01 ~]# ll -d /mnt
drwxr-xr-x. 2 chensiqi chensiqi 4096 3 Month 1400:14 /mnt
[root@nfs01 ~]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      18003272 4154188  12927896  25% /
tmpfs                   502068       0    502068   0% /dev/shm
/dev/sda1               487652   34856    427196   8% /boot
172.16.1.31:/data     18003328 4154240  12928000  25% /mnt

Step 7: Set rpcbind and nfs services to boot

[root@nfs01 ~]# tail -3 /etc/rc.local
#start up nfs service by chensiqi at 20170315
/etc/init.d/rpcbind start
/etc/init.d/nfs start
[root@nfs01 ~]# 

Second, start deploying web-side NFS client shared mount

Configure web01 server:

Step 1: NFS client needs to install nfs-utils package

yum -y install nfs-utils

Step 2: Mount shared directories

[root@web01 ~]# showmount -e nfs01
Export list for nfs01:
/data 172.16.1.0/24
[root@web01 ~]# mkdir -p /var/html/www
[root@web01 ~]# showmount -e nfs01
Export list for nfs01:
/data 172.16.1.0/24
[root@web01 ~]# mount 172.16.1.31:/data /var/html/www
[root@web01 ~]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      18003272 4815804  12266280  29% /
tmpfs                   502068       0    502068   0% /dev/shm
/dev/sda1               487652   34856    427196   8% /boot
172.16.1.31:/data     18003328 4154240  12928000  25% /mnt
172.16.1.31:/data     18003328 4154240  12928000  25% /var/html/www

Step 3: Test write data

[root@web01 ~]# cd /var/html/www
[root@web01 www]# ll
//Total dosage 4
-rw-r--r--. 1 chensiqi chensiqi 0 3 Month 1519:27 csfdsf
-rw-rw-r--. 1 chensiqi chensiqi 0 3 Month 1400:14 test2
-rw-rw-r--. 1 chensiqi chensiqi 4 3 Month 1400:14 test.txt
[root@web01 www]# touch 11111
[root@web01 www]# ll
//Total dosage 4
-rw-r--r--. 1 chensiqi chensiqi 0 3 Month 1519:34 11111
-rw-r--r--. 1 chensiqi chensiqi 0 3 Month 1519:27 csfdsf
-rw-rw-r--. 1 chensiqi chensiqi 0 3 Month 1400:14 test2
-rw-rw-r--. 1 chensiqi chensiqi 4 3 Month 1400:14 test.txt

Step 4: Configure boot-up automatic mounting

[root@web01 www]# tail -1 /etc/rc.local 
mount -t nfs -o nodev,noexec,nosuid,rw  172.16.1.31:/data /var/html/www

Configure web02 server:

Configuration is the same as web01 server

Third, configure Rsync backup server

Note: Since the configuration has been configured in the project backup, you only need to modify the configuration file here.

Step 1: Add a new nfsbackup module to the configuration file / etc/rsyncd.conf

Add the following to the configuration file

[nfsbackup]
# Use directory
path = /data/
# Ignore mistakes
ignore errors
# Readable and Writable (true or false)
read only = false
# Prevent remote lists (don't let the server see what's on the server remotely)
list = false
# Allow IP
hosts allow = 172.16.1.0/24
# Prohibit IP
hosts deny = 0.0.0.0/32
# Virtual user
auth users = rsync_backup
# Files that store users and passwords
secrets file = /etc/rsync.password

Step 2: Start the rsync service

Method 1: If no rsync startup script is written

[root@backup ~]# rsync --daemon
[root@backup ~]# ss -antup | grep rsync
tcp    LISTEN     0      5                     :::873                  :::*      users:(("rsync",7098,5))
tcp    LISTEN     0      5                      *:873                   *:*      users:(("rsync",7098,4))

Method 2: If a startup script has been written

[root@backup ~]# /etc/init.d/rsyncd start
Starting Rsync:                                            [Determine]
[root@backup ~]# ss -antup | grep rsync
tcp    LISTEN     0      5                     :::873                  :::*      users:(("rsync",7098,5))
tcp    LISTEN     0      5                      *:873                   *:*      users:(("rsync",7098,4))

Step 3: rsync service start-up

[root@backup ~]# echo ". /etc/init.d/rsyncd start" >> /etc/rc.local
[root@backup ~]# tail -1 /etc/rc.local 
. /etc/init.d/rsyncd start

Fourth, configure inotify event monitoring tool on NFS server

Step 1: Install the inotify event monitoring tool

This tool needs to install epel source
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

[root@nfs01 ~]# yum -y install inotify-tools

Step 2: Time-to-time push test for rsync + inotify

Open two shell windows

Enter the following in the first window:
[root@nfs01 ~]# inotifywait -mrq --format '%w%f' -e delete,close_write,create /data
 After input, the shell is blocked (monitored from time to time)

Create, modify, and delete tests in the / data directory of another window:
At this point, we can see that the currently blocked shell window records all directory changes.

Instructions:
inotifywait: monitoring commands
 - m: Continuous monitoring (blocked)
- r: Recursive monitoring, monitoring directories and all subdirectories of directories
 - q: Output only simple monitoring information
 Format: Specifies the format for monitoring data output
 - e: Specify the type of event monitored
 Delete: delete events
 close_write: Close event written to a file (actually monitoring modification files)
create: create events

Step 3: Write synchronous push script when inotify + inotify

#!/bin/bash

Path=/data
backup_Server=172.16.1.41


/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data | while read line
do
        if [ -f $line ];then
                rsync -az $line --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
        else
                cd $Path &&\
                rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
        fi
done

Step 4: script add boot (background) boot

[root@nfs01 ~]# echo "sh /server/scripts/inotify.sh &" >> /etc/rc.local

Step 5: Synchronization testing

NFS Storage Server: Do the following

[root@nfs01 ~]# cd /data
[root@nfs01 data]# ll
//Total dosage 4
-rw-r--r--. 1 root root 4 3 Month 1521:02 aaa
[root@nfs01 data]# touch chensiqi  #Establish
[root@nfs01 data]# ll
//Total dosage 4
-rw-r--r--. 1 root root 4 3 Month 1521:02 aaa
-rw-r--r--. 1 root root 0 3 Month 1521:16 chensiqi
[root@nfs01 data]# echo 1111 >> chensiqi #modify
[root@nfs01 data]# ll
//Total dosage 8
-rw-r--r--. 1 root root 4 3 Month 1521:02 aaa
-rw-r--r--. 1 root root 5 3 Month 1521:17 chensiqi
[root@nfs01 data]# rm -rf aaa  #delete

rsync backup server: view directory synchronization effect

[root@backup ~]# cd /data
[root@backup data]# ll
//Total dosage 4
-rw-r--r--. 1 rsync rsync 5 3 Month 152017 chensiqi
[root@backup data]# cat chensiqi
1111
[root@backup data]# 

Posted by dgudema on Fri, 19 Apr 2019 18:15:33 -0700