
Remote Sync (rsync) is a tool for transferring files across network resources. Rsync is written in C language and supports syncing files to remote repositories such as Vultr Block Storage.
You can use Rsync to back up your Vultr Cloud Compute instance files to a mounted block storage instance. Furthermore, you can automate the entire process using Linux cron Jobs, allowing the backup to run without manual intervention.
This guide walks you through setting up automatic backups with Rsync and Vultr Block Storage on Ubuntu 24.04 Cloud Compute instance.
Before you begin:
By default, Ubuntu ships with Rsync. This is because the tool is popularly used in most unix-based systems. If Rsync isn't available in your system, run the following command to install it.
The Rsync tool provides the following command that you can use to run backup.
In the above command:
rsync: This is the base command for running the tool.
[options]. These are flexible Rsync flags that you can use to customize how Rsync works. Here are the most common flags:
-a: Runs the tool in archive mode and preserves most file attributes including file permissions, symbolic links, and file ownership.
-z: Compresses data during transmissions to conserve space.
--delete: Deletes extraneous files from destination dirs. This means files that aren't on the source (sending side).
If you delete a file in the source location and run the rsync command specifying the --delete flag, Rsync also deletes the file in the remote destination. Therefore, to avoid accidentally deleting your backups, omit the --delete flag. For best results, always follow the 321 backup rule to ensure data redundancy, diversity, and geographic separation:
source: This is the local directory that you want to copy.
destination: This is the remote directory where you want to send your files for backups.
To run a manual backup and copy the files from your Cloud Compute instance to the Vultr Block Storage, follow the steps below:
Create a new project directory under your user home directory. You may skip this step in a production environment if you want to back up an already existing directory.
Navigate to the new ~/project directory.
Use the touch command to create two sample files under the directory.
Use the Rsync command to copy the two files above to the Vultr Block storage. Run the command. Replace /mnt/blockstorage/project/ with the actual path where you want to backup the data.
List all files in the directory
Output:
Edit the test.txt file from the source directory.
Enter the following dummy content.
Save and close the file.
Sync the new changes using the Rsync command.
Check the Block Storage directory.
Output:
The above output shows that Rsync has synced the new changes to the backup directory.
While manual Rsync backups may be ideal for some use cases, they're time consuming and are not efficient for production purposes. This section shows you how to set up automatic Rsync backups using Linux Cron Jobs.
Open the crontab settings.
The following settings detail the basic configurations you should make to schedule a Cron Job that runs at a specific time.
Linux Cron job provides flexible timings that you can use. Here are some ideal settings for backups.
* * * * * your_user_name rsync command: Runs every minute.0 0 * * * your_user_name rsync command: Runs every day at midnight.0 0 * * 0 your_user_name rsync command: Runs every Sunday at midnight.0 0 1 * * your_user_name rsync command: Runs every first day of the month at midnight.Refer to the guide on How to Use the Cron Task Scheduler to learn more about customizing the Cron job timings.
Schedule a Cron job to run every minute by adding the following settings at the end of the /etc/crontab file. Replace your_user_name with your correct Linux username and /mnt/blockstorage/project/ with the correct Vultr Block Storage path.
Save and close the file.
Add a new file in the local source directory.
Continously watch the crontab log file by piping the Linux tail command to the grep command.
Output:
Press Ctrl + C to exit the previous command. Then, list the Block Storage files after the cron Job runs at least once.
Output.
The Cron Job is now running as expected and Rsync has successfully synced the new test3.txt file with the Block Storage.
Rsync allows you to restore files from the backup storage to the original source if you delete the files. Use the following syntax:
Depending on your business workflow, you can choose to directly restore the backup files to the original source or copy them to a temporary directory.
Delete the original files from the original source directory.
Ensure the local source directory is empty.
Output
Run the following Rsync command to sync the files from the Block Storage to the source directory. Replace /mnt/blockstorage/project/ with the correct Vultr Block Storage path.
Review the content of the local storage directory.
Output
This guide took you through setting up automatic backups with Rsync and Vultr Block Storage. You have learned using the Rsync command and applying common flags to run manual backups. Then, you have automated the backups using the Linux crontab jobs and restored the files.
0 Comments
Be the first to comment and share your perspective with the community.