search
HomeBackend DevelopmentPHP TutorialRemember to incrementally synchronize remote server files with rsync

rsync remote shell incrementally synchronizes data

There are two ways to synchronize files with rsync, one is the daemon method (rsync daemon) and the other is through the remote shell method (rsync remote shell).

The difference between the two methods

daemon mode. This method connects to the remote rsync daemon through TCP. You need to use a configuration file and enable the daemon process.

<code>rsync [OPTION] user@host::src dest
rsync [OPTION] src user@host::dest</code>

remote shell method, this method does not require the use of configuration files or daemon processes.

<code>rsync [OPTION] user@host:src dest
rsync [OPTION] src user@host:dest</code>

The daemon method is too troublesome. I have to set up the configuration file and daemon process, so I chose the remote shell method to synchronize as follows:

<code>  rsync -avr --delete yanruitao@123.123.123.123:/export/test/htdocs/files/ /export/test/htdocs/files/</code>

The parameters avr --delete respectively represent

<code>-a 归档(archive)模式,以递归方式传输文件,并保持文件属性
-v 输出同步的详细信息(verbose)
-r 对子目录进行递归模式处理(recursive)
--delete 删除源(SRC)中没有目标(DST)中有的文件</code>

If everything is normal, when you run the above synchronization command, you will be prompted to enter the password of the yanruitao user on the remote machine (the first time you will be prompted whether to establish a relationship (it seems to be, I forgot the details), just press Enter), after execution It will be synchronized according to the rules.

But there is a problem. I want to add a crontab task and perform incremental synchronization at 2 o'clock every day. I cannot enter the password at this time. At this time, I have to establish a trust relationship on the two machines

Establish trust relationship between two machines

Let’s take a look at encryption and authentication based on public and private keys before establishing a trust relationship

Private key signing process

The picture below is stolen, haha, it’s perfect to use this picture to understand the authentication process:

<code>消息-->[私钥]-->签名-->[公钥]-->认证
私钥数字签名,公钥验证</code>
  1. Alice generates a public key and a private key and sends the public key to Bob.
  2. Alice uses her private key to generate a signature, which is encryption.
  3. Alice sends the signed information to Bob.
  4. Bob uses Alice’s public key to decrypt and verify the authenticity of the signature.
    Remember to incrementally synchronize remote server files with rsync
Public key encryption process

The picture below is also stolen (it’s easier to understand if you have a picture). Here is Alice sending information to Bob through symmetric key technology:

<code>消息-->[公钥]-->签名后的消息-->私钥-->解密后的消息
公钥加密,私钥解密</code>
  1. Bob generates his own public and private keys and sends the public key to Alice.
  2. Alice encrypts the message with Bob's public key.
  3. Alice sends the encrypted information to Bob.
  4. Bob uses his private key to decrypt and obtain the information sent by Alice.
    Remember to incrementally synchronize remote server files with rsync

Establish trust relationship between hosts

Understanding the above knowledge of public keys and private keys, now let’s establish a trust relationship between two hosts, , , to establish a trust relationship with 100 on 101 (that is, 100 does not need to enter a password when logging in to 101), this time the private key signature process is used:

Generate the public key and private key in the home directory of yanruitao192.168.1.100 (the existing ones can be ignored)
<code>ssh-keygen -t rsa
#执行完之后会在家目录下的.ssh文件夹下生成id_rsa、id_rsa.pub两个文件,后者是公钥。
scp .ssh/id_rsa.pub yanruitao@192.168.1.101:/home/yanruitao/
#scp同样是通过remote shell的方式传送文件,回车之后会提示输入密码,此时
#还未建立信任关系,因此需要输入密码。确认100的公钥发送给101</code>
Establish a trust relationship with yanruitao192.168.1.100 in the home directory of yanruitao192.168.1.101
<code>#将刚才传送的100机器的公钥写入101yanruitao及目录下的.ssh/authorized_keys文件
cat id_rsa.pub >> .ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
#至此就在yanruitao@192.168.1.101上对192.168.1.100建立了信任关系</code>
Set up crontab
<code>0 */2 * * * rsync yanruitao@192.168.1.101:/export/wwwroot/hotdocs/files/ /export/wwwroot/hotdocs/files/
#设置完成,每天02:00分会执行一次增量同步</code>
Problems encountered

Of course, you may encounter problems when you actually operate it. When I was doing it, the user names of the two machines were different, one was yanruitao and the other was mywife (haha, don’t laugh). The trust relationship between yanruitao and mywife’s resume is this At this time, you need to re-operate the above steps to establish the trust relationship in the mywife home directory. Another problem is that the owner of the /export/wwwroot/htdocs/files/ folder on mywife’s machine must be mywife:

<code>#修改文件夹所有者为mywife
sudo chown mywife:users /export/wwwroot/htdocs/files/</code>

Otherwise, an error may be reported (I forgot the specific error, I encountered it at the company). Although it does not seem to affect synchronization, $? will return 23, which will affect the following operations. Please pay attention here as well.

Reference article

http://www.williamlong.info/archives/837.html
http://www.cnblogs.com/ymy124/archive/2012/04/04/2432432.html
http://www.zhihu.com /question/25912483

The copyright of this article belongs to the author iforever (). Any form of reprinting is prohibited without the author's consent. After reprinting the article, the author and the original text link must be given in an obvious position on the article page, otherwise we reserve the right to pursue legal liability s right.

The above introduces the incremental synchronization of remote server files with rsync, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.