60bdd5f98f2cc100695558c4-Bash Scripting Exercise Enterprise-Level Automation-220724-211738
60bdd5f98f2cc100695558c4-Bash Scripting Exercise Enterprise-Level Automation-220724-211738
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 .
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
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
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.