Skip to content

Automated Folder Backups with Backtrooper

MkDocs Python Hero

backtrooper.sh is a fully automated and nohup-safe bash script that backs up folders from a source directory to a backup destination, complete with Star Wars flair, timestamped logs, and real-time notifications via Telegram and ntfy.

Designed for interactive and unattended use, it supports manual folder selection or auto-fallback when idle.


1. Key Features

  • Interactive folder selection with fallback timeout
  • rsync with progress tracking and error tolerance
  • Fully background-safe (nohup compatible)
  • Notifies via Telegram and ntfy
  • Timestamped log output for traceability
  • Auto-deletes backups and logs older than 6 months
  • Themed for fun, but serious about backups

2. Directory Structure

The script uses the following paths by default:

Purpose Path
Backup Base /path/to/backup/
Backup Output /path/to/backup/docker_config_YYYYMMDD_HHMMSS/
Log Storage /path/to/backup/docker_config_log/

Make sure to create these directories before running the script:

Bash
mkdir -p /path/to/backup/docker_config_log

3. Configuration Section

Edit the first few lines of the script to match your environment. These include:

BASE_DIR="/path/to/source"
DEST_PARENT="/path/to/backup"

NTFY_TOPIC="backtrooper"
NTFY_URL="https://ntfy.example.com/$NTFY_TOPIC"

TELEGRAM_BOT_TOKEN="your_telegram_token"
TELEGRAM_CHAT_ID="your_chat_id"

Update these values to use your own notification endpoints.


4. Running the Script

You can launch backtrooper.sh interactively or in detached mode:

Bash
# Foreground (interactive)
bash backtrooper.sh

# Background (terminal-safe)
nohup bash backtrooper.sh > /dev/null 2>&1 &

Logs will still be generated and saved in the log directory.


5. Folder Selection Behavior

Once started, Tipah (your backup droid with royal gowns and rebel plans) lists available folders inside $BASE_DIR. You’ll be prompted to choose:

  • One or more folder numbers (e.g., 1 2 5)
  • Or 0 to select all folders
  • Or wait 20 seconds, Tipah will auto-select all by default

This makes it suitable for both manual and scheduled runs.


6. Notification System

Notification logic:

Condition Telegram ntfy
All succeeded ✅ Text message ✅ Tagged message
Partial/failed ✅ Log file attached ✅ Error-tagged message

This ensures you’re informed of what happened, even if you’re away from the terminal.


7. Automation with Cron

Set it to run every day at 2AM (or any other time):

0 2 * * * nohup bash /path/to/backtrooper.sh > /dev/null 2>&1 &

Make sure the script is executable:

Bash
chmod +x /path/to/backtrooper.sh

8. Full Script

Click to expand the full backtrooper.sh script
#!/bin/bash

# === ⚙️ CONFIGURATION ===
BASE_DIR="/path/to/source"
DEST_PARENT="/path/to/backup"
DATE=$(date +%Y%m%d_%H%M%S)
DEST_MAIN="$DEST_PARENT/docker_config_$DATE"
LOG_DIR="$DEST_PARENT/docker_config_log"
LOGFILE="$LOG_DIR/backtrooper_log_$DATE.txt"

NTFY_TOPIC="backtrooper"
NTFY_URL="https://ntfy.example.com/$NTFY_TOPIC"

TELEGRAM_BOT_TOKEN="your_bot_token"
TELEGRAM_CHAT_ID="your_chat_id"
TELEGRAM_URL="https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendDocument"

mkdir -p "$DEST_MAIN"
mkdir -p "$LOG_DIR"

# === 🎬 STAR WARS INTRO ===
echo ""
echo "☄️  A long time ago in a server far, far away..."
sleep 1
echo "👨‍🚀 Darth Eirros and Droid Tipah initiate..."
sleep 1
echo "🪐 Backtrooper: Mudbox Backup Hope (Rsync + Nohup Edition)"
echo ""

# === 🧠 FOLDER LISTING ===
echo "🧾 Tipah: These are the available folders in $BASE_DIR:"
folders=()
index=1

