← Back to Bash Course | Chapter 14: Best Practices & Real-World Scripting | Lesson 7 of 7

Capstone: एक Real-World Backup/Cleanup Script

यह final example course के कई ideas को एक छोटी पर realistic script में combine करता है जो कुछ files को backup करती है और पुरानी files को safely clean up करती है।

Script Header और Logging Set Up करना

Capstone script strict mode और एक छोटे timestamped logger से शुरू होती है, दोनों पहले इस course में covered हुए, उस safety net को establish करते हुए जिस पर बाकी script rely करती है।

उदाहरण: Setting Up the Script Header and Logging

bash
#!/bin/bash
set -euo pipefail

log() {
    echo "[$(date +'%H:%M:%S')] $1"
}

log "backup script starting"

Backup से पहले Preconditions Validate करना

कुछ भी करने से पहले, script confirm करती है कि source directory मौजूद है और mkdir -p से backup destination idempotently बनाती है, ताकि यह fast और clearly fail हो अगर कुछ missing हो बीच में fail होने के बजाय।

उदाहरण: Validating Preconditions Before Backing Up

bash
#!/bin/bash
set -euo pipefail

source_dir="source_data"
backup_dir="backups"

mkdir -p "$source_dir"
echo "sample" > "$source_dir/file1.txt"

if [[ ! -d $source_dir ]]; then
    echo "source directory missing, aborting" >&2
    exit 1
fi
mkdir -p "$backup_dir"
echo "preconditions satisfied: source and backup directories are ready"

rm -rf "$source_dir" "$backup_dir"

पूरी Backup और Cleanup Script

यह कई concepts को साथ जोड़ती है: एक main() entry point, एक logging function, precondition checks, एक timestamped backup archive, और एक retention count से पुराने backups की cleanup, सब strict mode में एक clear final exit status के साथ wrapped।

उदाहरण: The Full Backup and Cleanup Script

bash
#!/bin/bash
set -euo pipefail

source_dir="source_data"
backup_dir="backups"
retain_count=3

log() {
    echo "[$(date +'%H:%M:%S')] $1"
}

validate() {
    if [[ ! -d $source_dir ]]; then
        log "ERROR: source directory '$source_dir' does not exist"
        exit 1
    fi
    mkdir -p "$backup_dir"
}

run_backup() {
    local stamp
    stamp=$(date +'%Y%m%d_%H%M%S')
    local archive="$backup_dir/backup_$stamp.tar"
    tar -cf "$archive" -C "$source_dir" .
    log "created backup: $archive"
}

cleanup_old_backups() {
    local count
    count=$(find "$backup_dir" -name 'backup_*.tar' | wc -l)
    if (( count > retain_count )); then
        find "$backup_dir" -name 'backup_*.tar' | sort | head -n "$((count - retain_count))" | xargs rm -f
        log "pruned old backups, kept the most recent $retain_count"
    else
        log "no pruning needed, $count backup(s) present"
    fi
}

main() {
    mkdir -p "$source_dir"
    echo "data" > "$source_dir/file.txt"

    validate
    run_backup
    sleep 1
    run_backup
    cleanup_old_backups

    log "backup script finished successfully"
    rm -rf "$source_dir" "$backup_dir"
}

main "$@"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह check किए बिना एक backup script लिखना कि source directory actually मौजूद है, जो एक confusing empty backup produce कर सकता है।
  2. पहले यह confirm किए बिना पुराने backups delete करना कि नया backup succeed हुआ, data की सभी copies एक साथ खोने का risk लेते हुए।
  3. एक script में logging skip करना जो unattended run होनी है (उदाहरण के लिए cron के through), failures को बाद में diagnose करना impossible बनाते हुए।
चैप्टर सारांश
  • एक well-built real-world script strict mode, main() entry point वाले functions, logging, validation, और idempotent operations combine करती है।
  • कुछ भी destructive करने से पहले inputs और preconditions validate करना avoidable failures की एक पूरी class रोकता है।
  • Backup/cleanup logic को छोटे functions (backup, फिर cleanup) के रूप में structure करना हर responsibility को independently reason करना easy रखता है।
  • एक छोटी script भी एक बड़ी script जैसी discipline से benefit होती है: inputs check करें, progress log करें, errors handle करें, और एक meaningful status के साथ exit करें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.