Capstone: एक Real-World Backup/Cleanup Script
In this page:
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
#!/bin/bash
set -euo pipefail
log() {
echo "[$(date +'%H:%M:%S')] $1"
}
log "backup script starting"
Login to try C/C++/Java/PHP code in the editor
Backup से पहले Preconditions Validate करना
कुछ भी करने से पहले, script confirm करती है कि source directory मौजूद है और mkdir -p से backup destination idempotently बनाती है, ताकि यह fast और clearly fail हो अगर कुछ missing हो बीच में fail होने के बजाय।
उदाहरण: Validating Preconditions Before Backing Up
#!/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"
Login to try C/C++/Java/PHP code in the editor
पूरी 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
#!/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 "$@"
Login to try C/C++/Java/PHP code in the editor
- यह check किए बिना एक backup script लिखना कि source directory actually मौजूद है, जो एक confusing empty backup produce कर सकता है।
- पहले यह confirm किए बिना पुराने backups delete करना कि नया backup succeed हुआ, data की सभी copies एक साथ खोने का risk लेते हुए।
- एक 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 करें।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- Quoting Pitfalls and Why to Always Quote Variables
- set -euo pipefail as a Script Header
- Structuring a Script (Functions, main() Pattern, Exit at the End)
- Idempotent Scripts (Safe to Re-Run)
- Logging in Scripts (Timestamped log Function)
- Writing Portable Scripts (Bash-isms vs POSIX sh)
- Capstone: A Real-World Backup/Cleanup Script