Logo

dev-resources.site

for different kinds of informations.

A Beginner's Guide to rsync : A Powerful Tool for File Synchronization

Published at
11/23/2024
Categories
backup
synchronisation
automation
linux
Author
testouri_med_amine
Author
18 person written this
testouri_med_amine
open
A Beginner's Guide to rsync : A Powerful Tool for File Synchronization

Introduction :

You've probably faced the difficulty of effectively moving and synchronizing files between many platforms as a system administrator. A powerful answer for this operation is the rsync (remote synchronization) command-line tool, which offers a number of capabilities to streamline backup and file transfer procedures.

To help you fully utilize rsync, we'll go over its foundations and provide real-world examples in this guide.

Create a Simple Test File :

Let’s start by creating a simple file that we will later copy using rsync.

nvim testfile
Enter fullscreen mode Exit fullscreen mode

Add some content, for example:

Hello, this is some content for testing!
Enter fullscreen mode Exit fullscreen mode

Save and exit. This file will serve as our test file for the subsequent steps.

Copying Files with rsync :

Now, let’s use rsync to copy the same file. While rsync can do what cp does, it also provides additional options to optimize and resume file transfers, we will see this later on.

rsync testfile newfiletest
Enter fullscreen mode Exit fullscreen mode

This will copy testfile to a new file called newfiletest in your local system. The real power of rsync comes in when you need to copy files to different systems.

Using rsync to Transfer Files to a VPS (virtual private server) :

Let's take it a step further. Instead of copying a file locally, let’s copy it to a virtual machine (VM). The beauty of rsync is its ability to transfer files across different systems effortlessly. Assuming SSH is configured and you have the necessary permissions, use the following command:

rsync testfile user@vm-ip:/home/user/testfile

Enter fullscreen mode Exit fullscreen mode

This will transfer the testfile to your VM’s /home/user/ directory. If you don’t specify a destination path, rsync will automatically place it in the user's home directory, here is an example :

transferring a file using rsync command

As you can see in the picture, the file has been transferred successfully.

Using rsync to transfer a Directory to a VM :

Copying files is simple, but what about copying directories? You can use the -r option to copy an entire directory:

rsync -rv directory/source user@vmip:/home/user/directory/destination

Enter fullscreen mode Exit fullscreen mode

using rsync to transfer a whole directory

The -r option is for recursive copying, meaning it will copy the contents of the directory, including all subdirectories and files.

Using the -u Option for Updates :

The -u option is one of the most powerful features of rsync. It allows updates only, meaning it will only copy files that have been modified since the last transfer.

Here’s a simple test to demonstrate this:

rsync -u testfile user@vm-ip:/home/user/testfile
Enter fullscreen mode Exit fullscreen mode

trying u option with rsync

If testfile hasn't changed, rsync will skip it, saving you time and bandwidth.

Leveraging the -P Option for Partial Transfers :

What occurs if there is a disruption to your transfer? The -P option is revolutionary. For huge files or erratic connections, it enables rsync to continue transfers where they left off.

rsync -P testfile user@vm-ip:/home/user/testfile
Enter fullscreen mode Exit fullscreen mode

This will ensure that if the transfer stops, it can pick up where it left off, reducing redundancy and time.

Understanding the -a (Archive) Mode :

-a (archive mode) is one of the most crucial rsync parameters. It guarantees that timestamps, symbolic links, and file permissions are maintained throughout the transmission. When transferring important files or directories and wishing to preserve their integrity, this is essential.

rsync -av testfile user@vm-ip:/home/user/testfile

Enter fullscreen mode Exit fullscreen mode

The -a option is a combination of several options: -r (recursive), -l (preserve symlinks), -p (preserve permissions), and others. This makes rsync an excellent tool for backing up files without losing any metadata.

Key rsync Options at a Glance :

To customize the behavior of rsync, you can leverage a variety of options:

-a (archive mode): Preserves file permissions, modification times, and other attributes.
-v (verbose): Provides detailed output about the transfer process.
-z (compress): Compresses data during transfer, reducing network usage.
-P (partial): Resumes interrupted transfers.
-u (update): Only transfers newer files.

Make Backups with a Script :

Let’s take everything we’ve learned so far and apply it to a real-world use case: creating an automated backup script using rsync. We’ll use a cron job to back up a local directory to our VM on a regular basis.

