Day 5 Task :- Mastering Shell Scripts for Directory Creation and Automated Backups 🗂️💾

Day 5 Task :- Mastering Shell Scripts for Directory Creation and Automated Backups 🗂️💾

Day 5 :- TASK

1.Create Directories Using Shell Script:

  • Write a bash script createDirectories.sh that, when executed with three arguments (directory name, start number of directories, and end number of directories), creates a specified number of directories with a dynamic directory name.

      <<readme
      info:This file will create directories as per given argument
      createDirectories.sh <directory name> <start number of directories> <end number of directories>
      readme
    
      directory_name=$1
      start_number=$2
      end_number=$3
    
      if [ $# -ne 3 ]; then
              echo "usage : $0 day 1 90"
              exit 1
      fi
    
      for ((i=start_number; i<=end_number; i++))
      do
              mkdir "${directory_name}${i}"
      done
    
      echo "Directories created from ${directory_name}${start_number} to ${directory_name}${end_number}"
    

Example 1: When executed as ./createDirectories.shday 1 90, it creates 90 directories as day1 day2 day3 ... day90.

  • Example 2: When executed as ./createDirectories.shMovie 20 50, it creates 31 directories as Movie20 Movie21 Movie22 ... Movie50.

2.Create a Script to Backup All Your Work:

#!/bin/bash
<<readme
info : This script will create backup
./createBackup.sh <source_dir> <target_dir>
readme

source_dir=$1
target_dir=$2
timestamp=$(date "+%Y-%m-%d-%H-%M-%S")

backup_dir="${target_dir}/backup_${timestamp}"

zip -r "${backup_dir}.zip" "${source_dir}" >  /dev/null

if [ $? -eq 0 ]; then
        echo "backup_${timestamp} created successfully"
else
        echo "Backup was not perfomed backup_${timestamp}"
fi

3.Read About Cron and Crontab to Automate the Backup Script:

  • Cron is the system's main scheduler for running jobs or tasks unattended. A command called crontab allows the user to submit, edit, or delete entries to cron. A crontab file is a user file that holds the scheduling information

      * * * * * bash /home/ubuntu/backup.sh /home/ubuntu/backup /home/ubuntu/devo
    

    4.Read About User Management:

  • A user is an entity in a Linux operating system that can manipulate files and perform several other operations. Each user is assigned an ID that is unique within the system. IDs 0 to 999 are assigned to system users, and local user IDs start from 1000 onwards.

  • Create 2 users and display their usernames.

Thank you for reading!

© 2024 Anand Raval. All rights reserved.