1
0
Files
dotfiles/scripts/10-backup-files.sh

66 lines
2.2 KiB
Bash

#!/bin/bash
# scripts/10-backup-files.sh - Backup existing files that would conflict
set -euo pipefail
# Source utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/utils.sh"
backup_files() {
local dotfiles_dir="$1"
log_info "Checking for file conflicts..."
local backed_up=()
# Auto-discover files from stow directories
if [ -d "$dotfiles_dir/stow" ]; then
# Use new stow structure - scan all files in stow subdirectories
for config_dir in "$dotfiles_dir/stow"/*; do
if [ -d "$config_dir" ]; then
log_info "Checking $(basename "$config_dir") configuration for conflicts..."
# Find all files that would be stowed
find "$config_dir" -type f | while read -r file; do
# Get relative path from config directory
relative_file="${file#$config_dir/}"
target_file="$HOME/$relative_file"
if [ -f "$target_file" ] && [ ! -L "$target_file" ]; then
log_warning "Backing up existing file: $target_file -> ${target_file}.bak"
mv "$target_file" "${target_file}.bak"
backed_up+=("$relative_file")
fi
done
fi
done
else
# Fallback to old structure - hardcoded file list
local files_to_check=(.bashrc .bash_aliases .inputrc .gitconfig)
for file in "${files_to_check[@]}"; do
if [ -f "$HOME/$file" ] && [ ! -L "$HOME/$file" ]; then
log_warning "Backing up existing file: $HOME/$file -> $HOME/${file}.bak"
mv "$HOME/$file" "$HOME/${file}.bak"
backed_up+=("$file")
fi
done
fi
if [ ${#backed_up[@]} -gt 0 ]; then
log_info "Backed up ${#backed_up[@]} file(s): ${backed_up[*]}"
else
log_info "No file conflicts found"
fi
}
# Run if called directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
if [ $# -ne 1 ]; then
log_error "Usage: $0 <dotfiles_directory>"
exit 1
fi
backup_files "$1"
fi