This guide shows you how to backup your dotfiles and restore them on a new machine. Use this when migrating to a new computer or creating backups of your configuration.
Ensure you have:
Use this for complete configuration backup including history.
cd ~/.dotfiles
git status
git add -A
git commit -m "backup: configuration snapshot $(date +%Y-%m-%d)"
git push origin main
git tag -a "backup-$(date +%Y-%m-%d)" -m "Configuration backup"
git push origin --tags
Use this when you only want specific configurations.
git checkout -b backup/machine-name-$(date +%Y%m%d)
# Example: exclude work-specific configs
rm -rf work/
git add -A
git commit -m "backup: filtered configuration"
git push origin backup/machine-name-$(date +%Y%m%d)
Use this for offline backups without Git history.
cd ~
tar -czf dotfiles-backup-$(date +%Y%m%d).tar.gz \
--exclude='.git' \
--exclude='*.zwc' \
--exclude='.DS_Store' \
.dotfiles/
tar -czf complete-backup-$(date +%Y%m%d).tar.gz \
.dotfiles/ \
.ssh/config \
.gnupg/pubring.kbx \
.gnupg/trustdb.gpg
# On macOS
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install git stow
git clone https://github.com/yourusername/dotfiles.git ~/.dotfiles
cd ~/.dotfiles
When git or stow aren’t available on the target system:
# Copy dotfiles to remote system
rsync -avz ~/.dotfiles/ user@remote-host:~/dotfiles-backup/
# On remote system, manually copy needed files
cp ~/dotfiles-backup/zsh/.zshrc ~/.zshrc
cp ~/dotfiles-backup/git/.gitconfig ~/.gitconfig
# Create archive on source machine
tar -czf dotfiles-essential.tar.gz \
.dotfiles/zsh/.zshrc \
.dotfiles/git/.gitconfig \
.dotfiles/vim/.vimrc
# Transfer and extract on target
scp dotfiles-essential.tar.gz user@remote-host:
ssh user@remote-host 'tar -xzf dotfiles-essential.tar.gz'
For quick setup of essential configurations on remote systems:
# Essential files for basic functionality
scp ~/.zshrc user@remote-host:~/.zshrc
scp ~/.gitconfig user@remote-host:~/.gitconfig
# On remote system, znap will auto-install when .zshrc is sourced
ssh user@remote-host
zsh # Will trigger znap installation and plugin setup
Note: The znap plugin manager will automatically clone and set up plugins when the shell configuration is first loaded, making remote setup straightforward.
# List available backups
git tag -l "backup-*"
git branch -r | grep backup/
# Restore specific backup
git checkout backup-2024-01-15
# Dry run to see what would happen
stow -n -v git zsh vim
# Create backup directory
mkdir ~/config-backup-$(date +%Y%m%d)
# Move existing files
[[ -f ~/.gitconfig ]] && mv ~/.gitconfig ~/config-backup-*/
[[ -f ~/.zshrc ]] && mv ~/.zshrc ~/config-backup-*/
# Apply all configs
stow git zsh vim homebrew
# Or selectively
stow git # Just git configs
stow zsh # Just shell configs
brew bundle --file=~/.config/homebrew/Brewfile
# Znap (if using zsh config)
git clone --depth 1 https://github.com/marlonrichert/zsh-snap.git ~/znap/zsh-snap
# Restart shell
exec zsh
gpg --import ~/config-backup-*/gpg-private-keys.asc
gpg --import-ownertrust ~/config-backup-*/gpg-ownertrust.txt
Check that configurations are properly restored:
# Verify symlinks
ls -la ~ | grep "\->"
# Test git aliases
git st # Should work if aliases restored
# Check shell configuration
echo $SHELL
echo $ZDOTDIR
# Verify Git identity
git config user.name
git config user.email
Create ~/.dotfiles/scripts/backup.sh
:
#!/usr/bin/env bash
set -euo pipefail
BACKUP_DATE=$(date +%Y-%m-%d)
DOTFILES_DIR="$HOME/.dotfiles"
# Function to backup dotfiles
backup_dotfiles() {
cd "$DOTFILES_DIR"
# Check for uncommitted changes
if [[ -n $(git status -s) ]]; then
echo "📦 Committing current changes..."
git add -A
git commit -m "backup: auto-backup $BACKUP_DATE"
fi
# Push to remote
echo "📤 Pushing to remote..."
git push origin main
# Create backup tag
echo "🏷️ Creating backup tag..."
git tag -a "backup-$BACKUP_DATE" -m "Automated backup"
git push origin --tags
echo "✅ Backup complete!"
}
# Run backup
backup_dotfiles
Make it executable:
chmod +x ~/.dotfiles/scripts/backup.sh
Add to crontab for daily backups:
# Edit crontab
crontab -e
# Add daily backup at 10 PM
0 22 * * * ~/.dotfiles/scripts/backup.sh >> ~/.dotfiles/backup.log 2>&1
Problem: Stow conflicts during restore
Solution:
--adopt
to adopt existing files: stow --adopt git
Problem: Missing dependencies after restore
Solution:
brew bundle
to install from BrewfileProblem: Git push fails with authentication error
Solution:
ssh-keygen -t ed25519
git remote set-url origin git@github.com:user/dotfiles.git
Problem: Restored shell doesn’t work properly
Solution:
brew install zsh
chsh -s $(which zsh)