July 2, 2024
Tutorials

How To Use Rsync to Sync Local and Remote Files and Directories in Ubuntu 22.04

How To Use Rsync to Sync Local and Remote Files and Directories in Ubuntu 22

Rsync (remote sync) is a command line tool for synchronizing files and directories across various machines. You can easily utilize it to transfer data, both locally and remotely. Rsync is commonly used for creating backups and transferring remote files.

This article is about how to synchronize local and remote files and folders using Rsync in Ubuntu 22.04. Also, check How to Use SFTP to Securely Transfer Files with a Remote Server on Ubuntu 22.04?

How To Use Rsync to Sync Local and Remote Files and Directories in Ubuntu 22.04?

The given below procedural guide will be used to sync local and remote files and directories in Ubuntu 22.04:

  • Method 1: Sync Local and Remote Files and Dictionaries via terminal
  • Method 2: Sync Local and Remote Files and Directories through Rsync Script

How to Establish Connection Between Local and Remote Server in Ubuntu 22.04?

Using the following commands can connect you to the remote server in Ubuntu 22.04 machine.

Step 1: Install OpenSSH Server

To access the remote server from your local system, you need to install the OpenSSH server on your Ubuntu 22.04:

sudo apt install openssh-server -y
 

Once the installation process of OpenSSH is completed, you can proceed to the next step.

Step 2: Check SSH Status

Run the command to confirm whether OpenSSH is working correctly:

sudo systemctl status ssh

You will see the information about the current status of the SSH service.

Note: Make sure the ssh.service is running.

Step 3: Generate SSH Key

Generate an SSH key with the following command to ensure secure communication between your local and remote systems:

ssh-keygen

Step 4: Copy SSH Key to Remote Server

Execute the command below to transfer your generated key to the remote server:

ssh-copy-id techpub@127.0.0.1

This command allows you to log in to the remote server without entering a password.

Step 5: Test SSH Connection

Now, you can test the SSH connection to the remote server to ensure a successful setup without using a password:

ssh techpub@127.0.0.1

Method 1: Sync Local and Remote Data via Terminal

Follow these commands to use rsync in terminal for syncronizing files in Ubuntu 22.04.

Step 1: Search for Directories in /var/www/

From the remote server terminal, use the find command to inspect the source directory, such as /var/www/:

sudo find /var/www/ -type d

Here, you can see the files in your /var/www/ directory.

Step 2: Sync /var/www/ to Local Machine

Use the following rsync command to synchronize the files and directories from /var/www/ on the remote server to your local machine (~/Downloads/sync-folder):

rsync -avz -e ssh techpub@127.0.0.1:/var/www/ ~/Downloads/sync-folder

The -v flag is used to provide details about what’s happening during the process.

Step 3: Verify Directories in the Local “sync-folder”

To confirm whether the files from /var/www/ have been successfully synchronized to the local path, execute the command:

sudo find ~/Downloads/sync-folder -type d

As you can view in the above output that all the contents from the remote server are now available on your local machine.

Method 2: Sync Local and Remote Data Through Rsync Script

Through the following commands, you can create a script for synchronization between local and remote files.

Step 1: Create Backup Sync Script

To create a script for synchronization, you first need to generate a .sh file using the command:

sudo nano backup_sync.sh

Step 2: Type Script for Sync Files

Once you have opened the script file, type the provided script using rsync command:

rsync -avz -e ssh techpub@127.0.0.1:/var/www/ ~/Downloads/sync-folder 
#source Destination

Step 3: Make Sync Script Executable

Grant execution permission to the backup_sync script using the command:

sudo chmod +x backup_sync.sh

Step 4: Run Sync Script

To synchronize the local and remote files, simply execute the backup_sync.sh file from the terminal:

./backup_sync.sh

You can see that directories from the remote server have been successfully received in the sync-folder.

Conclusion

In Ubuntu, rsync is a simple and easy-to-use command for synchronizing files. It allows you to sync both local and remote files and directories. There are two different methods for utilizing the rsync command: directly from the terminal and by using a .sh script file. This article explored both methods in detail, providing a detailed guide on how to use the rsync command to synchronize local and remote files and directories in Ubuntu 22.04.

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video