# dots-backup Bash script to back up dotfiles into a local git repo, and restore them on any system. ## Requirements - `bash` - `rsync` - `git` ## How It Works `dot-backup.sh` reads a list of dotfiles, copies them into a local git repo using `rsync`, then auto-commits any changes. Backup layout: ``` ~/Programming/GIT/my-dotfiles/ ├── home/ # files from $HOME (relative paths) │ ├── .bashrc │ ├── .gitconfig │ └── .config/ │ └── nvim/ └── system/ # files from / (absolute paths) └── etc/ └── bash_completion.d/ ``` ## First Run ```bash git clone https://github.com/you/dots-backup cd dots-backup ./dot-backup.sh ``` On first run the script: 1. Creates `~/.config/dot-backup/` and `~/.local/share/dot-backup/` and prints a short setup summary 2. Creates `~/Programming/GIT/my-dotfiles/{home,system}` and initializes a git repo inside it 3. Runs the backup immediately using built-in defaults Optionally set up a config file and custom files list (script will tell you the exact paths): ```bash cp config.example ~/.config/dot-backup/config touch ~/.config/dot-backup/files.list ``` Add a remote to the backup repo so you can push: ```bash cd ~/Programming/GIT/my-dotfiles git remote add origin git@github.com:you/my-dotfiles.git git push -u origin master ``` ## Usage ``` ./dot-backup.sh [options] -n, --dry-run Show what would be copied without copying -v, --verbose Print each file as rsync transfers it -r, --restore Restore dotfiles from backup to original locations -q, --quiet Suppress stdout; write output to log instead -p, --push Push to remote after commit -h, --help Show this help ``` ## Daily Routine Run manually after changing config: ```bash ./dot-backup.sh ``` Or automate with cron (silent, logs to `~/.local/share/dot-backup/backup.log`): ```bash # crontab -e 0 * * * * /path/to/dot-backup.sh -q ``` Push manually after backup: ```bash cd ~/Programming/GIT/my-dotfiles && git push ``` Or set `GIT_REMOTE` in config and use `-p` to push automatically: ```bash ./dot-backup.sh -p # or combined with cron: ./dot-backup.sh -q -p ``` ## Restoring on a New Machine Preview what would be restored first: ```bash ./dot-backup.sh -r --dry-run ``` Then restore: ```bash ./dot-backup.sh -r ``` System files (absolute paths like `/etc/bash_completion.d`) may need `sudo`. ## Configuration Copy `config.example` to `~/.config/dot-backup/config` and edit: ```bash cp config.example ~/.config/dot-backup/config ``` Available options: ```bash DEFAULT_OUTPUT_DIR="${HOME}/Programming/GIT/my-dotfiles" LOG_FILE="${HOME}/.local/share/dot-backup/backup.log" DOTFILES_LIST="${HOME}/.config/dot-backup/files.list" GIT_REMOTE="git@github.com:you/my-dotfiles.git" GIT_BRANCH="master" ``` `GIT_REMOTE` — if set, script ensures it is registered as `origin` on every run. Required for `--push`. `GIT_BRANCH` defaults to the current branch if unset. ## Adding Files **Edit the script** — add paths to the `DOTFILES` array in `dot-backup.sh`. **Or use the external list** — add paths to `~/.config/dot-backup/files.list`, one per line: ``` # extra dotfiles .config/myapp .config/otherapp.conf /etc/hosts ``` Relative paths are treated as `$HOME`-relative. Absolute paths (starting with `/`) are treated as system files.