borg-remote-android/setup.sh

76 lines
2.6 KiB
Bash
Raw Normal View History

2018-09-21 17:27:49 -04:00
#!/data/data/com.termux/files/usr/bin/bash
ABSPATH="$(readlink -f "$BASH_SOURCE")"
cd "${ABSPATH%/*}" || { echo "Cannot change directory" >&2; exit 1; }
function ask() {
echo -n "$1 [Y/n]? "
read RESP
if [ -z "$RESP" ] || [ "${RESP,,}" = "y" ]; then
return 0
fi
return 1
}
# Read a variable
function configure_var() {
# If the variable already exists, continue without asking questions
if [ ! -z "${!1}" ]; then
echo "It is ${!1}"
return
fi
DEFAULT="$2"
echo -n "export $1= ($2): "
read RESP
RESP="${RESP:-$DEFAULT}"
# Save the variable
#TODO: This introduces possible bugs when quotes are involved with RESP
echo "# Line added on $(date +%F-%T)" >>env
echo "export $1=\"$RESP\"" >>env
# Load the variable for use in this script
export "$1"="$RESP"
}
# Begin configuration
echo
echo "Asking config questions..."
echo "IMPORTANT: Trailing slashes matter for paths. Consult the rsync manual for details."
echo "It is recommended to use trailing slashes for all _LOCATION variables."
echo "Also, paths can be relative or absolute"
echo
# Load existing configurations
test -f ./env && source ./env
configure_var LOCAL_HOSTNAME android1
configure_var REMOTE_HOST user@example.com
configure_var SOURCE_LOCATION "/storage/emulated/0/"
configure_var DESTINATION_LOCATION "tmp/borg/$LOCAL_HOSTNAME/"
configure_var BORG_REPO "ssh://$REMOTE_HOST/~/borg-backups"
configure_var BORG_COMMAND "borg create --verbose --progress --stats --one-file-system --exclude-caches --compression=auto,lzma --exclude=storage/emulated/0/Android"
configure_var RSYNC_COMMAND "rsync --relative --recursive --links --times --human-readable --partial --info=progress2 --delete --delete-excluded --exclude=storage/emulated/0/Android"
# Install borg-backup into ~/.shortcuts
mkdir -p ~/.shortcuts
if [ -L ~/.shortcuts/borg-backup ]; then
rm ~/.shortcuts/borg-backup
fi
ln -s "$(readlink -f backup-android.sh)" ~/.shortcuts/borg-backup || echo "Unable to link to ~/.shortcuts" >&2
# Ask about authentication
SSH_KEYGEN_COMMAND="ssh-keygen -b 512 -t ed25519 -f $HOME/.ssh/id_ed25519 -N ''"
ask "Generate a new ssh key ($SSH_KEYGEN_COMMAND)" && $SSH_KEYGEN_COMMAND
ask "Run ssh-copy-id on host" && ssh-copy-id "$HOST"
# Create destination location
echo "Creating DESTINATION_LOCATION"
./run_remote.sh mkdir -p "$DESTINATION_LOCATION"
# Exclude file
touch exclude
ask "Edit exclude file with vi" && vi exclude
# Run final checks
ask "Check if borg command exists (on remote)" && { ./run_remote.sh command -v borg || echo "Borg command not found on remote host"; }
ask "Run borg list (on remote)" && ./run_remote.sh borg list "$BORG_REPO"