Skip to main content

Command Palette

Search for a command to run...

Day 5 Task: Advanced Linux Shell Scripting for DevOps Engineers with User management

Published
β€’5 min readβ€’View as Markdown

Tasks

  1. 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:

  1. Argument Validation:

    • The script checks if it receives exactly three arguments. If not, it displays the correct usage and exits.
  2. Argument Extraction:

    • The script extracts the directory name, start number, and end number from the provided arguments.
  3. 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.

  4. Output:

    • The script prints a message for each created directory.
  5. Completion Message:

    • Finally, it prints a completion message indicating that the directory creation process is complete.

Notes:

  • Ensure the script has execution permissions (chmod +x createDirectories.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

  1. Create a Script to backup all your work done till now.

    Bash Script: backupWork_easy.sh - Simplified Backup Automation

    In 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:

    1. 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.

    2. Source Directory Validation:

      • The script checks if the specified source directory exists. If not, it exits with an error message.
    3. Backup Creation:

      • Using tar, the script compresses and archives the source directory into a timestamped file in the backup destination.
    4. Completion Message:

      • The script provides feedback, indicating whether the backup process was successful or encountered an error.

Usage:

  1. Modify backup_source and backup_destination to match your directory structure.

  2. 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

  1. 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:

  1. Run a backup script daily at midnight:

     bashCopy code0 0 * * * /path/to/backupScript.sh
    
  2. Execute a cleanup script every Sunday at 3:30 AM:

     bashCopy code30 3 * * 7 /path/to/cleanupScript.sh
    

Automate Backups with Crontab:

  1. Edit crontab: crontab -e

  2. Add 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

  1. User Management in Linux:

    1. Creating Users:

    • Command: useradd

    • Example: sudo useradd username

2. Setting Passwords:

  • Command: passwd

  • Example: sudo passwd username

3. Modifying User Properties:

  • Command: usermod

  • Example: sudo usermod -aG groupname username (Add user to a group)

4. Deleting Users:

  • Command: userdel

  • Example: 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: su

  • Example: su - username

7. Granting Sudo Privileges:

  • Add user to the sudo group in /etc/group.
  1. 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 useradd command. For instance, to create two users, user1 and user2, the following commands are used:

     bashCopy codesudo useradd -m user1
     sudo useradd -m user2
    

    The -m flag ensures that home directories are created for the users.

    Displaying User Information:

    To display user information, including usernames, we can use a simple echo command. For example:

     bashCopy codeecho "Usernames created:"
     echo "1. user1"
     echo "2. user2"
    

    Putting it into Practice:

    1. Save as Script:

    2. Make Executable:

    3. Run Script:

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

More from this blog

Introduction Of DevOps

108 posts