Back to all articles
Linux & Servers2026-03-03

Automated Backup Guide on Linux for S3 Storage

Create a secure automated backup script to send files and databases directly to Amazon S3 in an encrypted format.

Data loss on production servers can destroy a company's reputation and cause massive financial losses. Although VPS providers offer disk snapshots, having independent backups stored off the source server is a golden rule of infrastructure.

In this guide, we show you how to create an automated file and database backup routine for Amazon S3 cloud storage (or any compatible object storage like MinIO or Backblaze).

1. Configuring Credentials (AWS CLI)

The recommended utility to interact with S3 from the Linux terminal is the AWS CLI.

Installation and Configuration:

  1. Install AWS CLI v2 (the awscli apt package is no longer available in Ubuntu 24.04):
    sudo snap install aws-cli --classic
  2. Create an access key (Access Key and Secret Key) in the AWS console via the IAM service, limiting permissions only to actions on the desired backup bucket.
  3. Configure credentials on the Linux server by running (with sudo, because the cron job in section 3 runs as root and reads root's credentials):
    sudo aws configure

2. Writing the Backup Script

Create a bash script to automate the database dump (in this example, PostgreSQL) and compress the static files folder.

#!/bin/bash
# Stop at the first error: a failed dump is neither uploaded nor deleted
set -euo pipefail

# Settings
# cron runs with a minimal PATH; the snap aws binary lives in /snap/bin
PATH="/usr/local/bin:/usr/bin:/bin:/snap/bin"
BACKUP_DIR="/tmp/backups"
BUCKET_NAME="my-secure-backup-bucket"
DATE=$(date +%Y-%m-%d-%H%M)

# Create temporary folder
mkdir -p $BACKUP_DIR

# 1. Database Backup
sudo -u postgres pg_dump my_database > $BACKUP_DIR/db_$DATE.sql

# 2. Compressing application files
tar -czf $BACKUP_DIR/files_$DATE.tar.gz /var/www/my-app

# 3. Encrypted upload to S3
aws s3 cp $BACKUP_DIR/db_$DATE.sql s3://$BUCKET_NAME/db_$DATE.sql --sse AES256
aws s3 cp $BACKUP_DIR/files_$DATE.tar.gz s3://$BUCKET_NAME/files_$DATE.tar.gz --sse AES256

# Cleanup temporary local files
rm -rf $BACKUP_DIR

Save the script as /home/user/scripts/backup.sh and make it executable with chmod +x /home/user/scripts/backup.sh.

3. Automating with Cron Job

To run the script automatically every night (at 2:00 AM):

  1. Open the system scheduled tasks:
    sudo crontab -e
  2. Add the line at the end of the file:
0 2 * * * /home/user/scripts/backup.sh >> /var/log/backup-s3.log 2>&1

4. Restore into a test database

The backup counts only after a restore has been rehearsed. Download the dump into an empty database, never on top of production:

sudo aws s3 cp s3://my-secure-backup-bucket/db_2026-10-01-0200.sql /tmp/restore.sql
sudo -u postgres createdb my_database_test
sudo -u postgres psql -d my_database_test -f /tmp/restore.sql
sudo -u postgres psql -d my_database_test -c '\dt'

\dt lists the tables. An empty list means the dump did not load. Application files come back into a separate directory:

sudo aws s3 cp s3://my-secure-backup-bucket/files_2026-10-01-0200.tar.gz /tmp/files.tar.gz
sudo mkdir -p /var/www/restore-test
sudo tar -tzf /tmp/files.tar.gz | head
sudo tar -xzf /tmp/files.tar.gz -C /var/www/restore-test

tar -tzf lists the archive before extracting. Check the first path: GNU tar strips the leading / when it creates the archive, so the content comes out as var/www/my-app/... inside the -C directory. Without -C, it extracts into the current directory and can overwrite files already there. Delete /tmp/restore.sql after the test. The line in section 3 appends stdout and stderr to /var/log/backup-s3.log. If pg_dump or aws s3 cp fails, the error stays in that file instead of disappearing into cron.

When the backup fails

The cron line in section 3 appends output to /var/log/backup-s3.log. Three checks, in this order:

  • tail -n 40 /var/log/backup-s3.log shows whether pg_dump, tar, or aws s3 cp failed.
  • aws sts get-caller-identity, as the same user cron runs, confirms the credential still works. AccessDenied on s3 cp is the bucket policy, not a full disk.
  • df -h /tmp before the dump. With no free space, pg_dump dies halfway and the object in S3 is incomplete.

Conclusion

Having automated backup policies stored away from the source infrastructure is essential. By configuring encryption at the S3 level and monitoring the execution of these scripts, your business will be fully protected against accidental deletions, hardware disasters, or cyber attacks.

Managed KVM VPS

ExpertCore provisions and runs the VPS: isolated KVM, backups, and engineering support.

See VPS plans