Published on

Creating automated backup with shell script and crontab

Authors
  • avatar
    Name
    Marcio Moreira Junior

In today's data-driven world, the importance of regular backups cannot be overstated. Have you considered how automated backups can save time and reduce the risk of data loss? This how-to guide will take you through creating automated backups using shell scripts and crontab, focusing on practical examples and best practices for Linux environments.


Understanding the Backup Process

Before diving into scripts, it's crucial to understand the backup process. Backups should be reliable and easily restorable. A typical practice is to implement a rotation system where backups are stored in multiple locations, such as local, remote, or cloud storage.

Setting Up Your Shell Script

Let's start by writing a simple shell script that will handle our backups. Open a terminal and create a backup script named backup.sh:

#!/bin/bash

# Variables
SOURCE='/path/to/source'
DESTINATION='/path/to/destination/backup_$(date +%F).tar.gz'

# Create a backup
/bin/tar -czf $DESTINATION $SOURCE

# Log the backup
echo "Backup of $SOURCE completed at $(date)" >> /var/log/backup.log

This script defines the source directory to back up and creates a compressed tarball in the destination folder with the current date in its name. To make the script executable, run:

chmod +x backup.sh

Automating Backups with Crontab

Now that we have our backup script, it’s time to automate its execution using crontab. This will allow us to schedule backups at regular intervals without manual intervention. To edit your crontab file, use:

crontab -e

Add the following line to the crontab file:

0 2 * * * /path/to/your/script/backup.sh

This entry schedules the backup script to run daily at 2 AM. The format consists of five fields:

  1. Minute (0 - 59)
  2. Hour (0 - 23)
  3. Day of the Month (1 - 31)
  4. Month (1 - 12)
  5. Day of the Week (0 - 6)

In this example, 0 2 * * * implies the script runs at 2 AM every day.

Verifying Backups

Post-backup verification is essential. You can create a function in your backup script to check if the file was created successfully:

if [[ -f $DESTINATION ]]; then
    echo "Backup verification successful: $DESTINATION exists." >> /var/log/backup.log
else
    echo "Backup verification failed!" >> /var/log/backup.log
fi

This adds an additional layer of assurance that your backups are being performed correctly.

Troubleshooting Common Issues

While setting up automated backups, you may encounter various issues:

  • Permission Denied: Ensure your script has execute permissions and valid read/write permissions for both source and destination directories.

  • Cron Not Running: Verify that the cron service is active. Depending on your distribution, you might use:

    sudo systemctl status cron
    
  • Logging Output: If your output isn’t being logged, check the paths used in your script and permissions for the log file. Also, you can redirect errors directly in the crontab entry:

    0 2 * * * /path/to/your/script/backup.sh >> /var/log/backup.log 2>&1
    

Conclusion

Automating backups with a shell script and crontab is a powerful method to safeguard your data. Following this guide, you can ensure your backups are regular, reliable, and easily manageable. Remember to monitor your backup logs periodically, and consider implementing incremental backups for data-heavy environments. With proper automation, you can concentrate on other critical tasks while your backups run seamlessly in the background.