android-borgmatic-setup/init.sh

118 lines
3.0 KiB
Bash
Raw Normal View History

2019-10-26 12:41:49 -04:00
#!/bin/bash
{
# For later:
# /data/data/com.termux/files/usr/libexec/termux-api JobScheduler --ei job_id 1 --ei period_ms 900000 --ez charging true -es script $HOME/test.sh
set -ex
SCRIPT_DIR="$(cd "${BASH_SOURCE%/*}"; pwd)"
DEBS_URL="https://linx.austenwares.com/selif/z4v8ou3s.tar"
DEBS_FNAME="${DEBS_URL##*/}"
DEBS_HASH="033a276609c8ae55062c991a5062282df73e395a5746ab709af982780cb56491 $DEBS_FNAME"
die() {
echo "ERROR: $1" >&2
exit 1
}
install_requirements() {
2019-10-26 23:32:34 -04:00
termux-setup-storage
2019-10-26 14:37:11 -04:00
LAST_UPDATE="$(stat -c %Y /data/data/com.termux/files/usr/var/cache/apt/pkgcache.bin)"
NOW="$(date +%s)"
if ((NOW - LAST_UPDATE > 60 * 60 * 24)); then
apt-get update
fi
2019-10-26 12:41:49 -04:00
apt-get upgrade -y
2019-10-26 23:32:34 -04:00
apt-get install -y python screen wget openssh clang
2019-10-26 12:41:49 -04:00
if ! command -v borgmatic 2>/dev/null || ! command -v borg 2>/dev/null; then
get_debs
install_debs
fi
2019-10-26 22:43:59 -04:00
if [[ ! -e ~/.ssh/id_ed25519 ]]; then
ssh-keygen -t ed25519 -N '' -f ~/.ssh/id_ed25519
fi
2019-10-26 23:32:34 -04:00
if ! command -v borgmatic; then
pip3 install borgmatic
fi
2019-10-26 12:41:49 -04:00
}
get_debs() {
wget "$DEBS_URL" -O "$DEBS_FNAME"
sha256sum -c <(echo "$DEBS_HASH") || die "Download hash mismatch"
}
install_debs() {
tar -xf "$DEBS_FNAME"
pushd debs
dpkg -i *.deb || :
apt-get -f install
popd
}
replace () {
FILE="$1"
PLACEHOLDER="${2}_PLACEHOLDER"
2019-10-26 14:37:11 -04:00
VALUE="$3"
if (( $# == 4 )); then
PROMT="$4"
printf "%s" "$PROMT [$VALUE] > "
read -r
if [[ -n "$REPLY" ]]; then
VALUE="$REPLY"
fi
2019-10-26 12:41:49 -04:00
fi
# Do the replacement
sed -i "$FILE" -e "s/${PLACEHOLDER//\//\\/}/${VALUE//\//\\/}/g"
}
install_templates() {
echo "Installling templates"
BORGMATIC_CONFIG_FILE="$HOME/.config/borgmatic/config.yaml"
2019-10-26 14:37:11 -04:00
BORGMATIC_EXCLUDES_FILE="$HOME/.config/borgmatic/excludes"
2019-10-26 12:41:49 -04:00
if [[ "$1" = "-f" ]] || [[ ! -e "$BORGMATIC_CONFIG_FILE" ]]; then
mkdir -p "$HOME/.config/borgmatic"
cp "$SCRIPT_DIR/config.yaml" "$BORGMATIC_CONFIG_FILE"
2019-10-26 23:32:34 -04:00
set +x
2019-10-26 14:37:11 -04:00
replace "$BORGMATIC_CONFIG_FILE" REPO "user@files:$(hostname)" "What is your repository?"
replace "$BORGMATIC_CONFIG_FILE" CROSS_FILESYSTEM "true" "Cross filesystem?"
replace "$BORGMATIC_CONFIG_FILE" REMOTE_PATH "borg1" "Remote borg command?"
replace "$BORGMATIC_CONFIG_FILE" PASSWORD "$(tr -dc "+|~=[];:\-/.,<>?'#@!$%^&*(){}_A-Z-a-z-0-9" < /dev/urandom | head -c32)" "Encryption password?"
replace "$BORGMATIC_CONFIG_FILE" EXCLUDE_FROM "$BORGMATIC_EXCLUDES_FILE"
2019-10-26 12:41:49 -04:00
set -x
echo "Config file differences:"
diff -Nu "$SCRIPT_DIR/config.yaml" "$BORGMATIC_CONFIG_FILE" || :
fi
2019-10-26 14:37:11 -04:00
if [[ "$1" = "-f" ]] || [[ ! -e "$BORGMATIC_EXCLUDES_FILE" ]]; then
mkdir -p "$HOME/.config/borgmatic"
cp "$SCRIPT_DIR/excludes" "$BORGMATIC_EXCLUDES_FILE"
fi
2019-10-26 12:41:49 -04:00
}
install_complete() {
cat <<-EOF
2019-10-26 22:43:59 -04:00
2019-10-26 12:41:49 -04:00
Installation complete!
2019-10-26 22:43:59 -04:00
Your ssh key:
2019-10-26 22:46:19 -04:00
$(cat ~/.ssh/id_ed25519.pub)
2019-10-26 22:43:59 -04:00
Configure your excludes
${EDITOR:-vim} "$BORGMATIC_EXCLUDES_FILE"
2019-10-26 12:41:49 -04:00
To setup your repository, run:
borgmatic init -e repokey-blake2
To make a backup, run:
borgmatic --verbosity 2
EOF
}
2019-10-26 22:43:59 -04:00
cd "$(mktemp -d)"
2019-10-26 12:41:49 -04:00
install_requirements
install_templates "$1"
install_complete
}