[Linux] Practical Record of Cross-Host File Synchronization

  • Objective:

The main goal is to implement automatic file synchronization between the primary and backup hosts. When the programs or files on the primary host are updated, the system automatically synchronizes them to the backup host at a specified time or frequency, achieving synchronization within a certain time frame.

  • Applicable Systems: Redhat 7 series
  • Specific operational steps are as follows:

1. Development of the file synchronization script.

    1. Implement passwordless access for rsync. For security reasons, this should be executed on the backup host.
    2. Execute the command:
      ssh-keygen   # Generate local key certificate. Press Enter until completion.

    3. Upload the key to the remote host using the command:
      ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]  # Replace with the actual remote host IP address before executing.

    4. Enter the user password for the remote host. If successful, the operation is complete.

2. File synchronization command.

    1. Example:
      rsync -raz --delete [email protected]:/home/test/app/ /home/test/app

    2. The –delete flag indicates that if files are deleted on the primary host, the corresponding files on the backup host will also be deleted.
    3. The IP is the remote host’s IP. This should be the primary server’s IP.
    4. The first directory is the directory on the remote host that needs to be synchronized, which should end with a /.
    5. The second directory is the local directory for synchronization, which should not end with a /.

3. Setting up scheduled tasks. Use crontab to set up scheduled tasks.

    1. Use the command:
      crontab -l # View existing scheduled tasks.

    2. Edit the scheduled task execution, example:
      5 22 * * 1-5 sh /home/###/file_syn/code.sh >> /home/###/file_syn/log/code.o.log 2>> /home/###/file_syn/log/code.e.log

    3. The first parameter: Generate the cron expression as needed, noting that Linux has only 5 parameters, which specify minutes, differing from the cron in Java’s qz.
    4. The second parameter: The full path of the script file to be executed. If not using the sh file path mode, the script file needs to have execution permissions added.
    5. >> Redirect standard output to the execution file, followed by the full path of the log record file.
    6. 2>> Redirect error output to the execution file, followed by the full path of the error log record file.
    7. Input the command:
      crontab -e  # Enter the scheduled task editing mode. Append the prepared command at the end, :wq # Save and exit.

Leave a Comment