Skip to main content

Command Palette

Search for a command to run...

Day 9 Task: Shell Scripting Challenge Directory Backup with Rotation

Updated
2 min read
Day 9 Task: Shell Scripting Challenge Directory Backup with Rotation
R

Hi, I’m Rakshita. A Cloud, DevOps, AI, and Python enthusiast passionate about learning and simplifying technology for others. I love exploring how modern tools and automation can make systems smarter and more efficient. Here, I write about: ☁️ Cloud & DevOps practices 🤖 AI in the world of automation 🐍 Python for real-world problem-solving 💡 Growth, consistency, and the learner’s mindset My goal is to bridge the gap between learning and doing, and help others grow confidently in the evolving tech landscape.

TASK:

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. Create two directories- One is to write the shell script (scripts), another is to take the backup (backups):

     mkdir scripts backups
    
  2. Enter into the scripts directory and make a file named backup_with_rotation.sh:

  3. Insert the script:

     #!/bin/bash
    
     << readme
     This is a script to take a backup of directory
     usage:
     ./backup_with_rotation.sh <path of directory> <path of backup directory>
    
     readme
    
     source_dir=$1
     backup_dir=$2
     timestamp=$(date '+%Y-%m-%d-%H-%M-%S')
    
     function create_backup {
     zip -r "${backup_dir}/backup_${timestamp}.zip" "${source_dir}" > /dev/null
    
     if [ $? -eq 0 ]; then
    
             echo "Backup created successfully"
     else
             echo "Backup was not performed for $timestamp"
     fi
     }
    
     function perform_rotation {
                 backups=($(ls -t "$backup_dir/backup_"*.zip))
    
                 if [ "${#backups[@]}" -gt 3 ]; then
                         backups_to_remove=("${backups[@]:3}")
                         for backup in "${backups_to_remove[@]}";
                         do
                                 rm "$backup"
                         done
                 fi
     }
    
     create_backup
     perform_rotation
    
  4. Change the permissions of this file to make it executable:

     chmod 700 backup_with_rotation.sh
    
  5. Install zip:

        sudo apt install zip
    
  6. Execute:

     ./backup_with_rotation.sh /home/ubuntu/scripts /home/ubuntu/backups
    

    Here,

    /home/ubuntu/scripts = path of directory

    /home/ubuntu/backups = path of backup directory

  7. Take backup 4-5 times:

  8. Check if the last 3 backups are there in your backups directory:

Conclusion

In this guide, we've walked through the creation of a robust bash script that automates the backup of directories with ease. By taking a directory path as a command-line argument, this script efficiently creates timestamped backup folders, ensuring that each backup is neatly organized and easily identifiable.

Moreover, we've implemented a rotation mechanism to keep only the last three backups, effectively managing storage space while maintaining the most recent data. This approach not only simplifies the backup process but also ensures that your system remains clutter-free.

Remember, automation is key to efficiency, and this backup script is a small yet powerful step in that direction.

Happy scripting!