Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management
Tasks
You have to do the same using Shell Script i.e using either Loops or command with start day and end day variables using arguments
Bash Script:
createDirectories.sh- Dynamic Directory Creation
This Bash script is designed to dynamically create a specified number of directories with names based on a given pattern. When executed with three arguments - directory name, start number, and end number, it generates directories with names ranging from the start number to the end number appended to the specified directory name.
Usage:
bashCopy code./createDirectories.sh <directory_name> <start_number> <end_number>
Example:
bashCopy code./createDirectories.sh day 1 5
This example command will create directories named day1, day2, day3, day4, and day5.
Script Overview:
Argument Validation:
- The script checks if it receives exactly three arguments. If not, it displays the correct usage and exits.
Argument Extraction:
- The script extracts the directory name, start number, and end number from the provided arguments.
Directory Creation Loop:
It then enters a loop from the start number to the end number (inclusive).
In each iteration, it creates a directory with a name formed by appending the current iteration number to the specified directory name.
Output:
- The script prints a message for each created directory.
Completion Message:
- Finally, it prints a completion message indicating that the directory creation process is complete.
Notes:
Ensure the script has execution permissions (
chmod +xcreateDirectories.sh).Modify and adapt the script as needed, and feel free to incorporate additional features or error handling based on specific requirements.
This script is a flexible tool for automating the creation of directories in a sequential pattern, making it suitable for various use cases, including organizing daily logs or any other structured data. ππ» #BashScripting #Automation #DevOps
Create a Script to backup all your work done till now.
Bash Script:
backupWork_easy.sh- Simplified Backup AutomationIn the realm of DevOps, where every keystroke matters, backups are the unsung heroes ensuring your work is safe and sound. Let's demystify how a simple Bash script can become your backup ally.
Script Overview:
Directory Configuration:
backup_source: Specify the directory you want to back up.backup_destination: Define where you want to store the backups.backup_filename: Compose a filename with a timestamp for each backup.
Source Directory Validation:
- The script checks if the specified source directory exists. If not, it exits with an error message.
Backup Creation:
- Using
tar, the script compresses and archives the source directory into a timestamped file in the backup destination.
- Using
Completion Message:
- The script provides feedback, indicating whether the backup process was successful or encountered an error.
Usage:
Modify
backup_sourceandbackup_destinationto match your directory structure.Run the script using
./backupWork_easy.sh.
Example:
bashCopy code./backupWork_easy.sh
Note:
The script offers a straightforward way to create timestamped backups with minimal configuration.
Ensure execution permissions with
chmod +x backupWork_easy.sh.
This script, inspired by the wisdom shared in the [Reference Video], makes the often overlooked task of backups a breeze. Remember, a reliable backup strategy is a cornerstone of every DevOps engineer's routine! ππΎ #DevOps #BackupScript #AutomationMagic
Read About Cron and Crontab, to automate the backup Script
Cron and Crontab in DevOps: A Quick Overview ππ
Cron: The Scheduler
Cron is a Unix-based job scheduler.
It automates repetitive tasks at set times or intervals.
Crontab: The Scheduler's Blueprint
Crontab is the command-line utility for managing cron jobs.
Users create, edit, and delete scheduled tasks with crontab.
Crontab Structure:
- Six fields define the schedule: minute, hour, day of the month, month, day of the week, and the command to execute.
Examples:
Run a backup script daily at midnight:
bashCopy code0 0 * * * /path/to/backupScript.shExecute a cleanup script every Sunday at 3:30 AM:
bashCopy code30 3 * * 7 /path/to/cleanupScript.sh
Automate Backups with Crontab:
Edit crontab:
crontab -eAdd entry for daily backups at 2 AM:
bashCopy code0 2 * * * /path/to/backupScript.sh
Conclusion: Cron and Crontab are your allies in automating DevOps tasks. Master them to orchestrate operations seamlessly. Time is on your side! β°π¨βπ» #DevOps #Automation #Cron #Crontab
User Management in Linux:
1. Creating Users:
Command:
useraddExample:
sudo useradd username
2. Setting Passwords:
Command:
passwdExample:
sudo passwd username
3. Modifying User Properties:
Command:
usermodExample:
sudo usermod -aG groupname username(Add user to a group)
4. Deleting Users:
Command:
userdelExample:
sudo userdel username
5. Understanding User Information:
User information is stored in
/etc/passwd.Password hashes are stored in
/etc/shadow.Group information is in
/etc/group.
6. Switching Users:
Command:
suExample:
su - username
7. Granting Sudo Privileges:
- Add user to the
sudogroup in/etc/group.
Create 2 users and just display their Usernames
User Management in Linux: A Theoretical Overview
In the realm of system administration, user management is a foundational task. Let's explore the theoretical aspects of creating and displaying user information in Linux.
Creating Users:
In Linux, users can be created using the
useraddcommand. For instance, to create two users,user1anduser2, the following commands are used:bashCopy codesudo useradd -m user1 sudo useradd -m user2The
-mflag ensures that home directories are created for the users.Displaying User Information:
To display user information, including usernames, we can use a simple
echocommand. For example:bashCopy codeecho "Usernames created:" echo "1. user1" echo "2. user2"Putting it into Practice:
Save as Script:
- Save the above commands in a file, say
createUsersAndDisplay.sh.
- Save the above commands in a file, say
Make Executable:
- Ensure the script is executable with
chmod +xcreateUsersAndDisplay.sh.
- Ensure the script is executable with
Run Script:
- Execute the script with
./createUsersAndDisplay.sh.
- Execute the script with
Conclusion:
User management is a fundamental aspect of system administration. These theoretical steps illustrate the process of creating users and providing insights into how to display user information.
Feel free to adapt these steps based on your system's requirements and security practices. Understanding user management is crucial for maintaining a secure and well-organized system. ππ©βπ» #Linux #UserManagement #SystemAdministration