Project 1: Simplified Linux Administration: User Management and Backup with Shell Scripts 🚀
Introduction
Are you ready to streamline user management and backup tasks on your Linux system? In this guide, we'll walk you through creating shell scripts to automate these processes. By the end, you'll have powerful tools at your fingertips and a deeper understanding of shell scripting.
Prerequisites:
Before we begin, ensure you have:
🐧Access to a Linux distribution (e.g., Ubuntu, Fedora)
💻Basic knowledge of the command line interface
📝Familiarity with text editors like Nano or Vim
Step 1: Understanding the Project
Our goal is to develop two shell scripts:
User Management Script: Adds, deletes, and modifies user accounts.
Backup Script: Performs secure backups of specified directories.
Step 2: Creating the User Management Script
Open Your Text Editor: Create a new file named
user_management.sh
.Define Functions: Add functions for adding, deleting, and modifying users.
#!/bin/bash # Description: This script automates user management tasks in Linux. # Function to add a new user add_user() { username=$1 useradd $username if [ $? -eq 0 ]; then echo "User $username added successfully." else echo "Error: Failed to add user $username." fi } # Function to modify an existing user modify_user() { old_username=$1 new_username=$2 usermod -l $new_username $old_username if [ $? -eq 0 ]; then echo "User $old_username modified to $new_username successfully." else echo "Error: Failed to modify user $old_username." fi } # Function to delete an existing user delete_user() { username=$1 userdel -r $username if [ $? -eq 0 ]; then echo "User $username deleted successfully." else echo "Error: Failed to delete user $username." fi }
Taking User Input: Parse command-line arguments to determine the action to perform.
# Main script if [ $# -lt 2 ]; then echo "Usage: $0 [add|modify|delete] username [new_username]" exit 1 fi case $1 in "add") add_user $2 ;; "modify") modify_user $2 $3 ;; "delete") delete_user $2 ;; *) echo "Invalid option" ;; esac exit 0
Set Execution Permission: Run the following command to add execution permission to the script:
chmod +x User_Management.sh
Testing the Script: Execute the script with different options to verify functionality.
sudo ./User_Management.sh add supriya
sudo ./User_Management.sh modify supriya supriyasurkar
sudo ./User_Management.sh delete supriyasurkar
Step 3: Developing the Backup Script
Create a New File: Open a new file named
backup.sh
in your text editor.Define Backup Function: Write a function to compress and archive specified directories.
#!/bin/bash # Description: This script performs backup tasks in Linux. # Hardcoded source directory source_directory="/home/ubuntu/Linux-Project" # Destination backup directory backup_directory="/home/ubuntu/Backups" # Function to perform backup of a directory backup_directory() { # Format the date in a human-readable format (YYYY-MM-DD_HH-MM-SS) backup_date=$(date +%Y-%m-%d_%H-%M-%S) backup_file="$backup_directory/backup_$backup_date.tar.gz" tar -czf "$backup_file" "$source_directory" echo "Backup of $source_directory created: $backup_file" }
Parsing User Input: Take command-line arguments to specify the directory for backup.
# Main script if [ $# -eq 0 ]; then echo "Usage: $0 directory_to_backup" exit 1 fi backup_directory "$1" exit 0
Set Execution Permission: Run the following command to add execution permission to the script:
chmod +x backup.sh
Testing the Script: Execute the script to ensure it creates backups as expected.
./backup.sh /path/to/directory_to_backup
These steps ensure that the shell scripts are executable and can be run to perform user management and backup tasks efficiently.
Step 4: Sharing Your Project on GitHub
Create a GitHub Repository: Sign in to GitHub and create a new repository.
Initialize Git: In your project directory, initialize a Git repository.
Add Remote Repository: Link your local repository to the remote GitHub repository.
Add and Commit Changes: Add your shell scripts to the staging area and commit changes.
Push Changes to GitHub: Push the committed changes to your GitHub repository.
Conclusion:
🎉Congratulations! You've created powerful shell scripts to automate user management and backup tasks in Linux.