Hello, I'm Arthur.
Imagine working on a website for months, only to lose important files because of a failed update or accidental deletion.
This is why backups are an essential part of server management.
A reliable backup system helps you recover important data when something goes wrong. However, creating a backup manually every few weeks is not always enough.
In this guide, we'll explore how to create automated Linux backups, schedule them with cron, and verify that your data can be restored when needed.
1. Decide What Needs to Be Backed Up
Before creating a backup script, identify the data that is important to your application.
For a typical website, this may include:
- Website files and application code
- Database contents
- Configuration files
- Uploaded images and documents
- Important application settings
For example, a WordPress website needs both its files and database to recover properly.
Backing up only the website directory may not be enough because posts, user accounts, and other content are stored in the database.
Avoid backing up temporary files or unnecessary cache directories unless your application specifically requires them.
2. Create a Backup Directory
Start by creating a directory where backup files will be stored.
sudo mkdir -p /var/backups/website
Restrict access to the directory:
sudo chmod 700 /var/backups/website
This helps prevent unauthorized users from accessing the backup files.
However, storing backups on the same server is not sufficient for disaster recovery.
If the server's disk fails or the entire system becomes unavailable, both the original data and its backups may be lost.
We'll address this by using a separate storage location later in the guide.
3. Create a Simple Backup Script
A shell script can automate the process of creating compressed backups.
For example, create a script:
sudo nano /usr/local/bin/website-backup.sh
Add the following code:
#!/bin/bash
set -euo pipefail
SOURCE="/var/www/html"
BACKUP_DIR="/var/backups/website"
DATE=$(date +%F_%H-%M-%S)
mkdir -p "$BACKUP_DIR"
tar -czf "$BACKUP_DIR/website-$DATE.tar.gz" \
-C "$SOURCE" .
Save the file.
This script creates a compressed archive of the website directory with a timestamp in its filename.
For example:
website-2026-11-30_02-00-00.tar.gz
The timestamp helps identify when each backup was created.
Make the script executable:
sudo chmod 700 /usr/local/bin/website-backup.sh
Before scheduling it, run the script manually and confirm that it creates the expected archive.
4. Back Up Your Database
If your application uses a database, it should be backed up separately.
For a MySQL or MariaDB database, you can use mysqldump.
For example:
mysqldump -u backupuser -p \
--single-transaction \
--databases website_db \
> /var/backups/website/database.sql
Replace backupuser and website_db with your actual database username and name.
The command prompts for the database password.
The --single-transaction option can provide a consistent backup for transactional tables such as InnoDB, without locking those tables for the entire operation.
For production environments, review your database engine, storage requirements, and backup consistency needs before relying on this approach.
Also, avoid storing database passwords directly in scripts or command history.
5. Schedule Automatic Backups With Cron
Once the backup script works correctly, you can schedule it to run automatically.
Open the cron configuration for the appropriate user:
sudo crontab -e
To run the backup every day at 2 AM, add:
0 2 * * * /usr/local/bin/website-backup.sh
Cron uses five time fields:
- Minute
- Hour
- Day of the month
- Month
- Day of the week
The example runs the backup once every day at 2 AM.
Choose a schedule that matches your application's data changes and recovery requirements.
For a website that receives frequent orders or customer updates, daily backups may not be sufficient.
6. Store Backups Outside the Server
A backup stored on the same server can be lost if the server experiences a major failure.
A better approach is to copy backups to separate storage.
This could be:
- A remote backup server
- Object storage
- A separate storage system
- An off-site backup service
For example, rsync can transfer files to another Linux server over SSH:
rsync -avz /var/backups/website/ \
backupuser@backup-server:/backups/website/
Replace the username and destination with your actual backup server details.
For automated production backups, configure SSH key authentication securely and restrict the backup account's permissions.
You can also use encryption to protect sensitive backup data while it is stored or transferred.
7. Remove Old Backups Safely
Backups can consume significant storage over time.
A retention policy determines how long backup copies should be kept.
For example, you might keep daily backups for a limited period and retain selected weekly or monthly copies for longer.
Before deleting old backups, verify that newer copies exist and that they are complete.
Avoid using broad deletion commands without checking the target directory first.
For important systems, consider backup software that supports retention policies, encryption, verification, and recovery management.
8. Test Your Backup and Recovery Process
A successful backup command does not guarantee that the data can be restored correctly.
Periodically test the recovery process in a separate environment.
For a compressed archive, you can list its contents with:
tar -tzf website-backup.tar.gz
This allows you to inspect the archive without extracting it.
For a real recovery test, restore the files and database to a separate test environment and verify that the application works.
A good recovery test should confirm that:
- The files are complete.
- The database can be imported.
- Configuration settings are available.
- The application starts correctly.
- The recovered data meets your recovery requirements.
Testing backups is especially important for websites and applications that store customer information or business records.
9. Choose Hosting That Supports Your Backup Strategy
Your backup requirements depend on the amount of data your application stores and how quickly it needs to recover.
For general websites and applications, Seimaxim VPS hosting is an option to explore when evaluating server infrastructure.
If your workload involves large datasets, AI development, or GPU-based computing, Seimaxim GPU dedicated servers provide another infrastructure option to consider.
Regardless of the hosting environment, keep backup copies separate from the original data and test your recovery process regularly.
Final Thoughts
Automated backups can make Linux server management more reliable and reduce the risk of losing important data.
Start by identifying the files and databases that matter, create a backup script, and schedule it to run automatically.
Then store copies outside the server, maintain a sensible retention policy, and test the recovery process.
A backup strategy is only complete when you know that your data can be restored successfully.
Top comments (0)