# Day 5: Advanced Linux Shell Scripting for DevOps Engineers with User Management

Hello, DevOps Enthusiasts! 👋

As part of my **#90DaysOfDevOps** challenge, I dived into **Advanced Linux Shell Scripting** today, focusing on directory creation, backups, automation with crontab, and user management. Here's a detailed walkthrough of today's tasks and my solutions.

---

### 📌 **Task 1: Create Directories Using Shell Script**

#### Objective:

Create a bash script `createDirectories.sh` to generate a specified number of directories dynamically based on user inputs.

#### Code:

```bash
#!/bin/bash

<<Info
Author       : Amitabh Soni  
Date         : 25/11/24  
Description  : This script takes three arguments and creates a specified number of directories at once.  

Example      :  
./createDirectories.sh day 1 90 → Creates directories day1, day2, ..., day90.  
Info

# Loop through the range of directory numbers
for i in $(seq $2 $3); do
    mkdir "$1$i"
done
```

#### Example Execution:

* `./createDirectories.sh day 1 90` → Creates `day1` to `day90`.
    
* `./createDirectories.sh Movie 20 50` → Creates `Movie20` to `Movie50`.
    

Check out the script and output images below:  

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732554096225/c11d4392-c6bc-42aa-bcad-c61e399222f4.png align="center")

  

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732554103052/ea3135a1-5440-45bb-8a04-800584ed4f4c.png align="center")

---

### 📌 **Task 2: Backup Script**

#### Objective:

Write a script to back up a directory or file and save it in a predefined backup folder.

#### Code:

```bash
#!/bin/bash

<<Info
Author       : Amitabh Soni  
Date         : 25/11/24  
Description  : This script creates a zip backup of a directory or file and stores it in the backup directory.  
Info

# Function to create a backup
function create_backup() {
    timestamp=$(date '+%Y-%m-%d_%H-%M-%S')  
    target_dir="/home/ubuntu/Day-05/backup"  
    backup_file="${target_dir}/backup_${timestamp}.zip"  

    # Create the zip file
    zip -r "$backup_file" "$1"  

    if [ $? -eq 0 ]; then
        echo "Backup created: $backup_file"  
    else
        echo "Error: Backup failed."  
    fi
}

create_backup "$1"
```

#### Steps:

1. Save the script as `backup.sh`.
    
2. Run it with the directory or file you want to back up:
    
    ```bash
    ./backup.sh /path/to/directory_or_file
    ```
    

#### Output:

* Script execution screenshot:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732554138632/b1b43a77-e5cd-4932-b543-15f5e16b0ec5.png align="center")
    
* Backup folder after execution:  
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732554161556/ebcf0c7f-bd83-45ff-ac6a-1fd2c0541d23.png align="center")
    

---

### 📌 **Task 3: Automating Backups with Cron and Crontab**

#### Objective:

Automate the `backup.sh` script to run at regular intervals using **crontab**.

#### Steps:

1. Open the crontab editor:
    
    ```bash
    crontab -e
    ```
    
2. Add the following line to run the backup every minute:
    
    ```bash
    * * * * * /path/to/backup.sh /path/to/directory_or_file
    ```
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732554265638/0bf24d2e-1150-48b3-bff7-715dee04cc54.png align="center")

#### Verifications:

* Watching real-time backups with `watch ls`:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732554195147/1542dcae-f567-467f-b37e-1950396d0c60.png align="center")
    
* Final backup check:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732554205911/ff13c8f9-7b34-469d-9ca1-6da077a30b4c.png align="center")
    

---

### 📌 **Task 4: User Management in Linux**

#### Objective:

Create two users and display their usernames.

#### Steps:

1. Use `adduser` command to create users:
    
    ```bash
    sudo adduser username1  
    sudo adduser username2  
    ```
    
2. Display usernames:
    
    ```bash
    cut -d: -f1 /etc/passwd | tail -2
    ```
    

#### Execution:

* Adding users:
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732554321429/297bec8c-f7bf-4d47-b3ed-8148b2a2331b.png align="center")
    
* Password setup:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1732554338446/c3cb39be-a2ed-49ab-b8ef-6889e77ebc9b.png align="center")

---

### 💡 **Key Learnings:**

* Leveraged **shell scripting** for dynamic tasks like directory creation and backups.
    
* Automated routine tasks with **cron and crontab**.
    
* Explored the fundamentals of **Linux user management**.
    

### 🔗 **Check Out My Progress:**

* [LinkedIn](https://www.linkedin.com/in/amitabh-devops)
    
* [GitHub Repo](https://github.com/Amitabh-DevOps/90DaysOfDevOps/blob/feat/day05/2024/day05/solution.md) (Pull request)
    

Stay tuned for more updates from my #90DaysOfDevOps challenge! 🚀

---

What do you think about these tasks? Feel free to share your thoughts or ask questions in the comments below! 😊
