Learn how to implement a secure 3-2-1 backup strategy on Ubuntu 24.04 LTS using automated scripts, encryption, and off-site cloud storage.
6 min read
Data loss can happen to anyone. Whether it is a failing storage drive, an accidental deletion, or a critical system crash, losing your website or application data is stressful. For developers, system administrators, and business owners in India managing digital infrastructure, having a solid backup plan is non-negotiable.
Relying on a single backup copy stored on your local server is a recipe for disaster. If the physical machine goes down, your backup goes down with it. That is where the time-tested 3-2-1 backup strategy comes in. Combined with automation, encryption, and off-site cloud storage, you can protect your servers against almost any unexpected event.
Understanding the 3-2-1 Backup Strategy
The 3-2-1 backup rule is an industry standard designed to ensure that your data survives major failures. The rule is simple to remember and practical to implement:
3 copies of your data: Keep your production data and at least two distinct backup copies.
2 different storage media: Store your backups on different types of hardware or storage environments (such as local disk and cloud object storage).
1 off-site copy: Keep at least one backup copy in a physically separate location away from your primary server.
If your primary server is hosted in a data center in Mumbai, keeping a secondary copy on a local external drive is a start. However, keeping your final off-site copy in a separate region or with a cloud object storage provider completes the 3-2-1 formula. If your primary facility experiences a catastrophic issue, your off-site data remains completely safe and accessible.
Preparing Your Server Environment
To implement this guide, we will use Ubuntu 24.04 LTS. Before we automate anything, we need to ensure our system has the necessary tools installed. We will use rsync for efficient file synchronization and the AWS Command Line Interface (CLI) to interact with S3-compatible object storage buckets.
First, update your package index and install rsync and the AWS CLI by running the following commands in your terminal:
sudo apt update
sudo apt install rsync awscli -y
Here is what these commands do:
sudo apt update: Refreshes your local package list to ensure you install the latest stable software versions available for Ubuntu 24.04 LTS.
sudo apt install rsync awscli -y: Installs Rsync for local and remote file transfer, and the AWS CLI tool to push backups to your S3 bucket, automatically confirming prompts with the -y flag.
Next, configure your AWS CLI credentials with your S3-compatible provider details. Run the configuration command:
aws configure
Enter your Access Key ID, Secret Access Key, your preferred default region (e.g., ap-south-1), and set the output format to json when prompted.
Creating Local Encrypted Snapshots
Before sending data off-site, it is best practice to create a clean, compressed, and encrypted archive on your local server. This saves bandwidth during upload and ensures your data is secure both in transit and at rest.
Let us create a backup script directory and write a simple shell script to handle archiving. Create a script file named backup.sh:
nano /usr/local/bin/backup.sh
Add the following script content, making sure to replace /var/www/html with the actual path of the data you want to back up:
#!/bin/bash
# Configuration variables
BACKUP_DIR="/var/backups/local"
SOURCE_DIR="/var/www/html"
DATE=$(date +%Y-%m-%d_%H-%M-%S)
ARCHIVE_NAME="site-backup-$DATE.tar.gz"
ENCRYPTED_NAME="$ARCHIVE_NAME.gpg"
PASSPHRASE="your_strong_encryption_passphrase"
# Create local backup directory if it does not exist
mkdir -p $BACKUP_DIR
# Step 1: Create a compressed tar archive of the source directory
tar -czf $BACKUP_DIR/$ARCHIVE_NAME $SOURCE_DIR
# Step 2: Encrypt the archive using GnuPG
gpg --symmetric --batch --passphrase "$PASSPHRASE" $BACKUP_DIR/$ARCHIVE_NAME
# Step 3: Remove the unencrypted archive
rm $BACKUP_DIR/$ARCHIVE_NAME
echo "Local encrypted backup created successfully: $ENCRYPTED_NAME"
Make the script executable by running:
sudo chmod +x /usr/local/bin/backup.sh
Here is what each tool in the script does:
tar -czf: Creates a compressed gzip archive of your source directory to save disk space.
gpg --symmetric: Encrypts the archive using symmetric-key cryptography with a strong passphrase so unauthorized parties cannot read your data.
rm: Deletes the unencrypted intermediate file, leaving only the secure, encrypted version behind.
Automating Off-Site Transfer to S3 Buckets
Now that we have a secure local archive, we need to push it to an off-site S3 bucket to fulfill the off-site requirement of the 3-2-1 strategy. We can expand our backup script or create a synchronization step using the AWS CLI.
Add the S3 upload command to the bottom of your /usr/local/bin/backup.sh file:
# Step 4: Upload encrypted backup to S3 bucket
aws s3 cp $BACKUP_DIR/$ENCRYPTED_NAME s3://your-bucket-name/backups/
# Step 5: Optional - Clean up local backups older than 7 days to save disk space
find $BACKUP_DIR -type f -mtime +7 -exec rm {} \;
Here is a breakdown of the new commands:
aws s3 cp: Copies the local encrypted backup file directly into your specified remote S3 cloud bucket.
find ... -mtime +7 -exec rm {} \;: Locates and removes local backup archives older than 7 days to prevent your server's storage from filling up.
Scheduling Backups with Cron
Manual backups are easy to forget. Automation ensures your data is protected consistently without human intervention. We can use Cron, the standard job scheduler in Ubuntu 24.04 LTS, to run our backup script automatically every night.
Open the root crontab file by running:
sudo crontab -e
Add the following line at the bottom of the file to run the backup script every day at 2:00 AM:
0 2 * * *: Runs the task at the 0th minute of the 2nd hour (2:00 AM) every day.
> /var/log/backup.log 2>&1: Redirects both standard output and error messages to a log file, making it easy to troubleshoot if a backup fails.
Testing Your Disaster Recovery Plan
A backup plan is useless if you cannot successfully restore your data. Disaster recovery testing is the final and most important step of the process. You should periodically download your backup from S3, decrypt it, and test extraction.
Finally, extract the archive to a temporary test directory to verify the file integrity:
mkdir /tmp/restore-test && tar -xzf /tmp/restored-backup.tar.gz -C /tmp/restore-test
If your files extract cleanly and look correct, your disaster recovery pipeline is verified and fully operational.
Conclusion
Implementing the 3-2-1 backup strategy does not have to be complicated. By combining Ubuntu 24.04 LTS system tools like tar and gpg with automated S3 cloud storage uploads, you create a robust, secure defense against data loss. Take time today to set up your scripts, automate your schedules, and test your restores so you can run your servers with absolute peace of mind.