· 9 min read
How to Set Up Automated Server Backups on Ubuntu VPS
Learn how to set up automated server backups on Ubuntu VPS using Bash scripts and Cron. Schedule backups, secure your data, and test recovery.

· 9 min read
Learn how to set up automated server backups on Ubuntu VPS using Bash scripts and Cron. Schedule backups, secure your data, and test recovery.


· 5 min read
Use this production readiness checklist to secure, deploy, monitor and optimize your application before launching it to real users.

· 10 min read
How much does a server cost for a small business in 2026? Compare VPS, cloud, dedicated, and physical server prices, including hidden costs.

· 8 min read
AWS vs VPS for startups: compare pricing, scalability, security, performance, and management to choose the right hosting solution.
A server failure, accidental file deletion, failed deployment, or security incident can put your application and business data at risk.
That's why having a reliable backup system is essential for every production server.
However, manually creating backups every day is not always practical. You might forget to run them, miss important files, or discover that a backup is incomplete when you actually need it.
The solution is automated server backups.
In this tutorial, you'll learn how to set up automated server backups on an Ubuntu VPS using Bash, Linux tools, and Cron. We'll also cover backup scheduling, retention, remote storage, and recovery testing.
Whether you're managing a small website, a Node.js application, or a production VPS, this guide will help you build a basic backup workflow.
Automated server backups are scheduled copies of important server data that are created without requiring someone to run backup commands manually.
A backup process can automatically:
For example, you can configure your Ubuntu server to create a backup every night at 2:00 AM.
This reduces repetitive work and helps ensure that recent copies of important data are available when something goes wrong.
A production server can experience different types of failures. Without a backup, recovering from these incidents may be difficult or expensive.
Here are some common reasons to automate your backups.
A developer might accidentally delete an important configuration file or application directory. A recent backup can help restore the missing data.
A deployment can introduce unexpected problems. Having a backup of important files and configurations can make recovery easier.
Hardware problems, filesystem corruption, and other infrastructure failures can make data unavailable. An off-site backup provides another recovery option.
Automated backups run according to a schedule. You don't need to remember to execute backup commands every day.
A tested backup and recovery process helps businesses restore important services after an incident.
Before creating a backup script, identify which data your application actually needs.
Common backup targets include:
DataWhy It MattersApplication filesContains your application code and uploaded filesEnvironment configurationStores application configuration and references to secretsNginx configurationContains reverse proxy and web server settingsSSL certificatesHelps restore HTTPS configurationDatabaseStores users, transactions, and application recordsServer configurationContains important system settingsDeployment configurationIncludes Docker Compose and related deployment files
You don't necessarily need to back up every directory on your server.
For example, temporary files, caches, and package directories can often be recreated.
Also remember that database backups should use database-specific tools such as pg_dump for PostgreSQL or mysqldump for MySQL. Copying a live database directory with a normal file archive is not a substitute for a consistent database backup.
First, connect to your VPS using SSH.
ssh your_user@your_server_ip
Update the package list:
sudo apt update
Install the required tools:
sudo apt install tar gzip cron -y
Check whether Cron is running:
sudo systemctl status cron
If it is not running, enable and start it:
sudo systemctl enable --now cron
Cron will be responsible for running your backup script automatically.
Create a directory for storing backup archives.
sudo mkdir -p /var/backups/server
Set appropriate permissions:
sudo chmod 700 /var/backups/server
This restricts access to the backup directory.
Important: This directory is on the same VPS. It is useful for demonstrating the workflow, but it should not be your only backup destination. If the VPS fails or is compromised, local backups may also be lost.
For production, use a separate storage server, remote backup destination, or suitable cloud storage.
Now create a Bash script that will archive important server directories.
sudo nano /usr/local/bin/server-backup.sh
Add the following script:
#!/bin/bash
set -Eeuo pipefail
BACKUP_DIR="/var/backups/server"
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_FILE="$BACKUP_DIR/server-backup-$TIMESTAMP.tar.gz"
LOG_FILE="/var/log/server-backup.log"
mkdir -p "$BACKUP_DIR"
echo "[$(date)] Starting server backup..." >> "$LOG_FILE"
tar -czf "$BACKUP_FILE" \
--exclude="/var/www/*/node_modules" \
--exclude="/var/www/*/.git" \
--exclude="/var/cache" \
--exclude="/var/tmp" \
-C / \
etc \
var/www \
home \
usr/local
echo "[$(date)] Backup completed: $BACKUP_FILE" >> "$LOG_FILE"
find "$BACKUP_DIR" \
-type f \
-name "server-backup-*.tar.gz" \
-mtime +7 \
-delete
echo "[$(date)] Old backups cleaned up." >> "$LOG_FILE"
Save the file.
This script performs several tasks:
The exact directories should be adjusted to match your server. Review the paths and exclusions before using the script.
If your application files are stored outside /var/www or /home, add the correct directories.
Also, this example does not create a consistent database backup. Add a separate database export step before relying on the workflow for production recovery.
Run:
sudo chmod 700 /usr/local/bin/server-backup.sh
This allows the root user to execute the script while restricting access to it.
Before scheduling automated execution, run the script manually.
sudo /usr/local/bin/server-backup.sh
If the backup completes successfully, list the generated archive:
sudo ls -lh /var/backups/server/
You should see a file with a name similar to:
server-backup-2026-11-12_02-00-00.tar.gz
Check the log:
sudo tail -n 20 /var/log/server-backup.log
Testing the script manually helps identify permission errors, missing directories, and other issues before automation begins.
Now let's configure the server to run the backup script automatically.
Open the root user's Cron configuration:
sudo crontab -e
Add this line:
0 2 * * * /usr/local/bin/server-backup.sh
This schedules the backup script to run every day at 2:00 AM according to the server's local time.
Here is how the Cron expression works:
FieldValueMeaningMinute0At minute zeroHour2At 2 AMDay*Every dayMonth*Every monthWeekday*Every day of the week
Other useful Cron examples:
0 1 * * * /usr/local/bin/server-backup.sh
Runs every day at 1:00 AM.
0 2 * * 0 /usr/local/bin/server-backup.sh
Runs every Sunday at 2:00 AM.
0 3 1 * * /usr/local/bin/server-backup.sh
Runs on the first day of every month at 3:00 AM.
To check your scheduled jobs:
sudo crontab -l
Your Ubuntu VPS can now run the backup script automatically according to the schedule.
Keeping backups on the same VPS is not enough for reliable disaster recovery.
If the server becomes unavailable, local backups may become inaccessible too.
A better approach is to copy completed backups to a separate server using SSH and Rsync.
Install Rsync if needed:
sudo apt install rsync -y
For example, you can transfer your backup directory to a remote server:
rsync -avz /var/backups/server/ \
backupuser@backup-server-ip:/remote-backups/my-server/
Replace:
backupuser with your remote server username.backup-server-ip with the destination server IP./remote-backups/my-server/ with the correct destination directory.For automation, configure SSH key-based authentication and restrict the backup account's permissions.
Do not place passwords directly inside scripts or Cron entries.
For sensitive production data, use encrypted backups and protect the encryption keys separately from the backup files.
Also consider network failures, remote storage permissions, and transfer verification.
Application files are only one part of your production environment.
Your database may contain the most important information, including user accounts, orders, customer records, and application data.
For PostgreSQL, a basic logical backup can be created using:
pg_dump -U your_db_user \
-d your_database \
-F c \
-f database-backup.dump
For MySQL, you can use:
mysqldump -u your_db_user \
-p your_database \
> database-backup.sql
These commands are examples. In an automated production script, configure authentication securely rather than exposing database passwords in command history or scripts.
Schedule database backups according to your application's data-change rate and recovery requirements.
For PostgreSQL, also consider whether your application needs point-in-time recovery using continuous WAL archiving rather than relying only on periodic logical dumps.
Creating backups is not enough.
You must also verify that the files can actually be restored.
First, inspect the contents of a compressed archive:
tar -tzf /var/backups/server/server-backup-YYYY-MM-DD_HH-MM-SS.tar.gz
To extract the archive into a separate test directory:
sudo mkdir -p /tmp/backup-restore-test
sudo tar -xzf /var/backups/server/server-backup-YYYY-MM-DD_HH-MM-SS.tar.gz \
-C /tmp/backup-restore-test
Replace the example filename with the actual backup filename.
Check the extracted files before considering the restore successful.
For database backups, test restoring them into a separate test database. Do not overwrite your live production database just to test a backup.
Regular restoration tests help confirm that your backups are usable when you need them.
Even an automated backup system can fail if it is not configured correctly.
Avoid these common mistakes:
Local backups can be lost along with the original server. Maintain a separate off-site copy.
A backup archive may exist but still be incomplete, corrupted, or missing important files.
Test recovery regularly.
Application files do not contain all the data needed to restore a database-driven application.
Use database-specific backup tools.
Unlimited backups can consume storage and affect server performance.
Use a defined retention policy based on recovery requirements and available storage.
Backups may contain sensitive customer information, application configuration, or credentials.
Use restricted permissions, encryption, secure transfers, and controlled access.
A scheduled task can fail because of permission issues, disk space, or network problems.
Monitor backup logs and alert someone when a backup fails.
For a more reliable production backup strategy, follow these recommendations:
A useful approach is the 3-2-1 backup strategy: maintain three copies of important data, use two different storage types, and keep one copy off-site.
The exact strategy should reflect your application's availability, compliance, and recovery requirements.
Automated server backups are an essential part of managing a reliable production environment.
With a Bash script and Cron, you can automate a basic backup workflow on Ubuntu VPS. Adding remote storage, database-specific backups, monitoring, and regular restore testing makes the process much more dependable.
Remember: a backup is only useful when you can restore it successfully.
If you're running a production application, don't wait for a server failure to discover that your backup system isn't working.
At ItTechSoft, we help businesses deploy, secure, and maintain production applications with reliable infrastructure, server management, and backup solutions.
Deploy. Secure. Scale.
Learn more about our services at ItTechSoft.