Day 5 of My #90DaysOfDevOps Challenge: Advanced Linux Shell Scripting and User Management in DevOps
Introduction
Today, for my #90DaysOfDevOps challenge, I focused on advanced Linux shell scripting and user management. This session was full of hands-on tasks that highlighted how important automation and managing users are in a DevOps environment.
Task 1: Creating Directories Using a Shell Script
One of the essential parts of managing projects is organizing files and directories. I wrote a Bash script called createDirectories.sh
that takes three arguments: the name of the directory, the starting number, and the ending number.
Script Explanation:
The script uses a loop to create the specified number of directories. Here’s how it works:
#!/bin/bash
# Check for the right number of arguments
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <directory-name> <start-number> <end-number>"
exit 1
fi
directory_name=$1
start=$2
end=$3
# Create directories
for ((i=start; i<=end; i++)); do
mkdir "${directory_name}${i}"
done
echo "Created directories from ${directory_name}${start} to ${directory_name}${end}."
Example Usage:
- Running
./
createDirectories.sh
day 1 90
Output:
Created directories from day1 to day90.
Command Line Result:
$ ls
day1 day2 day3 ... day90
- Running
./
createDirectories.sh
Movie 20 50
Output:
Created directories from Movie20 to Movie50.
Command Line Result:
$ ls
Movie20 Movie21 Movie22 ... Movie50
Task 2: Creating a Backup Script
Next, I learned how important backups are in a DevOps role. I created a backup script to keep my work safe and secure. This script automates the process of backing up important files or directories to ensure that nothing is lost.
Backup Script Example:
#!/bin/bash
# Define source and destination directories
SOURCE_DIR="/path/to/source"
BACKUP_DIR="/path/to/backup"
# Create a backup
tar -czf "${BACKUP_DIR}/backup_$(date +%Y%m%d).tar.gz" "${SOURCE_DIR}"
echo "Backup created at ${BACKUP_DIR}/backup_$(date +%Y%m%d).tar.gz."
Output:
Backup created at /path/to/backup/backup_20231009.tar.gz.
Task 3: Automating Backups with Cron and Crontab
To automate the backup process, I looked into Cron and Crontab. Cron is a tool that schedules tasks to run at specific times. I watched a video that explained how to set up Cron jobs.
Cron Job Example:
To run the backup script daily at 2 AM, I edited the crontab file:
crontab -e
And added this line:
0 2 * * * /path/to/your/backup_script.sh
Output:
There’s no direct output, but the backup script will run automatically every day at 2 AM.
Task 4: User Management in Linux
User management is essential for controlling access and permissions in a Linux environment. I created two new users and displayed their usernames, learning about user IDs and their importance.
User Creation Example:
sudo adduser user1
sudo adduser user2
# Displaying usernames
echo "Usernames created: user1, user2"
Output:
Adding user `user1' ...
Adding user `user2' ...
Usernames created: user1, user2
Conclusion
Today’s tasks pushed me to improve my scripting skills and understand user management in Linux better. I felt accomplished as I worked through each task, and I see how these skills are vital for a successful DevOps career.
That’s it for Day 5! Don’t forget to share your learnings with the DevOps community. Shell scripting is a skill that will serve you well throughout your journey, making your work faster, more efficient, and automated.
See you tomorrow for more DevOps fun! Let’s learn together and grow together in the world of DevOps! 🌱🚀 #DevOpsJourney #LearningTogether #GrowthMindset #TechCommunity #TrainWithShubham #90DaysOfDevOps
Follow my #90DaysOfDevOps challenge on LinkedIn, and stay tuned for more updates!
Subscribe to my newsletter
Read articles from Siddharth Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Siddharth Patil
Siddharth Patil
👋 Hi, I'm SIDDHARTH PATIL, a DevOps learner diving into Linux and cloud computing. 🚀 Currently mastering the basics and excited for the journey ahead! 💻☁️ #DevOps #Linux #CloudComputing"