From 76e2e75a9c8f5c95b20fe1ecd216898ce445c560 Mon Sep 17 00:00:00 2001 From: Jono Targett Date: Wed, 3 Jul 2024 20:25:29 +0930 Subject: [PATCH] Added rysnc importer/exporter script with backup --- export | 22 ++++++++++++++++++++++ import | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ tracked-files | 2 ++ 3 files changed, 73 insertions(+) create mode 100755 export create mode 100755 import create mode 100644 tracked-files diff --git a/export b/export new file mode 100755 index 0000000..7757988 --- /dev/null +++ b/export @@ -0,0 +1,22 @@ +#! /bin/bash + +set -eu + +repo_root=$(git rev-parse --show-toplevel) + +# Test for newer changes in the dotfiles. Protection against clobbering changes you might want to preserve. +dry_run=$(rsync -avu --itemize-changes --dry-run --files-from=./tracked-files --exclude-from=./.gitignore . ~) +if echo "${dry_run}" | grep -q '^<'; then + echo "Some installed files have newer mtimes than the repository versions. Please import the changes first." + exit 1 +elif echo "${dry_run}" | grep -q '^>'; then + echo "Some installed files differ from the repository versions. Creating a backup..." + backup_dir=$(mktemp -d -t backupXXXXXXXX) + ${repo_root}/import ${backup_dir} y > /dev/null + mkdir -p ${repo_root}/.backup/ + tar -czf ${repo_root}/.backup/$(basename $backup_dir).tgz $backup_dir +fi + +# Actually perform the copy +rsync -avu --itemize-changes --files-from=./tracked-files --exclude-from=./.gitignore . ~ + diff --git a/import b/import new file mode 100755 index 0000000..ae1777a --- /dev/null +++ b/import @@ -0,0 +1,49 @@ +#! /bin/bash + +set -eu + +function check_clean() { + if ! git diff-index --quiet HEAD -- ; then + echo "Git repository is dirty, aborting." + git status --short --untracked-files=no + exit 1 + fi +} + +function dry_run() { + rsync -HPrlm \ + --itemize-changes \ + --dry-run \ + --files-from=./tracked-files \ + --exclude-from=./.gitignore \ + "${source}" "${destination}" +} + + +source=${HOME} +repo_root=$(git rev-parse --show-toplevel) +destination="${1:-$repo_root}" + +echo "Importing working dotfiles to $destination" +dry_run + +if [ -z "${1+x}" ]; then + echo "Performing git repository check..." + check_clean +fi + +if [ -z "${2+x}" ]; then + read -r -p "Are you sure? [y/N] " response + response=${response,,} +else + response=$2 +fi + +if [[ "$response" =~ ^(yes|y)$ ]]; then + rsync -HPrlm \ + --itemize-changes \ + --files-from=./tracked-files \ + --exclude-from=./.gitignore \ + "${source}" "${destination}" +fi + diff --git a/tracked-files b/tracked-files new file mode 100644 index 0000000..e68ec5b --- /dev/null +++ b/tracked-files @@ -0,0 +1,2 @@ +.vim/ +.vimrc