Understanding Linux Backup and Rotation with Scripts

In Linux, managing backups and handling large amounts of data efficiently is crucial. Two common tools for this are zip and tar, which compress files to save space. Here’s a simplified guide on how to create a backup script, manage backups with rotation, and schedule tasks using cron.

Creating a Backup Script

  1. Start by Writing the Backup Script

    We'll use a script to create backups of a directory. Open a terminal and create a new file named backup.sh using the vim editor:

     vim backup.sh
    

    Enter the following script:

     #!/bin/bash
     # This script takes backup of a given directory
     # Usage: ./backup.sh <path_of_directory> <backup_directory>
    
     source_dir=$1    # The source directory to back up
     target_dir=$2    # The directory where backups will be saved
     timestamp=$(date '+%Y-%m-%d-%H-%M-%S')  # Current timestamp
    
     # Create backup
     backup_dir="${target_dir}/backup_${timestamp}"  # Backup file name with timestamp
     zip -r "${backup_dir}.zip" "${source_dir}"   # Compress and save
    
     if [ $? -eq 0 ]; then
         echo "Backup created successfully"
     else
         echo "Backup failed for $timestamp"
     fi
    

    Save and exit vim by typing :wq.

  2. Making the Script Executable

    Before running the script, make it executable:

     chmod 700 backup.sh
    
  3. Run the Backup Script

    Execute the script to back up a directory:

     ./backup.sh /home/ubuntu/scripts /home/ubuntu/backups
    

Managing Backups with Rotation

Backup rotation helps in managing storage by keeping only a fixed number of the latest backups and removing older ones. Modify the script to include rotation:

  1. Edit the Backup Script

    Open backup.sh for editing:

     vim backup.sh
    

    Update the script with rotation functionality:

     #!/bin/bash
     source_dir=$1
     target_dir=$2
     timestamp=$(date '+%Y-%m-%d-%H-%M-%S')
    
     # Create backup
     backup_dir="${target_dir}/backup_${timestamp}"
     zip -r "${backup_dir}.zip" "${source_dir}" >/dev/null
    
     if [ $? -eq 0 ]; then
         echo "Backup created successfully"
     else
         echo "Backup failed for $timestamp"
         exit 1
     fi
    
     # Perform rotation
     backups=($(ls -t "${target_dir}/backup_*.zip"))  # List backups sorted by date
     if [ "${#backups[@]}" -gt 5 ]; then
         backups_to_remove=("${backups[@]:5}")  # Get backups to remove
         for backup in "${backups_to_remove[@]}"; do
             rm "$backup"  # Remove old backups
         done
     fi
    

    Save and exit vim by typing :wq.

  2. Schedule the Backup Script Using Cron

    To automate the backup process, use cron to run the script at regular intervals. Edit the crontab:

     crontab -e
    

    Add the following line to schedule the backup to run every minute:

     * * * * * /path/to/backup.sh /home/ubuntu/scripts /home/ubuntu/backups >/dev/null 2>&1
    

    This will run the backup script every minute and suppress any output or errors.

Understanding Key Commands

  • ls -t: Lists files sorted by modification time, with the newest files first.

  • ls -t | head -5: Shows the 5 most recent files.

  • >/dev/null: Redirects output to null, effectively discarding it.

By using these scripts and cron jobs, you can automate backups, manage disk space efficiently, and ensure you always have recent backups available.