Linux--rsync Introduction

Source: Internet
Author: User
Tags inotify file permissions rsync

About Rsync
Rsync is a data-mirroring backup tool for Linux systems, using the Fast incremental Backup tool remote sync to remotely synchronize, support local replication, or synchronize with other Ssh,rsync hosts

Rsync

-A archive mode
-V Verbose output
-Q Silent Output
-R Sub-directory Recursive mode processing
-P Maintain the original permission property
-Z Compression on transfer
--delete the delete operation on the source server will also be synchronized on the target server

-C Open the validation switch
-R using Relative paths
-B Create a backup

rsync command
Three types of command formats for rsync

rsync [OPTION] ... SRC DEST
rsync [OPTION] ... SRC [[Email protected]]host:dest
rsync [OPTION] ... [[email protected]] HOST:SRC DEST

For the above three command formats, Rsync has three different modes of operation:
1. Copy Local files
[Email protected] ~]# rsync-a nfs.sh a.sh

2. Using the remote Shell Program (RSH,SSH) to copy the contents of the local machine to the remote machine
[Email protected] ~]# rsync-avz nfs.sh [email protected]:/root/b.sh
[[email protected] ~]# ssh [email protected] ' ls-l/root '

3. Use a remote shell program (such as RSH,SSH) to copy the contents of the remote machine to the local machine
[Email protected] ~]# Rsync-avz [email protected]:/etc/yum.repos.d/root/

Environment description
A machine--source server--ip192.168.56.11--application (rsync,inotify-tools, script)--centos7 system
Machine B--Target server--ip192.168.56.138--application (rsync)--CENTOS7 system

Use SSH, transfer key to B machine, convenient password-free login

[[email protected] ~]# ssh-keygen-t RSA//Key generation
[[email protected] ~]# ls. ssh/
Id_rsa id_rsa.pub
[Email protected] ~]# ssh-copy-id-i ~/.ssh/id_rsa.pub [email protected]//a machine will generate the public key to B machine
[[email protected] ~]# ssh [email protected]//try to login, found no password
[[Email protected] ~]# exit//exit B Machine

Rsync+inotify
Requirement: Synchronize the/etc directory on the source server to the/tmp/of the target server in real time

Install environment A B First turn off the firewall

[Email protected] ~]# systemctl status Firewalld
[Email protected] ~]# systemctl disable FIREWALLD
[Email protected] ~]# Systemctl stop Firewalld
[Email protected] ~]# Setenforce 0
AB Machine Installation Rsync

[[email protected] ~]# yum-y install rsync a machine
[[email protected] ~]# yum-y install rsync b machine
B-Machine Setup rsyncd.conf configuration file

[Email protected] ~]# Touch/etc/rsync.pass
[email protected] ~]# cat >>/etc/rsyncd.conf <<eof

Log file =/var/log/rsyncd.log
Pidfile =/var/run/rsyncd.pid
Lock file =/var/run/rsync.lock
Secrets file =/etc/rsync.pass
[Etc_from_client]
Path =/tmp/
Comment = Sync etc from client
UID = root
GID = root
Port = 873
Ignore errors
Use chroot = no
Read Only = no
List = no
Max connections = 200
Timeout = 600
Auth users = admin
Hosts allow = 192.168.56.11
Hosts Deny = 192.168.1.1
Eof
Create a user authentication file, set file permissions, start the Rsync service, and set up boot from

[[email protected] ~]# echo ' admin:123456 ' >/etc/rsync.pass

[Email protected] ~]# chmod 600/etc/rsync
[Email protected] ~]# Ll/etc/rsync

-RW-------. 1 root root 841 August 01:14/etc/rsyncd.conf
-RW-------. 1 root root 13 August 01:18/etc/rsync.pass

[Email protected] ~]# systemctl start RSYNCD
[Email protected] ~]# Systemctl enable RSYNCD
Created symlink From/etc/systemd/system/multi-user.target.wants/rsyncd.service to/usr/lib/systemd/system/ Rsyncd.service.
[Email protected] ~]# SS-ANLT
State recv-q send-q Local address:port Peer address:port
LISTEN 0 5 : 873 :
LISTEN 0 5::: 873::
:

A machine

[Email protected] ~]# Systemctl stop Firewalld
[Email protected] ~]# systemctl disable FIREWALLD
[Email protected] ~]# Getenforce
Enforcing
[Email protected] ~]# Setenforce 0
[Email protected] ~]# Sed-ri ' s/^ (selinux=). /\1disabled/g '/etc/sysconfig/selinux
Configuring the Yum Source
[Email protected] ~]# cd/etc/yum.repos.d/
[Email protected] yum.repos.d]# wget Http://mirrors.163.com/.help/CentOS7-Base-163.repo
[Email protected] yum.repos.d]# sed-i ' s/\ $releasever/7/g '/etc/yum.repos.d/centos7-base-163.repo
[Email protected] yum.repos.d]# sed-i ' s/^enabled=.
/enabled=1/g '/etc/yum.repos.d/centos7-base-163.repo
[Email protected] yum.repos.d]# yum-y install Epel-release
[[email protected] ~]# yum-y update--skip-broken

Install Rsync Server Software, just install, do not start, do not need to configure
[[email protected] ~]# yum-y install rsync
[[email protected] ~]# echo ' 123456 ' >/etc/rsync.pass
[Email protected] ~]# Cat/etc/rsync.pass
123456

