android-borgmatic-setup/init.sh
2019-10-26 14:37:11 -04:00

103 lines
2.7 KiB
Bash
Executable File

#!/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() {
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
apt-get upgrade -y
apt-get install -y python screen wget
if ! command -v borgmatic 2>/dev/null || ! command -v borg 2>/dev/null; then
get_debs
install_debs
fi
}
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"
VALUE="$3"
if (( $# == 4 )); then
PROMT="$4"
printf "%s" "$PROMT [$VALUE] > "
read -r
if [[ -n "$REPLY" ]]; then
VALUE="$REPLY"
fi
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"
BORGMATIC_EXCLUDES_FILE="$HOME/.config/borgmatic/excludes"
if [[ "$1" = "-f" ]] || [[ ! -e "$BORGMATIC_CONFIG_FILE" ]]; then
mkdir -p "$HOME/.config/borgmatic"
cp "$SCRIPT_DIR/config.yaml" "$BORGMATIC_CONFIG_FILE"
set -x
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"
set -x
echo "Config file differences:"
diff -Nu "$SCRIPT_DIR/config.yaml" "$BORGMATIC_CONFIG_FILE" || :
fi
if [[ "$1" = "-f" ]] || [[ ! -e "$BORGMATIC_EXCLUDES_FILE" ]]; then
mkdir -p "$HOME/.config/borgmatic"
cp "$SCRIPT_DIR/excludes" "$BORGMATIC_EXCLUDES_FILE"
fi
}
install_complete() {
cat <<-EOF
Installation complete!
To setup your repository, run:
borgmatic init -e repokey-blake2
To make a backup, run:
borgmatic --verbosity 2
EOF
}
cd "/data/data/com.termux/files/usr/tmp/tmp.k5RE3ly9Hm"
# cd "$(mktemp -d)"
install_requirements
install_templates "$1"
install_complete
}