Day 9 Challenge: Mastering Shell Script for Directory Backup and Rotation

Day 9 Challenge: Mastering Shell Script for Directory Backup and Rotation

Task

ยท

2 min read

Create a bash script that takes a directory path as a command-line argument and performs a backup of the directory. The script should create timestamped backup folders and copy all the files from the specified directory into the backup folder.

Additionally, the script should implement a rotation mechanism to keep only the last 3 backups. This means that if there are more than 3 backup folders, the oldest backup folders should be removed to ensure only the most recent backups are retained.

  1. created sample files
ubuntu@ip-172-31-45-42:~/devops_backup/Text_files$ touch File{6..8}.txt

  1. install zip

        sudo apt install zip
    
  2. Shell script named backup_with_rotation.sh

     #!/bin/bash
     # Display usage information
     function display_usage {
         echo "Usage: $0 /path/to/source_directory"
     }
    
     # Check if a valid directory path is provided as a command-line argument
     if [ $# -eq 0 ] || [ ! -d "$1" ]; then
         echo "Error: Please provide a valid directory path as a command-line argument."
         display_usage
         exit 1
     fi
    
     # Assign source directory from command-line argument
     source_dir="$1"
    
     # Function to create a timestamped backup and zip it
     function create_backup {
         local timestamp=$(date '+%Y-%m-%d_%H-%M-%S')
         local backup_dir="${source_dir}/backup_${timestamp}"
    
         # Create backup directory and zip its contents
         zip -r "${backup_dir}.zip" "$source_dir" >/dev/null
         if [ $? -eq 0 ]; then
             echo "Backup created successfully: ${backup_dir}.zip"
         else
             echo "Error: Failed to create backup."
         fi
     }
    
     # Function to perform rotation and keep only the last 3 backups
     function perform_rotation {
         local backups=($(ls -t "${source_dir}/backup_"*.zip 2>/dev/null))
    
         if [ "${#backups[@]}" -gt 3 ]; then
             local backups_to_remove=("${backups[@]:3}")
             for backup in "${backups_to_remove[@]}"; do
                 rm -f "$backup"
             done
         fi
     }
    
  3. Permission

  4. Run script

  5. Output

This script ensures you always have your directory's most recent 3 backups, helping you manage storage efficiently.

ย