Set file permissions, only set the file owner to have read, write permission can
[Email protected] ~]# chmod 600/etc/rsync.pass
[Email protected] ~]# Ll/etc/rsync.pass
-RW-------. 1 root root 7 August 17:51/etc/rsync.pass

[[email protected] ~]# LS//Create Test Catalog
Anaconda-ks.cfg

[Email protected] ~]# mkdir-pv/root/etc/test
mkdir: The directory "/ROOT/ETC" has been created
mkdir: The directory "/root/etc/test" has been created

[Email protected] ~]# RSYNC-AVH--port 873--progress--delete/root/etc/[email protected]::etc_from_client--password- File=/etc/rsync.pass

Sending incremental file list
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-mrneki/tmp/vmware-root/
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-mrneki/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-vmtoolsd.service-mrneki/
deleting systemd-private-5434e220d18940898b6d9672af036026-vgauthd.service-ehagl9/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-vgauthd.service-ehagl9/
deleting systemd-private-5434e220d18940898b6d9672af036026-cups.service-cqxf9f/tmp/
deleting systemd-private-5434e220d18940898b6d9672af036026-cups.service-cqxf9f/
deleting. font-unix/
deleting. Esd-1000/
deleting. xim-unix/
deleting. x11-unix/
deleting. test-unix/
deleting. ice-unix/
./
test/

Sent bytes received 670 bytes 1,490.00 bytes/sec
Total size was 0 Speedup is 0.00

B Machine

[Email protected] ~]# ls/tmp
Test
[[email protected] ~]# ll/proc/sys/fs/inotify/See if the server supports inotify and three Max supports
Total dosage 0
-rw-r--r--. 1 root root 0 August 02:04 max_queued_events
-rw-r--r--. 1 root root 0 August 02:04 max_user_instances
-rw-r--r--. 1 root root 0 August 02:04 max_user_watches

Installing Inotify-tools
[Email protected] ~]# yum-y install make GCC gcc-c++
[Email protected] ~]# yum-y install Inotify-tools

A machine

Write synchronization script, the most important step, let the script automatically to detect the directory we developed \
The file changes and then executes the RSUNC command to synchronize it to the server side
[Email protected] ~]# mkdir/scripts
[Email protected] ~]# touch/scripts/inotify.sh
[Email protected] ~]# chmod 755/scripts/inotify.sh
[Email protected] ~]# ll/scripts/inotify.sh
-rwxr-xr-x. 1 root root 0 August 18:15/scripts/inotify.sh

[Email protected] ~]# vim/scripts/inotify.sh
host=192.168.56.138
Src=/etc
Des=etc_from_client
Password=/etc/rsync.pass
User=admin
Inotifywait=/usr/bin/inotifywait

$inotifywait-mrq--timefmt '%y%m%d%h:%m '--format '%T%w%f%e '-e modify,delete,create,attrib $src \
| While Read Files;do
Rsync-avzp--delete--timeout=100--password-file=${password} $src [email protected] $host:: $des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
Done

Check Script
[Email protected] bin]# bash-x/scripts/inotify.sh

Startup scripts
[Email protected] ~]# nohup bash/scripts/inotify.sh &
[1] 58297

[Email protected] bin]# Ps-ef|grep inotify
Root 74599 1 0 17:01 pts/0 00:00:00/usr/bin/inotifywait-mrq--timefmt%y%m%d%h:%m--format%T%w%f%e-e modify,delete,c Reate,attrib/etc
Root 74621 2316 0 17:03 pts/0 00:00:00 bash/scripts/inotify.sh
Root 74622 74621 0 17:03 pts/0 00:00:00/usr/bin/inotifywait-mrq--timefmt%y%m%d%h:%m--format%T%w%f%e-e Modify,dele Te,create,attrib/etc
Root 74623 74621 0 17:03 pts/0 00:00:00 bash/scripts/inotify.sh
Root 74625 2316 0 17:03 pts/0 00:00:00 grep--color=auto inotify

Generate a new file on the source server
[Email protected] ~]# mkdir-p/etc/httpd24
[Email protected] ~]# LS/ETC/HTTPD24
[Email protected] ~]# echo ' Hello World ' >/etc/httpd24/test

To view the logs generated by inotify
[Email protected] ~]# Tail/tmp/rsync.log
20180816 17:05/etc/httpd24create,isdir was rsynced//created, Isdir is synchronous
20180816 17:05/etc/httpd24/testcreate was rsynced//sync created
20180816 17:05/etc/httpd24/testmodify was rsynced//synchronized modification

Set up boot from

[Email protected] ~]# chmod +x/etc/rc.d/rc.local
[Email protected] ~]# ll/etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 April 15:36/etc/rc.d/rc.local

[[email protected] ~]# echo ' nohup/bin/bash/scripts/inotify.sh ' >>/etc/rc.d/rc.local
[Email protected] ~]# tail/etc/rc.d/rc.local
To run scripts during boot instead of the using this file.

In contrast to previous versions due to parallel execution during boot
This script is not being run after all other services.

Please note this must run ' chmod +x/etc/rc.d/rc.local ' to ensure
That this script would be executed during boot.
Touch/var/lock/subsys/local
nohup/bin/bash/scripts/inotify.sh

Go to the target server to see if the newly generated files are automatically sent up:

[Email protected] tmp]# pwd
/tmp
[[email protected] tmp]# ls
ETC test

[[email protected] tmp]# ls etc/httpd24/
Test
To synchronize the source server's/etc directory to the target server, the new test file is automatically synchronized.

Linux--rsync Introduction

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.