Bash Scripting Journey — Two Real-World Automation Scripts Completed!

Vivek UmraoVivek Umrao
3 min read

As part of my Bash scripting learning journey, Week 3 pushed me into real-world Linux automation scenarios.
This week, I built two scripts that a system administrator or DevOps engineer could actually use on the job:

  1. User Account Management Script

  2. Automated Backup & Recovery with Rotation (using Cron)

It wasn’t all smooth sailing — I got stuck a few times, but with the help of Google, man pages, and trial & error, I managed to make everything work.


1️⃣ User Account Management Script

Goal: Create a single Bash script that can manage Linux users from the command line with different options.

Features I implemented:

  • Create a new user account (-c or --create)

  • Delete an existing user (-d or --delete)

  • Reset a user’s password (-r or --reset)

  • List all users with their UIDs (-l or --list)

  • Show help/usage information (-h or --help)

Key Commands Used:

  • id → Check if a user exists

  • useradd, userdel, chpasswd → Account creation, deletion, password reset

  • awk -F: → Parse /etc/passwd to get usernames & UIDs

  • Here-documents (cat << EOF) → Display help menus

Challenges I faced:

  • At first, I forgot to check if the username already exists before creating it — this caused errors.

  • I wasn’t validating passwords properly (needed confirmation input to avoid typos).

  • Fixed both issues after searching “bash check if user exists” and “bash compare passwords” on Google.


2️⃣ Automated Backup & Recovery with Rotation (Cron)

Goal: Create a script that backs up a given directory with timestamps, and keeps only the last 3 backups to save space.

Features I implemented:

  • Takes a directory as input.

  • Creates a timestamped backup folder.

  • Copies all contents into the backup folder.

  • Rotation system: Only keeps the newest 3 backups, deletes older ones automatically.

  • Works with Cron jobs for scheduled automation.

Key Commands Used:

  • date +"%Y-%m-%d_%H-%M-%S" → Generate unique timestamps

  • mkdir -p and cp -r → Create folders and copy files

  • ls -dt + Bash arrays → Sort backups and find the oldest

  • rm -rf → Remove old backups

  • cron → Schedule backups automatically

Challenges I faced:

  • Initially, my rotation logic deleted the newest backups instead of the oldest — turns out I needed to reverse the sorting order (ls -dt).

  • I also learned that testing with fake directories before running on real data is a lifesaver.


Sample Output

Here’s what my scripts look like when they run:

bashCopyEdit✅ Backup created: /home/user/docs/backup_2025-08-13_09-15-30
🗑️ Removed old backup: /home/user/docs/backup_2025-08-10_14-20-55
✅ User 'vivek' created successfully
🔑 Password for 'vivek' reset successfully

Key Learnings from This Week

  • Error Handling Matters — Always check if a user or directory exists before acting.

  • Automation Saves Time — Once set up with Cron, backups happen while I’m sleeping.

  • The Command Line Is Powerful — A few commands combined in a script can do the work of multiple manual steps.

  • Google Is Your Friend — It’s normal to get stuck, but solutions are often just a search away.


0
Subscribe to my newsletter

Read articles from Vivek Umrao directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vivek Umrao
Vivek Umrao