Bash Script to Backup and Compress Directories with Timestamp
- Category Shell
- Type Command
- Platform Linux
- Language Bash
- Price Free
- Copy 1 128
- Comments 0
Bash Script to Backup and Compress Directories with Timestamp
#!/usr/bin/env bash
# Create a timestamped tar.gz backup of a specific directory
# Usage: ./backup.sh [directory_to_backup] [backup_destination]
SOURCE_DIR="$1"
BACKUP_DIR="${2:-./backups}"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
if [ -z "$SOURCE_DIR" ]; then
echo "Error: Specify a source directory."
exit 1
fi
mkdir -p "$BACKUP_DIR"
BACKUP_FILE="$BACKUP_DIR/backup_$(basename "$SOURCE_DIR")_$TIMESTAMP.tar.gz"
tar -czf "$BACKUP_FILE" "$SOURCE_DIR"
echo "Backup created successfully: $BACKUP_FILE"
Free snippet — copy & paste!
Copy this snippet for free on Clayi Code. One click — no account required.
There are no comments yet :(