Linux uses rsync to implement file directory synchronization

Source: Internet
Author: User
Tags chmod file permissions rsync

To achieve the goal:
Server A synchronizes with Server B, which only indicates that server a synchronizes to Server B and Server B restores to server A.
Consider security factors and synchronize with ordinary users.
Using Cronjob, timed synchronization.

A/opt/web directory on server A, synchronizing with/opt/web directory on B client server. That is: B actively synchronized with a. (A's files are synchronized to the B machine)

The code is as follows Copy Code
Os:reaht AS4
A Server 192.168.0.100/opt/web
B Server 192.168.0.60/opt/web

A. A Server config

1. Installation Services

The code is as follows Copy Code
Yum Install xinetd
Yum Install rsync

2. Vi/etc/xinetd.d/rsync modifies disable =yes as disable =no, with the following revised documents

The code is as follows Copy Code

# Default:off
# description:the Rsync server is a good addition to a FTP server, as it
# allows CRC Checksumming etc.
Service rsync

{
Disable = no
Socket_type = Stream
wait = no
user = Backup
Server =/usr/bin/rsync
Server_args =–daemon
Log_on_failure + + USERID
}

3. Edit Primary profile/etc/rsyncd.conf (Create yourself if not present)

The code is as follows Copy Code
Vi/etc/rsyncd.conf
UID = Backup
GID = Backup
Use chroot = no # chroot not used
Max connections = 4 # Maximum number of connections is 4
PID file =/var/run/rsyncd.pid
Lock file =/var/run/rsyncd.lock
Log file =/var/log/rsyncd.log # logging Files
[web] # Here is the Certified module name, which needs to be specified on the client side
Path =/opt/web # A directory that needs to be mirrored
Ignore errors # can ignore some irrelevant IO errors
Read Only = true # reading only
List = False # does not allow column files
Hosts allow = 192.168.0.0 # allowed IP address
Hosts deny = 0.0.0.0/32 # banned IP address
Auth users = backup # authenticated username, if this line is not found, it is anonymous
Secrets file =/etc/backup.pass #认证用户的密码文件 Authentication file name

4. Edit server password file/etc/backup.pass, file can only have Read permission 400

The code is as follows Copy Code
Vi/etc/backup.pass #文件格式如下
backup:123456
chmod 400/etc/backup.pass

5. Start Service
Start rsync for the first time

The code is as follows Copy Code
Rsync–daemon–config=/etc/rsyncd.conf

If prompted

The code is as follows Copy Code
Failed to create PID File/var/run/rsyncd.pid:file exists

Using directives

The code is as follows Copy Code
Rm-rf/var/run/rsyncd.pid

Use the following command to see if Rsync is already running: Netstat-an|grep 873

The code is as follows Copy Code
[Root@localhost web]# Netstat-an|grep 873
TCP 0 0 0.0.0.0:873 0.0.0.0:* LISTEN
TCP 0 0::: 873:::* LISTEN

Reboot already running rsync

  code is as follows copy code
[root@mail video]# Ps-ef | grep rsync
root     27284     1  0 10:26?   00:00:00 rsync– daemon–config=/etc/rsyncd.conf
root     30516 29986  0 18:35 pts/3    00:00:00 grep rsync
[root@mail video]# kill-9 27284
[root@mail video]# rsync–daemon–config=/etc/rsyncd.conf
Service xinetd Restart, restart the xinted service
Rsync system, it needs to be started with a –deamon mode, and the services port is TCP 873
Rsync–deamon–config=/etc/rsyn cd.conf

There are several different ways to start a service at startup, such as:
A, join inetd.conf
Edit/etc/services, add rsync 873/tcp, specify the service port for Rsync is 873
Add/etc/inetd.conf, join rsync stream TCP nowait backup/bin/rsync Rsync–daemon
B, Join rc.local
In a variety of operating systems, RC file storage location is not the same, you can modify the system to boot when the Rsync–daemon loaded in.

Two. B Server config
1. Yum Install rsync installation rsync service
2. Create an rsync connection when the password file/etc/rsync_client.pass, the listing of their own random arrangement, file permissions for read-only 400
Vi/etc/rsync_client.pass #文件格式如下:
#只需要配置连接时使用的密码即可, you must be the same as the password defined on server A.

The code is as follows Copy Code
chmod 400/etc/rsync_client.pass

3. Use the rsync command to connect to the server to achieve file synchronization

The code is as follows Copy Code
Rsync-vzrtopg–progress–delete–password-file=/etc/rsync_client.pass Backup@192.168.0.100::web/opt/web

PS: When directory synchronization to save the same file owner information, you need to create the same user on the B server

The code is as follows Copy Code
Rsync-qcazrtopgl–progress–delete–password-file=/etc/rsync_client.pass–exclude= "*.gz" –exclude-from=/opt/pcdir Backup@192.168.0.100::web/opt/web

Backup is a certified user name;
192.168.0.100 for server-side IP;
The Web is a module name;
/opt/web as a local directory
Three Timed tasks

The code is as follows Copy Code
[Root@localhost admin_backups]# VI backup.sh

The contents are as follows:

The code is as follows Copy Code
#/bin/sh
Rsync-qcazrtopgl–progress–delete–password-file=/etc/rsync_client.pass–exclude= "*.gz" –exclude-from=/opt/pcdir Backup@192.168.0.100::web/opt/web

To add a timed task:

The code is as follows Copy Code
[Root@localhost admin_backups]# Crontab–e

Add the following:

The code is as follows Copy Code
*/1 * * * */home/admin/admin_backups/backup.sh >/dev/null 2>&1

Sync every minute from server A to Server B!
4. Use –execlude= to exclude file suffix names that do not need to be synchronized

The code is as follows Copy Code
Rsync-vzrtopg–progress–delete–password-file=/etc/rsync_client.pass–exclude= "*.tmp"

Backup@192.168.0.100::web/opt/web
5. Use –execlude-from= to exclude directories that do not need to be synchronized

  code is as follows copy code
[root@localhost opt]# Rsync-vzrtopg–progress–delete–password-file=/etc/rsync_client.pass–exclude-from=/opt/pcdir Backup@192.168.0.100::web/opt/web

The contents of the

/opt/pcdir file are as follows
Cat/opt/pcdir
temp/
Parameter Description
-VZRTOPG
V is verbose,
Z is compression,
R is recursive,
Topg is to keep the original properties of the file, such as the owner, the time parameter
--progress
is to show detailed progress
–delete
means that if the server side deletes this file, then the client also deletes the file accordingly, maintaining true consistency
–exclude= "*.tmp"                Does not contain some files
–execlude-from=        exclude directories that do not need to be synchronized
/opt/web/
Specify the client side store mirror destination path

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.