Bookmark this page

Guided Exercise: Synchronize Files Between Systems Securely

Synchronize the contents of a local directory with a copy on a remote server, by using the rsync command.

Outcomes

  • Use the rsync command to synchronize the contents of a local directory with a copy on a remote server.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

This command prepares your environment and ensures that all required resources are available.

[student@workstation ~]$ lab start archive-sync

Instructions

  1. On the workstation machine, use the ssh command to log in to the servera machine as the student user, and then switch to the root user.

    [student@workstation ~]$ ssh student@servera
    ...output omitted...
    [student@servera ~]$ sudo -i
    [sudo] password for student: student
    [root@servera ~]#
  2. Open a new terminal window, and log in to the serverb machine as the student user.

    [student@workstation ~]$ ssh student@serverb
    ...output omitted...
    [student@serverb ~]$
  3. Create the /home/student/serverlogs directory on the serverb machine. Use the rsync command to securely create an initial copy of the /var/log directory tree on the servera machine in the /home/student/serverlogs directory on the serverb machine.

    1. On the serverb machine, create the /home/student/serverlogs directory to store the synchronized log files from the servera machine.

      [student@serverb ~]$ mkdir ~/serverlogs
    2. On the servera machine, use the rsync command to synchronize the /var/log directory tree on the servera machine to the /home/student/serverlogs directory on the serverb machine. Only the root user can read all the /var/log directory contents on the servera machine. Transfer all the files in the initial synchronization.

      [root@servera ~]# rsync -av /var/log student@serverb:/home/student/serverlogs
      ...output omitted...
      student@serverb's password: student
      sending incremental file list
      log/
      log/README -> ../../usr/share/doc/systemd/README.logs
      log/boot.log
      ...output omitted...
      
      sent 1,390,819 bytes  received 508 bytes  309,183.78 bytes/sec
      total size is 1,388,520  speedup is 1.00
  4. On the servera machine, execute the logger "Log files synchronized" command to add an entry in the /var/log/messages log file to reflect when the last synchronization occurred.

    [root@servera ~]# logger "Log files synchronized"
  5. Use the rsync command to securely synchronize from the /var/log directory tree on the servera machine to the /home/student/serverlogs directory on the serverb machine. This time, only the changed log files are transferred.

    [root@servera ~]# rsync -av /var/log student@serverb:/home/student/serverlogs
    student@serverb's password: student
    sending incremental file list
    log/messages
    
    sent 3,854 bytes  received 3,807 bytes  2,188.86 bytes/sec
    total size is 1,388,648  speedup is 181.26
  6. On the serverb machine, verify the contents of the /home/student/serverlogs/log/messages file with the tail command.

    [student@serverb ~]$ tail -n 5 ~/serverlogs/log/messages
    Mar 22 04:25:08 servera systemd[1]: systemd-hostnamed.service: Deactivated successfully.
    Mar 22 04:25:09 servera systemd[1066]: Starting Mark boot as successful...
    Mar 22 04:25:09 servera systemd[1066]: Finished Mark boot as successful.
    Mar 22 04:25:47 servera chronyd[750]: Selected source 172.25.254.254
    Mar 22 04:26:25 servera root[1213]: Log files synchronized
  7. Exit and close the extra terminal.

    [student@serverb ~]$ exit
    logout
    Connection to serverb closed.
    [student@workstation]$
  8. Return to the workstation machine as the student user.

    [student@servera ~]$ exit
    logout
    Connection to servera closed.
    [student@workstation]$

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish archive-sync

Revision: rh134-9.3-5fd2368