Hello, I'm Arthur! I enjoy exploring Linux administration, server automation, and practical tools that make managing infrastructure easier.
Imagine working on a website or application for several months and suddenly losing important files because of an unexpected server failure.
It can happen due to accidental deletion, software problems, disk failures, or security incidents.
This is why regular backups are important.
Linux provides several tools that make server backups easier. One useful combination is tar, cron, and a separate backup storage location.
In this tutorial, we'll create a simple automated backup system.
1. Create a Backup Directory
First, create a directory where backup files will be temporarily stored.
sudo mkdir -p /backup You can check whether the directory exists using:
ls -ld /backup Make sure the account running the backup process has the required permissions.
2. Create a Backup Script
Instead of manually running backup commands every day, create a shell script.
Open a new file:
sudo nano /usr/local/bin/server-backup.sh Add the following code:
#!/bin/bash SOURCE="/var/www" DEST="/backup" DATE=$(date +%Y-%m-%d_%H-%M) mkdir -p "$DEST" tar -czf "$DEST/website-$DATE.tar.gz" "$SOURCE" find "$DEST" -type f -name "website-*.tar.gz" -mtime +7 -delete echo "Backup completed: $DATE" This script performs three main tasks:
- Compresses website files into a backup archive.
- Adds the current date to the filename.
- Removes matching backup archives older than seven days.
The example assumes your website files are stored in /var/www. Change the source directory according to your application.
3. Make the Script Executable
Run:
sudo chmod +x /usr/local/bin/server-backup.sh This allows the system to execute the script.
4. Test the Backup Manually
Before scheduling automation, test the script.
Run:
sudo /usr/local/bin/server-backup.sh Then check the backup directory:
ls -lh /backup You should see a compressed archive containing your website files.
5. Schedule Automatic Backups with Cron
Cron is a Linux utility that runs scheduled tasks automatically.
Open the root user's cron configuration:
sudo crontab -e Add this line:
0 2 * * * /usr/local/bin/server-backup.sh >> /var/log/server-backup.log 2>&1 This schedules the backup to run every day at 2:00 AM according to the server's local time.
The log output is saved to /var/log/server-backup.log.
6. Verify the Scheduled Job
To view scheduled cron jobs, run:
sudo crontab -l You can also inspect the backup log:
sudo tail -n 20 /var/log/server-backup.log Regularly checking the logs helps identify failed backup attempts.
7. Test Your Backup Files
Creating an archive does not automatically guarantee that it can be restored.
You can inspect its contents with:
tar -tzf /backup/website-2026-11-22_02-00.tar.gz Replace the filename with your actual archive name.
For a reliable backup strategy, periodically test restoring files to a separate directory.
8. Keep Backups Outside Your Server
There is one important limitation to this example.
If your VPS experiences complete disk failure, backups stored on the same disk may also be lost.
For better protection, copy backups to separate storage or a remote backup server.
You should also consider:
- Encrypting sensitive backup files.
- Restricting access to backup directories.
- Keeping multiple backup versions.
- Monitoring backup failures.
- Testing the complete restoration process.
For projects that need a reliable hosting environment, developers can explore HelloServer VPS Hosting for virtual server resources.
If an application requires dedicated hardware, HelloServer Dedicated Server Hosting is another option to evaluate.
Final Thoughts
Automated backups are one of the most useful habits in server administration.
With a simple Bash script and cron, you can schedule regular backups without manually repeating the same commands.
However, a backup is only useful when it can actually be restored.
Automate the process, monitor its results, and always keep an additional copy somewhere outside your primary server.