Project 1- Shell Script For User Management And Back-up in Linux

Project 1- Shell Script For User Management And Back-up in Linux

ยท

3 min read

๐ŸŒ€Task

-The script should be able to add, delete, and modify user accounts on a Linux system.

-Include options to create and manage groups for users.

-Implement a backup feature that compresses and archives a specified directory.

-The script should provide a user-friendly command-line interface with clear options and usage Instructions.

  1. Script Overview: Let's start by understanding the purpose of our user_management.sh script. This script provides a command-line interface for performing various user management tasks:
  • Creating a new user account

  • Deleting an existing user account

  • Modify an existing user accounts

  • Creating a new Group

  • Deleting an existing group

  • Backup a specified directory

#!/bin/bash

# Function to display usage instructions
usage() {
    echo "Usage: $0 [OPTIONS]"
    echo "Options:"
    echo "  -a, --add-user USERNAME        Add a new user"
    echo "  -d, --delete-user USERNAME     Delete an existing user"
    echo "  -m, --modify-user USERNAME     Modify an existing user"
    echo "  -g, --add-group GROUPNAME      Add a new group"
    echo "  -r, --delete-group GROUPNAME   Delete an existing group"
    echo "  -b, --backup DIRECTORY         Backup a specified directory"
}
  1. Provide an overview of the script's functionality:

    • Add User: Allow users to create new user accounts with custom parameters such as username, full name, home directory, and shell.

    • Delete User: Provide the ability to delete existing user accounts, including their home directories.

    • Modify User: Enable users to modify existing user accounts, including options to change full name, home directory, and shell.

    # Function to add a new user
    add_user() {
        username="$1"
        sudo useradd "$username"
        echo "User $username added successfully"
    }

    # Function to delete an existing user
    delete_user() {
        username="$1"
        sudo userdel -r "$username"
        echo "User $username deleted successfully"
    }

    # Function to modify an existing user
    modify_user() {
        username="$1"
        # Add modification logic here
        echo "User $username modified successfully"
    }
  1. Provide an overview of the script's functionality
  • Adding new groups with customizable attributes.

  • Deleting existing groups to maintain system cleanliness.

      # Function to add a new group
      add_group() {
          groupname="$1"
          sudo groupadd "$groupname"
          echo "Group $groupname added successfully"
      }
    
      # Function to delete an existing group
      delete_group() {
          groupname="$1"
          sudo groupdel "$groupname"
          echo "Group $groupname deleted successfully"
      }
    
    1. Function to backup a specited dirrectory
  • Creating a backup of critical system files and directories to facilitate system recovery in the event of data loss.

  • Backing up user-generated data to prevent data loss and ensure business continuity.

  • Automating regular backups to minimize the risk of data loss due to human error or system failures.

      # Function to backup a specified directory
      backup_directory() {
          directory="$1"
          backup_file="backup_$(date +'%Y%m%d_%H%M%S').tar.gz"
          tar -czf "$backup_file" "$directory"
          echo "Backup created: $backup_file"
      }
    
      # Main script logic
      while [[ $# -gt 0 ]]; do
          case "$1" in
              -a|--add-user)
                  add_user "$2"
                  shift 2 ;;
              -d|--delete-user)
                  delete_user "$2"
                  shift 2 ;;
              -m|--modify-user)
                  modify_user "$2"
                  shift 2 ;;
              -g|--add-group)
                  add_group "$2"
                  shift 2 ;;
              -r|--delete-group)
                  delete_group "$2"
                  shift 2 ;;
              -b|--backup)
                  backup_directory "$2"
                  shift 2 ;;
              -h|--help)
                  usage
                  exit ;;
              *)
                  echo "Error: Invalid option: $1"
                  usage
                  exit 1 ;;
          esac
      done
    

    Conclusion:

    • Summarize the benefits of using the script to automate user account management, group management, and backup tasks on Linux systems.

    • Encourage readers to explore the script's capabilities and integrate it into their workflows for enhanced productivity and efficiency.

ย