0% found this document useful (0 votes)
10 views3 pages

60bdd5f98f2cc100695558c4-Bash Scripting Exercise Enterprise-Level Automation-220724-211738

The document outlines a Bash scripting exercise for DevOps engineers focused on automating user management, service monitoring, and file operations in an enterprise environment. It includes detailed requirements and steps for creating a user, checking and managing the nginx service, organizing log files, and scheduling the script with cron. Completing this exercise provides practical experience relevant to real-world enterprise automation tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

60bdd5f98f2cc100695558c4-Bash Scripting Exercise Enterprise-Level Automation-220724-211738

The document outlines a Bash scripting exercise for DevOps engineers focused on automating user management, service monitoring, and file operations in an enterprise environment. It includes detailed requirements and steps for creating a user, checking and managing the nginx service, organizing log files, and scheduling the script with cron. Completing this exercise provides practical experience relevant to real-world enterprise automation tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Bash Scripting Exercise: Enterprise-Level Automation

Bash Scripting Exercise: Enterprise-Level Automation

Scenario

You are a DevOps engineer in a large enterprise. Your tasks include managing user accounts, monitoring services, and automating routine
file operations. In this exercise, you will write a Bash script to automate these tasks.

Requirements
1. User Management
Create a new user named enterprise_user .
Add enterprise_user to a group called enterprise_group .
Set a password for enterprise_user .
Ensure the password expires every 90 days.
2. Service Monitoring
Check if the nginx service is running.
If it is not running, start the service and log the action to a file.
3. File Operations
Create a directory structure /enterprise/data/logs and /enterprise/data/backup .
Move all .log files from /var/log to /enterprise/data/logs .
Archive the /enterprise/data/logs directory into a tarball named logs_backup.tar.gz and move it to
/enterprise/data/backup .

4. Automation and Logging


Schedule the script to run every day at 2 AM using cron.
Log all actions taken by the script to a file /var/log/enterprise_script.log .

Steps to Complete the Exercise

Step 1: User Management

1 #!/bin/bash
2
3 # Create a new user
4 sudo useradd -m enterprise_user
5
6 # Create a new group and add the user to the group
7 sudo groupadd enterprise_group
8 sudo usermod -aG enterprise_group enterprise_user
9
10 # Set a password for the user
11 echo "enterprise_user:password123" | sudo chpasswd
12
13 # Set password expiration policy
14 sudo chage -M 90 enterprise_user
Step 2: Service Monitoring

1 # Check if nginx service is running


2 if ! systemctl is-active --quiet nginx; then
3 # Start the nginx service
4 sudo systemctl start nginx
5 # Log the action
6 echo "$(date): nginx service was not running and has been started." >> /var/log/enterprise_script.log
7 else
8 echo "$(date): nginx service is running." >> /var/log/enterprise_script.log
9 fi

Step 3: File Operations

1 # Create the directory structure


2 sudo mkdir -p /enterprise/data/logs
3 sudo mkdir -p /enterprise/data/backup
4
5 # Move .log files to the logs directory
6 sudo mv /var/log/*.log /enterprise/data/logs/
7
8 # Archive the logs directory
9 sudo tar -czvf /enterprise/data/backup/logs_backup.tar.gz -C /enterprise/data logs
10
11 # Log the action
12 echo "$(date): .log files moved and archived." >> /var/log/enterprise_script.log

Step 4: Automation with Cron

To automate this script, you need to add it to the cron jobs:

1 # Open the crontab editor


2 crontab -e
3
4 # Add the following line to schedule the script
5 0 2 * * * /path/to/your/script.sh

Final Script

Combine all parts into a single script and ensure it has execution permissions:

1 #!/bin/bash
2
3 # Step 1: User Management
4 sudo useradd -m enterprise_user
5 sudo groupadd enterprise_group
6 sudo usermod -aG enterprise_group enterprise_user
7 echo "enterprise_user:password123" | sudo chpasswd
8 sudo chage -M 90 enterprise_user
9
10 # Step 2: Service Monitoring
11 if ! systemctl is-active --quiet nginx; then
12 sudo systemctl start nginx
13 echo "$(date): nginx service was not running and has been started." >> /var/log/enterprise_script.log
14 else
15 echo "$(date): nginx service is running." >> /var/log/enterprise_script.log
16 fi
17
18 # Step 3: File Operations
19 sudo mkdir -p /enterprise/data/logs
20 sudo mkdir -p /enterprise/data/backup
21 sudo mv /var/log/*.log /enterprise/data/logs/
22 sudo tar -czvf /enterprise/data/backup/logs_backup.tar.gz -C /enterprise/data logs
23 echo "$(date): .log files moved and archived." >> /var/log/enterprise_script.log

Make sure to give the script execution permissions:

1 chmod +x /path/to/your/script.sh

Conclusion

This exercise covers various aspects of enterprise-level Bash scripting, including user management, service monitoring, file operations, and
automation using cron. By completing this exercise, you will gain practical experience that is directly applicable to real-world enterprise
environments.

You might also like