![]() |
rsync illustration |
Hello there! Welcome to my blog. I hope you're all doing well. Today, I'll show you how to use the rsync command in Linux to back up data to another location, whether it’s local storage or a network destination.
I've been using rsync in my programs to automatically back up my data to different storage drives and my small server. Now, I want to guide you through using rsync manually.
Let's get started!
Introduction To rsync
rsync is a powerful tool in Linux for synchronizing files and directories between two locations. It is commonly used for backups and data transfers because it efficiently copies only the changes made to files.
Basic rsync syntax:
rsync [options] source destination
Explanation:
- source is the path to the file or directory you want to copy.
- destination is the location where you want to copy it to.
Commonly Used rsync Options
Here are some options that are commonly used with rsync to control how files are copied:
-v (Verbose mode): This option makes rsync show detailed output, so you can see which files are being transferred. It’s helpful for tracking progress and troubleshooting.
-r (Recursive): Use this option to copy entire directories, including all files and subdirectories inside them. Without this, rsync will only copy individual files.
-a (Archive mode): This is a powerful option that preserves file properties such as permissions, timestamps, symbolic links, ownership, and more. It’s commonly used when making backups, as it keeps everything the same as the original.
-z (Compression): Compresses files during transfer to save bandwidth. This is especially useful if you’re transferring data over a network, as it can speed up the process.
--delete: Deletes files in the destination that don’t exist in the source. Use this option carefully, as it keeps the destination mirror image of the source by removing files that are no longer present in the source directory.
-h (Human-readable): Outputs file sizes in an easy-to-read format (like MB or GB) rather than just bytes. This makes it easier to see how large your files are during transfer.
Basic rsync Usage
1. Copying File Locally
To copy files and directories within your local machine, use the following command:
rsync -av /source/dir/ /destination/dir/
Explanation:
- /source/dir/ is the folder you want to copy from.
- /destination/dir/ is where you want to copy the files to.
- -a (Archive mode) preserves file properties such as permissions and timestamps.
- -v (Verbose) provides detailed output, so you can see each file as it’s being copied.
This command is useful for creating backups on your local computer. For instance, you might use it to back up your documents to an external hard drive.
P.S:
The trailing slash (/) after source/dir/ means rsync will copy the contents of the source directory into the destination. Omitting the trailing slash would copy the source/dir directory itself into the destination.
2. Copying Files to a Remote Server
You can also use rsync to copy files to a remote server over SSH, which encrypts your data during transfer, providing a secure way to back up or transfer files over the internet:
rsync -avz /source/dir/ user@remote_host:/destination/dir/
Explanation:
- -z (Compress) compresses files during the transfer to reduce bandwidth usage, which is helpful when copying over the internet.
- user@remote_host:/destination/dir/ specifies the remote server’s destination location. Replace user with your username on the remote server and remote_host with the server’s IP address or domain name.
This command is ideal for backing up files from your computer to a remote server, such as saving project files to a cloud server.
Additional Options You Can Add
1. Dry Run Mode (-n)
If you want to preview the files that will be copied without actually transferring them, you can add the -n option (also called "dry run"):
rsync -avn /source/dir/ /destination/dir/
This shows what will happen without making any changes. It's helpful for verifying commands before running them.
2. Deleting Extra Files in Destination (--delete)
To make sure that the destination is an exact mirror of the source, you can add the --delete option:
rsync -av --delete /source/dir/ /destination/dir/
This removes files in the destination that don’t exist in the source, keeping the destination in sync with any deletions from the source directory
More Commands Of Some Practical rsync
1. Local Copy with Verbose and Archive Mode:
rsync -av /source/dir/ /destination/dir/
2. Copy to a Remote Server with Compression:
rsync -avz /source/dir/ user@remote_host:/destination/dir/
3. Preview Files to Be Transferred (dry run):
rsync -avn /source/dir/ /destination/dir/
4. Exact Mirror of Source with Deletion:
rsync -av --delete /source/dir/ /destination/dir/
5. Syncing Only Newer Files:
rsync -auv /source/dir/ /destination/dir/
6. Excluding Files and Directories:
rsync -av --exclude 'file_or_directory_name' /source/dir/ /destination/dir/
7. Limiting Bandwidth Usage:
rsync -av --bwlimit=1000 /source/dir/ /destination/dir/
8. Scheduling rsync With Cron:
crontab -e
# Runs rsync every day at midnight:
0 0 * * * rsync -av /source/dir/ /destination/dir/
This is a demonstration video to provide a clearer understanding.
End
This guide should give you a clear understanding of how to use rsync in different situations. Thank you for visiting my blog and taking the time to read through! I hope to see you again in my next post. Have a great day!