for dir in "$BASE_DIR"/*/; do
    folder_name=$(basename "$dir")
    echo "   [$index] $folder_name"
    folders+=("$folder_name")
    ((index++))
done

echo "   [0] 👻 SELECT ALL FOLDERS"
echo ""
echo "💬 Enter folder numbers separated by spaces (e.g., 1 3 4) or 0 for all"
echo "⏳ You have 20 seconds to respond..."

read -t 20 -r -p "➡️  Your selection: " selection
echo ""

# === HANDLE SELECTION ===
if [[ -z "$selection" || "$selection" =~ (^| )0($| ) ]]; then
    echo "⏰ Tipah will backup ALL folders in $BASE_DIR"
    selected_folders=("${folders[@]}")
else
    selected_folders=()
    for i in $selection; do
        index=$((i - 1))
        if [[ ${folders[$index]+_} ]]; then
            selected_folders+=("${folders[$index]}")
        else
            echo "⚠️  Invalid choice: $i (skipped)"
        fi
    done
fi

# === 🧱 BACKUP PROCESS ===
success=true

for folder in "${selected_folders[@]}"; do
    SOURCE="$BASE_DIR/$folder"
    DEST="$DEST_MAIN/$folder"

    echo ""
    echo "🛡️ Tipah preparing lightsaber for: $folder"
    for i in {1..20}; do
    printf "\r⚡ $(printf '█%.0s' $(seq 1 $i))"
    sleep 0.03
    done
    echo -e "\n🟢 Lightsaber Ready."

    echo "[$DATE] Tipah: Backing up $SOURCE to $DEST" | tee -a "$LOGFILE"
    mkdir -p "$DEST"

    rsync -a --info=progress2 --ignore-errors "$SOURCE/" "$DEST/" 2>>"$LOGFILE"
    RSYNC_EXIT=$?

    if [ $RSYNC_EXIT -eq 0 ]; then
        echo "[$DATE] ✅ $folder backup complete." | tee -a "$LOGFILE"
    elif [ $RSYNC_EXIT -eq 23 ]; then
        echo "[$DATE] ⚠️ $folder backup completed with some errors (exit code 23)." | tee -a "$LOGFILE"
    else
        echo "[$DATE] ❌ $folder backup FAILED! (exit code: $RSYNC_EXIT)" | tee -a "$LOGFILE"
        success=false
    fi
done

# === 📢 NOTIFICATIONS ===
if $success; then
    MSG="✅ [$DATE] Darth Eirros: All selected folders backed up successfully!\nLocation: $DEST_MAIN"
    curl -H "Title: Backtrooper Success" -H "Tags: success,backup" -d "$MSG" "$NTFY_URL"
    curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage" \
        -d chat_id="$TELEGRAM_CHAT_ID" -d text="$MSG" > /dev/null
else
    MSG="❌ [$DATE] Darth Eirros: One or more folders FAILED to backup! ⚠️ See attached log."
    curl -H "Title: Backtrooper FAIL" -H "Tags: error,backup" -d "$MSG" "$NTFY_URL"
    curl -s -X POST "$TELEGRAM_URL" -F chat_id="$TELEGRAM_CHAT_ID" \
        -F document=@"$LOGFILE" -F caption="$MSG" > /dev/null
fi

# === ♻️ AUTO DELETE OLD BACKUPS & LOGS (older than 6 months) ===
find "$DEST_PARENT" -maxdepth 1 -type d -name 'docker_config_*' -mtime +180 -exec rm -rf {} \;
find "$LOG_DIR" -type f -name 'backtrooper_log_*.txt' -mtime +180 -exec rm -f {} \;

# === ✅ END MESSAGE ===
echo ""
echo "✅ All backup tasks complete. You may close this terminal now, Commander Eiril. 🖖"
echo "🌠 Operation complete. Tipah returning to hyperspace... 🚀"
echo ""

View on GitHub

scripts/backtrooper.sh


9. Final Thoughts

This script started as a personal need to secure folders from system failures and human errors, then evolved into something functional and enjoyable. It’s not just about backups, it’s about how much better automation feels when your tools have character.

Customize it, fork it, or extend it. Tipah won’t mind.

May the backups be with you