2023-12-01 16:17:35 +00:00
|
|
|
#!/bin/sh -e
|
2022-01-23 16:44:51 +00:00
|
|
|
# Backup linux system and user files with borg
|
2023-07-20 12:58:14 +00:00
|
|
|
# Usage: bag [-n] [--dry-run] [--home] [--root MOUNTED_ROOT]
|
2022-01-19 20:25:19 +00:00
|
|
|
root="/"
|
2022-01-15 21:03:32 +00:00
|
|
|
args="--progress --stats"
|
2022-01-13 19:52:03 +00:00
|
|
|
while test $# -gt 0; do
|
|
|
|
case "$1" in
|
|
|
|
(-n) run="arg-test"; shift;;
|
2022-01-15 21:03:32 +00:00
|
|
|
(--dry-run) args="--dry-run --list"; name=test-$(date +%s); shift;;
|
2022-01-13 19:52:03 +00:00
|
|
|
(--root) shift; root="$1"; name="$(basename "$(realpath "$root")")"; echo $root $name; shift;;
|
2023-12-01 16:17:35 +00:00
|
|
|
(--home) "$0" home /home -e "sh:$HOME/.local/state" -e "sh:$HOME/data/4-*" -e "sh:$HOME/data/5-*" -e "sh:**/.stfolder" -e "sh:**/0-forks" -e "sh:**/.git"
|
|
|
|
exit $?;;
|
2022-01-13 19:52:03 +00:00
|
|
|
(-*) name="${1#-}"; shift;;
|
2022-01-19 20:25:19 +00:00
|
|
|
(*) break;;
|
2022-01-13 19:52:03 +00:00
|
|
|
esac
|
|
|
|
done
|
2022-01-19 20:25:19 +00:00
|
|
|
test $# -gt 0 || cd "$root"
|
2022-01-13 19:52:03 +00:00
|
|
|
name="::$(test -n "$name" && echo "$name" || cat /etc/hostname)_${1:-system}_$(date -u +"%y%m%d")"
|
|
|
|
echo "Backing up as $name"
|
2023-12-01 16:17:35 +00:00
|
|
|
# TODO ignore electron caches
|
2022-04-27 23:49:20 +00:00
|
|
|
${run:-sudo --preserve-env=BORG_REPO BORG_PASSPHRASE="$($BORG_PASSCOMMAND)" borg} create --exclude-caches $args \
|
2022-12-22 14:40:56 +00:00
|
|
|
$(echo $DIRS_IGNORE_SAFE -x 'software-challenge/**/build' -x state/emacs -x state/go -x .local/lib -x .cpan -x *cache -x $HOME/.gem |
|
2022-01-15 21:03:32 +00:00
|
|
|
sed 's|-x \([^ ]\+\)|-e sh:**/\1|g') \
|
|
|
|
"$name" $(test $# -eq 0 && echo etc home root || test $# -eq 1 && echo $1) "${@:2}" 2>&1
|
2022-05-03 09:29:48 +00:00
|
|
|
test -n "$run" || sudo chown -R $USER:$USER "$BORG_REPO"
|