90 Snippets: Journey of a Tech Wizard ๐Ÿง™โ€โ™‚๏ธ

90 Snippets: Journey of a Tech Wizard ๐Ÿง™โ€โ™‚๏ธ

90 Projects towards mastery - Day 02 โœจ


5 min read

Hey, tech enthusiasts! Astro here, ready to share the magical world of coding with you! Today, I'm thrilled to unveil my latest creation: the Backup Script! It's a script that performs regular backups, and trust me, it's packed with cool features that'll blow your mind! ๐Ÿ”ฅ๐Ÿš€

The source code is available here

Let's dive in and see what this powerful script is all about:

The Backup Script: Unraveling the Magic! ๐ŸŒŸ

In this article, I will take you through the journey of creating the Backup Script - a tool that organizes backups like a pro! We'll explore its inner workings, learn about the code snippets, and reveal the magic behind it all. Buckle up, because we're about to embark on a tech-filled adventure! ๐Ÿง™โ€โ™‚๏ธโœจ

Script Structure: The Building Blocks ๐Ÿ”ง

The "backup.sh" script is structured to handle various options and perform backups flawlessly. Let's break down some key parts:

The Help Function:

function display_help() {
    echo -e "\nname\t\t${GREEN}backup${RESET}"
    echo -e "utility\t\t${YELLOW}performs regular backups of a specified directory${RESET}\n"
    echo "usage:"
    echo -e "\t\tDisplay help:\t${GREEN}./backup -h${RESET}"
    echo -e "\t\tPerform backup:\t${GREEN}./backup [options] [dir]${RESET}\n"
    echo "params:"
    echo -e "\t\t- ${YELLOW}options${RESET}:\n\t\t\t[-c]\tCompress directory\n\t\t\t[-t]\tAdd timestamps\n\t\t\t[-ct]\tDo both\n"
    echo -e "\t\t- ${YELLOW}dir${RESET}:\tpath to the directory"
    echo -n ":"

The help function guides users on how to use the script effectively. It provides clear instructions on the available options and the correct usage. User-friendly and helpful, just the way I like it! ๐Ÿ’โ€โ™‚๏ธ

Parsing Arguments:

# Parsing

if [ $ARG_LEN == 1 ]; then
    if [ -d $1 ]; then

    elif [ $ARG_LEN = 2 ]; then
    if [ -d $1 ] && { [ $2 == "-r" ] || [ $2 == "-c" ] || [ $2 == "-t" ] || [ $2 == "-ct" ] || [ $2 == "-tc" ]; }; then
        elif [ -d $2 ] && { [ $1 == "-r" ] || [ $1 == "-c" ] || [ $1 == "-t" ] || [ $1 == "-ct" ] || [ $1 == "-tc" ]; }; then
        echo -e "${RED}Error:\n\tInvalid arguments\n${RESET}"
    echo -e "${RED}Error:\n\tInvalid number of arguments\n${RESET}"

We've got some nifty code to handle different argument scenarios. Whether you want to display help, perform backups with specific options, or deal with invalid inputs, this script has got you covered! No more confusion - just smooth sailing. ๐ŸŒŠ

The Magic of Backing Up: Compressing and Timestamps! ๐ŸŽฉ๐Ÿช„

Now, let's dive into the heart of the script - the backup process! Our script offers two exciting options:

Compressing Backups:

function backup() {
    # ... Compressing backup code goes here ...

When you select the "-c" option, the script works its magic to compress the backup into a neat and tidy archive. Say goodbye to cluttered directories and hello to efficient backups! It's like watching a magician put everything in its place with a flick of their wand! ๐Ÿช„๐Ÿ’พ

Adding Timestamps:

function backup()
    DATE=$(date +"%Y%m%d_%H%M%S")
    if [ $OPTIONS == "-c" ]; then
        tar "$1" "$ARCHIVE" "$DIR"
        elif [ $OPTIONS == "-t" ]; then
        cp -r "$DIR" "$ARCHIVE_DIR/${DIR_SPLIT[$LAST_INDEX]}_${DATE}"
        elif [ $OPTIONS == "-ct" ] || [ $OPTIONS == "-tc" ]; then
        tar "$1" "$ARCHIVE_DIR/${DIR_SPLIT[$LAST_INDEX]}_${DATE}.tar" "$DIR"
        rm -rf "$ARCHIVE_DIR/${DIR_SPLIT[$LAST_INDEX]}"
        cp -r "$DIR" "$ARCHIVE_DIR/${DIR_SPLIT[$LAST_INDEX]}"

With the "-t" option, the script adds timestamps to the backups, making them unique and easily identifiable. Timestamps are like secret codes - they unlock the history of each backup and ensure you always have the latest version at your fingertips! โฐ๐Ÿ“

Managing Backups with "backup_utils.sh" ๐Ÿ—ƒ๏ธ

Ah, but there's more! I've also created "backup_utils.sh" - a super useful utility to manage your backups like a tech wizard! Let's explore:

Extracting Backups:

if [ -e "./.pid" ]; then
    PID=`cat ./.pid`
    kill_backup $PID
        for pid in $LIST_PROC; do
            echo -e "${GREEN}Process ${YELLOW}$pid${RESET} killed successfully${RESET}"
            kill $pid

With the "-x" option, "backup_utils.sh" works its magic to extract your backups effortlessly. No more hassle - it's like watching a genie grant your wish! ๐Ÿงžโ€โ™‚๏ธ๐Ÿ”“

Stopping Backups:

function kill_proc()
    # List of all backup.sh processes
    LIST_PROC=`pgrep -f backup.sh`

    if [ ${#LIST_PROC[0]} == 0 ]; then
        echo "No backup process running"
        exit 0

    function kill_backup()
        local PROCESS=$1
        # Initialize variables to store the nearest number and the minimum difference
        local PROC=${LIST_PROC[0]}
        local min_diff=$(( ${PROCESS} - ${LIST_PROC[0]} ))
        if (( min_diff < 0 )); then
            min_diff=$(( -min_diff ))

        # Loop through the list of numbers to find the nearest one
        for pid in "${LIST_PROC[@]}"; do
            diff=$(( ${PROCESS} - ${pid} ))
            if (( diff < 0 )); then
                diff=$(( -diff ))
            if (( diff < min_diff )); then
        kill $PROC
        echo -e "${GREEN}Process ${YELLOW}$PROC${RESET} ${GREEN}has been terminated successfully!${RESET}"
# ...

And when it's time to take a break from backups, the "-s" option lets you stop the backup process with ease. You're in control, my friend - just like a true tech wizard! ๐Ÿšซ๐Ÿ”„

Next challenge ๐Ÿ‘พ

Next on my list is Day 03 Shell Scripting!

Assignment: Design a script that generates random passwords of a specified length and complexity (e.g., including uppercase, lowercase, numbers, and special characters).

Stay tuned! ๐Ÿ”ฅ

Conclusion: Unleash the Tech Wizard Within! ๐ŸŒŸ๐Ÿš€

There you have it, fellow tech enthusiasts - the Backup Script and its magical sidekick, "backup_utils.sh"! I hope you're as excited as I am to dive into the world of coding and conquer daily challenges. This series will be a rollercoaster of tech adventures, learning, and growth - all while having fun! ๐ŸŽข๐Ÿ’ป

So, let's strap in and journey together as we level up our skills, unlock the secrets of coding, and unleash the tech wizard within us! By Thor, By Odin, let's make this journey epic! ๐Ÿง™โ€โ™‚๏ธโœŒ๏ธ