Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use rsync to realize remote synchronization

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to use rsync to achieve remote synchronization", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use rsync to achieve remote synchronization" this article.

Rsync is a tool that enables incremental backups. With task planning, rsync can achieve timing or interval synchronization, and with inotify or sersync, trigger real-time synchronization can be achieved?

A brief introduction to rsync

Rsync (Remote Sync, remote synchronization) is an open source fast backup tool that can mirror and synchronize the entire directory tree between different hosts, support incremental backup, maintain links and permissions, and use optimized synchronization algorithms to perform compression before transmission, so it is very suitable for remote backup, mirror servers and other applications.

The official website of rsync: http://rsync.samba.org/, currently the latest version is 3.1.3, by Wayne Davison. For maintenance, as one of the most commonly used file backup tools, rsync is often one of the basic components installed by default on Linux and UNIX systems.

Rsync is a fast incremental backup tool that supports:

(1) local replication, (2) synchronization with other SSH, (3) synchronization with rsync host.

In a remote synchronization task, the client responsible for initiating the rsync synchronization operation is called the initiator, and the server responsible for the corresponding rsync synchronization operation from the client is called the synchronization source. During synchronization, the synchronization source is responsible for providing the original location of the document, and the initiator should have read access to that location. As shown in the figure:

2. Configure rsync source configuration rsync source server is roughly divided into three steps: (1) establish rsync configuration file; (2) create data file for backup account; (3) start rsync service.

(1) establish rsync configuration file

The / etc/rsyncd.conf file does not exist by default before the CentOS 7 system, and it has been available since CentOS 7 with template reference information (write what you need according to the actual situation, pay attention to the format).

[root@localhost ~] # vim / etc/rsyncd.confuid = nobody / / enable anonymous user gid = nobody use chroot = yes / / imprisoned in the source directory address = 192.168.1.1 / / listening address port 873 / / listening port log file = / var/log/rsyncd.log / / Log file location pid file = / var/run/rsyncd.pid / / the file location of the process ID hosts allow = 192.168.1.0 Compact 24 / / the client address allowed to access [wwwroot] / / shared module name path = / var/www/html / / the actual path of the source directory comment = aaa / / description (can be omitted) read only = no / / whether it is read-only dont compress = * .gz * .bz2 * .rar * .zip / / the file type that is no longer compressed during synchronization auth users = backuper / / authorized account secrets file = / etc/rsyncd_users.db / / data file storing account information

For security reasons, it is best to allow only read-only synchronization for rsync synchronization sources. In addition, synchronization can be done anonymously as long as the "auth users" and "secrets file" configuration items are removed!

(2) create data files for backup accounts

Create an account data file according to the contents of the rsync configuration file. One user per line, separated by a colon between the user and the password.

[root@localhost ~] # vim / etc/rsyncd_users.dbbackuper:123456

As the account information is stored in clear text, it is necessary to adjust the file permissions to avoid account information disclosure.

[root@localhost ~] # chmod 600 / etc/rsyncd_users.db

The backup user should have read access to the source directory.

[root@localhost] # ls-ld / var/www/htmldrwxr-xr-x. 2 root root 6 November 15 2016 / var/www/html

(3) start the rsync service

[root@localhost ~] # rsync-- daemon [root@localhost ~] # netstat-anpt | grep rsynctcp 00 192.168.1.1 daemon 873 0.0.0.0 * LISTEN 44001/rsync

If you need to restart the rsync service, you need to:

[root@localhost ~] # kill $(cat / var/run/rsyncd.pid) / / stop service [root@localhost ~] # rsync-- daemon// startup service [root@localhost ~] # kill-9 $(cat / var/run/rsyncd.pid)

Or directly use the "netstat-anpt | grep rsync" command to find out the process number, just like using the "kill process number". To stop the rsync service using the first method, you must delete the file that holds the rsync service process:

[root@localhost] # rm-rf / var/run/rsyncd.pid III. Use the rsync backup tool

Once the rsync synchronization source server is configured, the client can use the rsync tool to perform remote synchronization.

Option for the rsync command:-r: recursive mode Contains all files in the directory and subdirectories-l: for symbolic link files are still copied as symbolic link files-p: keep file permission mark-t: retain file time stamp-g: retain file subordinate group tag (superuser only)-o: retain file owner tag (superuser only)-D: retain device files and other special files-a: archive mode Recursive merge retains object attributes, equivalent to-rlptgoD-v: displays detailed (verbose) information of synchronization process-z: compresses files when transferring files (compress)-H: preserves hard-connected files-A: preserves ACL attribute information-- delete: deletes files that are present in the target location but not in the original location-- checksum: determines whether to skip files based on the checksum of the object

Rsync is a fast incremental backup tool that supports (1) local replication, (2) synchronization with other SSH, and (3) synchronization with rsync hosts.

(1) Local replication

[root@localhost ~] # rsync / etc/passwd 123.txt// is similar to the cp command

(2) synchronize with other SSH

[root@localhost ~] # rsync-av root@192.168.1.2:/root/123.txt. Root@192.168.1.2's password:

(3) synchronize with rsync host

[root@localhost ~] # rsync-avz backuper@192.168.1.1::wwwroot / root or [root@localhost ~] # rsync-avz rsync://backuper@192.168.1.1/wwwroot / root

The effect of these two commands is the same! To upload, just change the order of the directory (make sure you have write access to the uploaded directory)! Write permissions can only be executed by entering the following command on the synchronous source side

[root@localhost] # chmod otakw / var/www/html [root@localhost] # ls-ld / var/www/htmldrwxr-xrwx. 2 root root June 17 16:47 / var/www/html [root@localhost ~] # rsync-avz / root backuper@192.168.1.1::wwwroot

However, in a real work environment, backup work is usually repeated as planned, such as:

[root@localhost ~] # vim / root/123.pass123456// arbitrarily create a file to store the password information of rsync authorized users [root@localhost ~] # chmod 600 / root/123.pass// must set 600 permissions Otherwise, there will be an error [root@localhost ~] # crontab-e systemctl restart crond// / create scheduled task 30 22 * / usr/bin/rsync-az-- delete-- password-file=/root/123.pass backuper@192.168.1.1::wwwroot / a systemctl restart crond// / execute the script [root@localhost ~] # systemctl restart crond// to restart the crond service at 22:30 every night

About the format of the crond scheduled task profile (from top to bottom):

The above is all the contents of the article "how to use rsync to achieve remote synchronization". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report