Skip to content

Instantly share code, notes, and snippets.

@brokosz
Created July 2, 2025 02:31
Show Gist options
  • Save brokosz/d8aa90d659d7585898875586850bca9a to your computer and use it in GitHub Desktop.
Save brokosz/d8aa90d659d7585898875586850bca9a to your computer and use it in GitHub Desktop.
How It Works: 1. First Backup: Creates a simple .bak file (e.g., file.txt → file.txt.bak). 2. Subsequent Backups: Adds a timestamp before .bak to avoid overwriting existing backups (e.g., file.txt → file.txt_20241130_123456.bak). Examples: • First run: bu file.txt Creates: file.txt.bak • Subsequent runs: bu file.txt Creates: file.txt_20241130_12…
bak() {
# If a basic .bak file doesn't exist, create it
if [ ! -e "$1.bak" ]; then
cp "$1" "$1.bak"
else
# Otherwise, create a timestamped backup with .bak at the end
cp "$1" "$1_$(date +%Y%m%d_%H%M%S).bak"
fi
}
# fancier
# simple file backup in place for not crushing yourself when you save over the current file
bak() {
if [ $# -eq 0 ]; then
echo "Usage: bu <file_or_directory>"
return 1
fi
if [ ! -e "$1" ]; then
echo "❌ Error: '$1' does not exist"
return 1
fi
if [ ! -e "$1.bak" ]; then
cp -ai "$1" "$1.bak"
echo "💾 Backup created: $1.bak"
else
timestamp="$(date +%Y%m%d_%H%M%S)"
backup_name="$1_${timestamp}.bak"
cp -ai "$1" "$backup_name"
echo "💾 Backup created: $backup_name"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment