Day-2 @BashBlaze-7-Days-of-Bash-Scripting-Challenge

1. Interactive File and Directory Explorer


##############################################################################################
#!/bin/bash                                                                                  #
##############################################################################################
# Author : Gurudeo ray                                                                       #
##############################################################################################
#                                                                                            #
# About : Developed a script to full fill Day 2 Bashblaze challenge  (Interactive File and   # 
#         Directory Explorer)                                                                #
#                                                                                            #
##############################################################################################

echo "Welcome to Day2 chalenge"

#Printing all the file and directroies with their size in human redable format
echo "Files and directories in current path: "
ls -lh | awk -F " " '{ print $9 " ("$5")"}'

isCondition=true

#Creating interactive while loop to count characters provided by user as a input
while( $isCondition ); do 
    count=0
    read -p "Enter a line or characters (Press enter without any input to exit): " inputstring
    if [[ $inputstring != "" ]];then
        for(( i=0; i<${#inputstring}; i++ )); do
            $(( count++ )) 2>/dev/null
        done
        echo "Character count : $count"
    else
        exit;
    fi 
done

2. Directory Backup with Rotation

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

  • Additionally, we have implemented 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 will be removed to ensure only the most recent backups are retained.

    %[github.com/raygurudeo/BashBlaze-7-Days-of-B..

      ##############################################################################################
      #!/bin/bash                                                                                  #
      ##############################################################################################
      # Author : Gurudeo ray                                                                       #
      ##############################################################################################
      #                                                                                            #
      # About : Developed a shell script to backup resources, present in the directory provided    #
      #         by a user as a input.                                                              #
      #                                                                                            #
      #                                                                                            #
      ##############################################################################################
      #                                                                                            #
      # Execution : ./<script-name> <location to take backup>                                      #
      #                                                                                            #
      # Example   :  ./backup_with_rotation.sh /home/guru/Documents/                               #
      #                                                                                            #
      ##############################################################################################
    
      # Check if user has executed script correctly or not ?
      if [ $# -eq 1 ]; then
    
          # Taking directory input from user
          backup_input_dir=$1
    
          # Checking if user has entered correct location and Creating backup storage location (storage directory)
          if [ -d $backup_input_dir ]; then
    
              # Latest timestamp
              time_stamp="$(date +'%Y-%m-%d_%H-%M-%S')"
    
              # Locatin to store all the backups
              backup_file_path=`realpath $backup_input_dir`
              backup_storage_location=$backup_file_path"/backup_"$time_stamp
    
              # Backup file name with latest timestamp
              backup_file_name=backup_$time_stamp"".tar.gz
    
              mkdir -p $backup_storage_location
              echo "Backup storage location ( $backup_storage_location ) created successfully..."
    
              # Taking backup and storing at the backup storage location
              tar -cvf ${backup_storage_location}/${backup_file_name} ${backup_input_dir} 2>/dev/null 1>/dev/null
              if [ $? -eq 0 ]; then
                  echo "Backup created: $backup_storage_location/$backup_file_name"
              else
                  echo "Error: Failed to create a backup !!!"
                  exit 1
              fi
    
              # Steps to keep latest 3 backups and delete old backups
              for backup_list_item in $(ls -t $backup_input_dir | tail -n +4 | grep backup); do
                  rm -r $backup_input_dir/$backup_list_item
              done
    
          else
              echo "Error: Entered location is not correct.!!"
          fi
    
      else
          echo "Error: Please execute script correctly as given in the execution section of the Script header."
          head -17 backup_with_rotation.sh
          exit 1
      fi
    
      exit 0