Skip to main content

Command Palette

Search for a command to run...

Day 5 Task :- Mastering Shell Scripts for Directory Creation and Automated Backups ๐Ÿ—‚๏ธ๐Ÿ’พ

Updated
โ€ข2 min read
Day 5 Task :- Mastering Shell Scripts for Directory Creation and Automated Backups ๐Ÿ—‚๏ธ๐Ÿ’พ
A

"I'm a 3rd-year Computer Engineering student at Marwadi University with skills in C++, web development (MERN stack), and DevOps tools like Kubernetes. I contribute to open-source projects and share tech knowledge on GitHub and LinkedIn. I'm learning cloud technologies and app deployment. As an Internshala Student Partner, I help others find jobs and courses." now currently focusing on #90DaysOfDevops

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.

90DaysOfDevops

Part 42 of 49

So In This Series I am following 90 DaysOfDevops challenge , what i will learn in my devops journey i will share with you with my blogs ,You will get blog on tools which used by devops engineer , example :- Linux,ansible,terraform,docker,etc

Up next

Day 4 Task :- Exploring Kernel, Shell, and Shell Scripting ๐Ÿš€

Day 4 :- TASK โ€ข What is Kernel ? Kernel is the core component of a operating system, acting as the main interface between the computer's hardware and its processes. It manages system resources efficiently, including memory management, process manage...

More from this blog

A

Anand Raval

118 posts

Hello I am Anand Raval , i contributed my work in robotics(arduino uno) , fronted web devloper,competitive programming, now currently focusing on #90DaysOfDevops