Day 11 : Improving Backup Rotation Script with Trap Function

In my DevOps journey, scripting efficient and reliable backup processes is key. Earlier, I practiced a backup rotation script to manage backups of a directory with a 5-day rotation.

Today, I learned trap function and implemented to the backup script and improved it. This helps handle interruptions smoothly and ensures everything is cleaned up properly.

Backup and Rotation Script: An Overview

Backup Function

The main part of the script is the create_backup function. It creates a compressed backup of the target directory and names it with a timestamp to keep each backup unique.

Rotation Function

Rotation function to manage the backup rotation. It moves old backups (older than 5 days) to a temporary directory and deletes them after a short delay. This way, only the latest five backups are kept, saving storage space.

Adding the Trap Function

Why Use Trap in This Script

The trap function in shell scripting catches signals and runs specified commands when those signals are received. It ensures cleanup actions happen even if the script gets interrupted. In this script, trap prevents incomplete backup processes from leaving the system in a mess, ensures temporary files are cleaned up, and restores any backup files that were moved for rotation. This way, we make sure no backups are lost, and the system stays consistent.

Implementing Trap for Cleanup

I added the trap function to call the cleanup function when the script exits (EXIT), is interrupted (SIGINT), or is terminated (SIGTERM). The cleanup function checks if there are any backups in the temporary directory and restores them if needed, making sure no data is lost.

Complete Script with Trap Function

Here's the complete improved script:

#!/bin/bash
# This script is for backup of the work_dir with 5-day rotation

SOURCE="/root/my_dir/work_dir"
TIME_STAMP=$(date +%Y_%m_%d_%H%M%S)
DEST="/root/my_dir/backup"
TEMP_DIR="/root/my_dir/temp_backup"

# Function to create backup
function create_backup {
    zip -r "$DEST/backupfile_${TIME_STAMP}.zip" "$SOURCE" >/dev/null
    if [ $? -eq 0 ]; then
        echo "Backup created successfully for $TIME_STAMP"
    else
        echo "Backup failed for $TIME_STAMP"
        exit 1
    fi
}

# Function to move old backups to a temporary directory
function move_old_backups {
    mkdir -p "$TEMP_DIR"
    if [ $? -eq 0 ]; then
        echo "Temporary directory $TEMP_DIR created"
    fi
    backups=($(ls -t "$DEST"/backup*zip))
    backups_to_move=("${backups[@]:5}")
    for backup in "${backups_to_move[@]}"; do
        mv "$backup" "$TEMP_DIR/"
    done
    sleep 5
}

# Function to delete old backups in the temporary directory
function delete_old_backups {
    rm -rf "$TEMP_DIR"/*
}

# Function to restore backups from the temporary directory in case of interruption
function restore_backups {
    mv "$TEMP_DIR"/* "$DEST/"
}

# Function to handle cleanup on exit
function cleanup {
    if [ -d "$TEMP_DIR" ] && [ $(ls -A $TEMP_DIR) ]; then
        echo "Restoring backups from temporary directory..."
        restore_backups
    else
        echo "Cleaning up temporary files..."
        delete_old_backups
    fi
    rmdir "$TEMP_DIR" > /dev/null 2>&1
}

# Setting trap to call cleanup function on EXIT, SIGINT, and SIGTERM
trap cleanup EXIT SIGINT SIGTERM

# Main execution
create_backup
move_old_backups
delete_old_backups

trap function made the backup rotation script more robust and reliable. Now, the script ensures backups are not lost, temporary files are cleaned up, and any rotated backup files are restored if needed, even if the script is interrupted.

This practice not only improves the script’s functionality but also follows DevOps best practices, keeping systems consistent and reliable.

Thanks for reading! Stay tuned for more interesting blog coming soon.