rsync is a powerful command line utility for efficiently copying and synchronizing files between Linux systems. It’s particularly well suited for backups due to its ability to transfer only changed data, handle remote servers, and maintain file permissions.
Why rsync is Good for Backups
- Efficient Data Transfer - rsync only transfers the differences between source and destination files.
- Incremental Backups - It can backup incremental changes rather than full copies.
- Remote Support - rsync supports remote backups via SSH.
- File Permissions - Preserves file permissions, timestamps, and ownership.
- Selective Excludes - Can exclude specific files or directories with
--exclude.
Example Usage and Common Flags
Basic Command
rsync -avz /source/ user@remote:/destination/
-a= archive mode copies recursively and preserves permissions, timestamps, etc.-v= verbose output-z= files being transfered are compressed for faster transfer speeds
Common Flags Explained
--delete- Deletes files on the destination that don’t exist on the source.--exclude- Excludes specific files or patterns (e.g.,--exclude="*.tmp")--progress- Shows progress of the transfer.
Deleting Content on Remote Server
rsync -avz --delete /source/ user@remote:/destination/
This will delete files on the remote server that don’t exist on the source, effectively pruning the remote directory to match the source.
Setting Up SSH Keys for Remote Access
1. Generate SSH key pair:
ssh-keygen -t ed25519 -C "your_email@example.com"
You can leave the passphrase blank to use the ssh key without needing a password
This ssh key can also be used to authorize your computer with your Github account by adding it to the SSH and GPG keys section of the Github settings.
The public key can be found at ~/.ssh/id_ed25519.pub
2. Copy public key to remote server:
ssh-copy-id user@remote-server
This command copies your public ssh key to the remote server allowing you to login automatically with out a passphrase or password providing the key you created does not require a passphrase.
3. Test connection:
ssh user@remote-server
If you created your ssh key without a passphrase then you should be able to login to the remote server without needing a password.
Setting Up Cron Jobs for Automatic Backups
Example Cron Job
crontab -e
Add this line to schedule backups:
0 2 * * * rsync -az --delete /source/ user@remote:/destination/
- Runs daily at 2 AM
- Uses rsync with archive mode and delete flag
- Backs up /source/ directory to remote server
Why It’s Important to Test Your rsync Backup
Testing your rsync backup is crucial because it ensures that you can actually restore data from the backup when needed.
Here’s why:
- Verify The Backup Works: Testing confirms that the backup contains everything that is needed to restore to a working state.
- Reliability: A backup is only useful if it works when you need it most. Testing helps identify issues before a disaster strikes.
- Peace of Mind: Knowing that your backups are reliable gives you confidence in your data protection strategy.
- Compliance and Auditing: Many organizations require proof that backups can be restored, especially in regulated industries.
In short, testing your rsync backup is not just a best practice it’s a necessity for data reliability and business continuity. You should test your backups over time to make sure that any changes to your computing environment are still covered by your backups.
Conclusion
rsync is a versatile tool for Linux backups, offering efficiency, flexibility, and remote support. By understanding its flags and setting up SSH access, you can create robust backup solutions that keep your data safe and synchronized.
Latest Blog Posts:
How to do backups with rsync on Linux
Comprehensive guide to using rsync for backups on Linux systems
How to use GitHub Actions Workflow to Deploy Your Website using rsync automatically
Learn how to automate your Hugo website deployment with GitHub Actions and rsync for fast, secure updates.
How to add Lunr.js Search to Your Hugo Website
Learn how to integrate Lunr.js for fast, client-side search functionality in your Hugo static site.
How to use Github Actions to Deploy a Hugo Website to GitHub Pages automatically
In this post, I'll walk you through a complete GitHub Actions workflow for building and deploying a Hugo static website to GitHub Pages. This setup is ideal for developers who want a free, automated hosting solution for their blogs, documentation sites, or portfolios. It replaces manual uploads with a push-to-deploy pipeline.
How to Setup Hugo Admonitions (Markdown Callouts in Hugo)
By adding Hugo Admonitions to your website so you can easily add stylish note bars for things like tips, warnings, cautions, important information etc. You can do this quickly and easily directly using markdown.
How to setup Syntax Highlighting with Copy Button using Hugo
In this post, I’ll show you how to add syntax highlighting and a ‘Copy’ button to your Hugo code blocks using a single self-contained JavaScript file. You’ll be able to highlight code in multiple languages, optionally show line numbers, and let users copy snippets with one click.
How to strip unused CSS from your CSS files and Minify them into a single CSS file using Hugo
Using Hugo with PostCSS and PurgeCSS allows you to automatically remove unused CSS and bundle all your styles into a single minified file for production, improving page load times and performance.
Make an application always load as a floating window in Cosmic Desktop
Make an application always load as a floating window in Cosmic Desktop
Why I migrated from Wordpress to Hugo
My reasons for migrating from Wordpress to the Hugo static website generator
Using Gtk4 With C# and Gir.Core Box Layout Tutorial
In this box layout tutorial, you will learn how to build a C# Gtk Application that makes use of the box layout to display multiple widgets both vertically and horizontally. You will also learn how to use a box layout to display an icon and a label inside a Gtk.Button widget.