#!/usr/bin/env bash # shellcheck disable=SC2155 # ------------------------------------------------------------ # Configuration: Add files and directories to backup below # ------------------------------------------------------------ PATHS_TO_BACKUP=( # provide absolute paths to files and directories # ~ does not work here, use $HOME instead "$HOME/.config" # "/etc" # "/path/to/important/file.txt" # "/path/to/important/directory" # Add as many paths as you need ) # use rsync if available, otherwise fall back to cp cp() { if command -v rsync &> /dev/null; then command rsync -a "$@" else command cp -r "$@" fi } backup_item() { local path="$1" local backup_dir="$2" local timestamp="$(date +"%Y%m%d_%H%M%S")" # Check if the path exists if [ ! -e "$path" ]; then echo "🛑 Error: $path does not exist" return 1 fi # Get basename of the path local basename="$(basename "$path")" # Create backup name with timestamp if [ -n "$backup_dir" ]; then # If backup directory specified, put backup there local backup_name="${backup_dir}/${basename}.${timestamp}.bak" # Ensure backup directory exists mkdir -p "$backup_dir" else # Otherwise put backup in same directory as original with .bak extension local backup_name="${path}.${timestamp}.bak" fi # Make the backup if [ -d "$path" ]; then # It's a directory cp "$path" "$backup_name" echo "✅ Directory backup created: $backup_name" else # It's a file cp "$path" "$backup_name" echo "✅ File backup created: $backup_name" fi } # Optional backup directory from command line argument BACKUP_DIR="" if [ $# -eq 1 ]; then BACKUP_DIR="$1" echo "â„šī¸ Backup destination directory: $BACKUP_DIR" fi # Check if any paths are configured if [ ${#PATHS_TO_BACKUP[@]} -eq 0 ]; then echo "🛑 Error: No paths configured for backup" echo "🛑 Please edit the script and add paths to the PATHS_TO_BACKUP array" exit 1 fi echo "â„šī¸ Starting backup at $(date)" # Process each configured path for item in "${PATHS_TO_BACKUP[@]}"; do backup_item "$item" "$BACKUP_DIR" done echo "â„šī¸ Backup complete! $(date)"