# Linux - Remote Backups with rsync

#### What is rsync?

`rsync` is a command-line utility for synchronizing files and directories between two locations. It can handle local backups or remote synchronization over SSH, ensuring that your data is consistently updated and accessible.

**Why Use rsync for Backups?**

* **Incremental Backups:** Only the changes in files are synced.
    
* **Bandwidth Efficient:** Uses minimal bandwidth by syncing only differences.
    
* **Versatile:** Can sync both local and remote files.
    
* **Preserves File Permissions:** Maintains file permissions, ownership, timestamps, etc.
    
* **Secure:** Can be used with SSH for secure file transfer.
    

#### Checking for rsync Installation

Before diving into its usage, let's check if `rsync` is installed on your system:

```plaintext
rsync --version
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726758810856/d9747bd9-216c-4df0-8ad5-6785130bb00b.png align="center")

If it’s not installed, you can quickly set it up with the following commands:

```plaintext
sudo apt update
sudo apt install rsync
```

**Basic Usage of** `rsync`

Understanding the basic syntax of `rsync` is essential before automating your backup process:

```plaintext
rsync [options] source destination
```

For example, to back up your documents from the `/home/user/documents` directory to an external media location, you can use:

```plaintext
rsync -av /home/user/documents /media/backup
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726758915025/09c871d6-f962-4a07-9107-be893a80a27d.png align="center")

* `-a` stands for "archive" mode, which preserves permissions, timestamps, symbolic links, etc.
    
* `-v` is for "verbose," which displays the process of syncing files.
    

**Remote Backups Using SSH**

##### <mark>Step 1: Generate SSH Key Pair</mark>

To enable passwordless authentication, first generate an SSH key pair on your local machine:

```plaintext
ssh-keygen
```

This will create two files: a private key (`~/.ssh/id_rsa`) and a public key (`~/.ssh/id_`[`rsa.pub`](http://rsa.pub)).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726759846482/351610d1-10fe-4c3d-a5c2-8696597295d3.png align="center")

**<mark>Step 2: Copy the Public SSH Key</mark>**

Display the contents of your public SSH key on the local machine:

```plaintext
cat ~/.ssh/id_rsa.pub
```

This will output your public key. Copy the entire output.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726760540969/3a68a08e-c8be-4e2a-a4c2-f7ed2e94cbe5.png align="center")

**<mark>Step 3: Add Your Public Key to the Remote Server</mark>**

Now, log in to the remote server &gt; Paste the public key in the `authorized_keys` file.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726763578374/7322effb-f70b-44a1-aaa5-f54eb55aa940.png align="center")

**<mark>Step 4: Test the SSH Connection</mark>**

Then, try logging back into the remote server from the local machine without entering a password:

```plaintext
ssh user@172.31.13.54
```

If you successfully log in without a password prompt, your SSH key setup is working.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726763902192/b38690a2-42b5-4031-abad-9154dce71ecf.png align="center")

**<mark>Step 5: Using rsync for Remote Backups</mark>**

You can now use `rsync` to perform remote backups. Here's an example `rsync` command to sync a directory from the local machine to the remote server:

```plaintext
rsync -avz /local/path/to/backup/ user@172.31.13.54:backup/
```

* `-a`: Archive mode (preserves permissions, symbolic links, etc.).
    
* `-v`: Verbose (shows detailed output).
    
* `-z`: Compression (compresses data during transfer).
    

Be sure to replace `/local/path/to/backup/` with the actual directory you wish to back up and `backup/` with the desired destination path on the remote server.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1726765016069/3518bb64-2f7d-4873-be1a-db002d1dafef.png align="center")

#### Conclusion

Using `rsync` for backups streamlines the process of keeping your data secure, whether locally or remotely. By following these simple steps, you can ensure that your important files are backed up efficiently and reliably. Happy backing up!
