aboutsummaryrefslogtreecommitdiffstats

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

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):

cp config.example ~/.config/dot-backup/config
touch ~/.config/dot-backup/files.list

Add a remote to the backup repo so you can push:

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:

./dot-backup.sh

Or automate with cron (silent, logs to ~/.local/share/dot-backup/backup.log):

# crontab -e
0 * * * * /path/to/dot-backup.sh -q

Push manually after backup:

cd ~/Programming/GIT/my-dotfiles && git push

Or set GIT_REMOTE in config and use -p to push automatically:

./dot-backup.sh -p
# or combined with cron:
./dot-backup.sh -q -p

Restoring on a New Machine

Preview what would be restored first:

./dot-backup.sh -r --dry-run

Then restore:

./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:

cp config.example ~/.config/dot-backup/config

Available options:

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

External list (recommended) — create ~/.config/dot-backup/files.list with one path per line. When this file exists and is non-empty it replaces the built-in list entirely:

# shell
.bashrc
.bash_aliases

# system files
/etc/hosts
/etc/fstab

Built-in fallback — if files.list is missing or empty, the script uses the DOTFILES array hardcoded in dot-backup.sh.

Relative paths are treated as $HOME-relative. Absolute paths (starting with /) are treated as system files.