Here’s the backup script:

#!/bin/bash

# Define the source directory on the local machine

SOURCE="/home/user/documents"

# Define the destination directory on the VPS (in this case, your VPS server's /home/user/backup folder)

DEST="user@your-vps-ip:/home/user/backup/documents"

# Run rsync to back up the files from the local machine to the VPS

rsync -avz --delete "$SOURCE/" "$DEST/"

# Print a message when done

echo "Backup to VPS completed successfully!"

Enter fullscreen mode Exit fullscreen mode

Explanation:

-a: Archive mode (preserves file attributes).
-v: Verbose output, showing the progress.
-z: Compression during transfer, reducing data usage.
--delete: Deletes files from the destination that are no longer present in the source directory.

This script ensures that your local directory is backed up to the VM.

Important note : After creating the .sh file, you'll need to make it executable before running it. You can do this by using the following command in your terminal:

chmod +x your_script.sh
Enter fullscreen mode Exit fullscreen mode

Result of the script

Result of the script

By setting it up as a cron job, you can automate regular backups without manual intervention.

Conclusion: Why You Should Use rsync ?

rsync is a powerful and flexible tool that goes far beyond simple file copying. Whether you’re transferring files locally, across a network, or creating automated backups, rsync gives you control, efficiency, and peace of mind.

With its ability to resume interrupted transfers, synchronize only modified files, and preserve metadata, rsync is a must-have tool for every sysadmin and cloud professional.

Pro Tip: Use cron to automate your backups with a script like the one above and save yourself time and headaches!

This guide has hopefully made the power of rsync clear to you. It's a tool every sysadmin should have in their toolkit. Feel free to try these commands, experiment with different options, and discover how rsync can streamline your workflows.

Stay tuned for my next post, where we'll delve deeper into automating rsync tasks with cron jobs. We'll cover everything from basic scheduling to advanced techniques for ensuring reliable, scheduled backups.

backup Article's
30 articles in total
Favicon
Navigating Disaster Recovery in the Digital Age: Choosing the Right Approach – Part 3
Favicon
Navigating Disaster Recovery in the Digital Age: Choosing the Right Approach – Part 2
Favicon
Testing speed of modern External SSD backup
Favicon
S3 Storage For DevOps Backups
Favicon
Expert-Approved Gmail Backup
Favicon
Navigating Disaster Recovery in the Digital Age: Choosing the Right Approach – Part 1
Favicon
Seamless Mail Backup Solution for Consistent and Long-Term Email Safety
Favicon
Active Directory Backups: Ensuring the Security and Integrity
Favicon
Consumer tech problem, developer's solution: A new smartphone, an old bug
Favicon
How to Backup Virtual Machines to Object Storage with Borg, Borgmatic, Rclone, and Cron's Rsync
Favicon
A Beginner's Guide to rsync : A Powerful Tool for File Synchronization
Favicon
Comprehensive Disaster Recovery and Backup Strategy for Critical Fintech Applications on AWS
Favicon
Finding the Right Data Protection Solutions for Your Team’s Needs
Favicon
Top Backup Software Solutions to Protect Your Data in 2024
Favicon
Proxmox Backup by NAKIVO: The Ultimate Solution for VM Data Protection
Favicon
Your Current Backup Automation Is Missing the Key: Ansible, AWS S3, and Cron 🔑
Favicon
Publish a Windows application that automatically saves a backup of the desktop folder.
Favicon
Aurora snapshot recovery cross-account
Favicon
Using Timeshift for System's Snapshots and Recovery on Debian 12 via Command Line
Favicon
The Benefits of MedOne and VMware: A Cloud Solution Built for the Future
Favicon
A simple tool for automated MySQL database dumps and restores
Favicon
🔒 Protégez votre site WordPress : Sécurité et Backup, les indispensables pour bien débuter 🔒
Favicon
Top 7 oVirt Backup Solution 2024
Favicon
Azure SQL to Storage Account Export Tool
Favicon
Enable Microsoft 365 Backups
Favicon
Why i love docker
Favicon
How to Backup and Restore Kubernetes clusters using Velero
Favicon
Linux KVM Backup and Recovery Best Practices: Expert Advice You Need
Favicon
If pg_dump is not a backup tool, what is?
Favicon
The Essentials of Backup Rotation with nxs-backup

Featured ones: