Files
disaster_recovery/init_backup.sh
2021-03-16 23:51:55 +01:00

108 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
## This is an interactive prompt to guide the user the setting up the correct .env file.
echo -e "Enter name used for this backup, this should be uniq. \nIt will be used to name the configuration files and other files."
read -p "Backup name: " -r
NAME=$REPLY
echo -e "Do you want to generate a new keypair? (Y) or use excisting private key. (N)"
read -p "[Y]/[N] " -n 1
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
ssh-keygen -t ed25519 -f "id_ed25519_$NAME"
PRIVATE_KEY_FILE="id_ed25519_$NAME"
else
read -p "Input full path and name of private key: "
PRIVATE_KEY_FILE=$REPLY
fi
echo -e "Enter url of repository server, eg. \"disaster-recovery.example.com\""
read -p "URL: " -r
REPOSITORY_URL=$REPLY
echo -e "Enter path of TARGET path on the SERVER eg. \"/backup/server_01\""
read -p "Path: " -r
REPOSITORY_DIR=$REPLY
echo -e "Enter username for the SERVER user."
read -p "USER: " -r
REPOSITORY_USER=$REPLY
unset REPLY
read -p "Specify SSH port to be used (Default: 22, Hetzner use 23 for Borg!): " -r
echo ""
if [[ -z $REPLY ]]; then
REPOSITORY_PORT=22
else
REPOSITORY_PORT=$REPLY
fi
echo -e "Do you want to upload the new public_key to the server via SCP?"
read -p "[Y]/[N] " -n 1
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo ""
unset REPLY
read -p "Specify port to upload with (Default 22): " -r
echo ""
if [[ -z $REPLY ]]; then
REPLY=22
fi
TEMPDIR=$(mktemp -d)
scp -P "$REPLY" "$REPOSITORY_USER@$REPOSITORY_URL:.ssh/authorized_keys" \
"$TEMPDIR/authorized_keys"
cat "$PRIVATE_KEY_FILE.pub" >> "$TEMPDIR/authorized_keys"
scp -P "$REPLY $TEMPDIR/authorized_keys" \
"$REPOSITORY_USER@$REPOSITORY_URL:.ssh/authorized_keys"
rm -rf "$TEMPDIR"
fi
echo -e "Do you want to initialize the repository now?"
read -p "[Y]/[N] " -n 1
echo ""
unset REPLY
if [[ $REPLY =~ ^[Yy]$ ]]; then
INIT=1
echo -e "\nYour borg repository will be initialized with the following settings:"
fi
# Final output
echo -e "\n##############################"
echo -e "Please verify before using the following in your .env file:"
echo -e "# REPOSITORY_URL=\"$REPOSITORY_URL\""
echo -e "# REPOSITORY_DIR=\"$REPOSITORY_DIR\""
echo -e "# REPOSITORY_USER=\"$REPOSITORY_USER\""
echo -e "# REPOSITORY_PORT=\"$REPOSITORY_PORT\""
echo -e "# PRIVATE_KEY_FILE=\"$PRIVATE_KEY_FILE\""
echo -e "##############################\n"
if [[ -n $INIT ]]; then
echo -e "Remember to save your password and add it to your .env file."
echo -e "Please wait while repository is initialized, this can take a while."
## Initialize the repository
borg init --encryption=repokey \
--rsh="ssh -i $PRIVATE_KEY_FILE" \
ssh://$REPOSITORY_USER@$REPOSITORY_URL:$REPOSITORY_PORT/./$REPOSITORY_DIR/
## Make a backup of the borg key. (Keep this SAFE!)
echo -e "Your borg key will now be exported to ./backup_key_$NAME.borgkey"
borg key export \
ssh://$REPOSITORY_USER@$REPOSITORY_URL:$REPOSITORY_PORT/./$REPOSITORY_DIR/ \
"./backup_key_$NAME.borgkey"
echo -e "Your repository should have been initialized and key exported."
echo -e "Keep the following SAFE AND A COPY SOMEWHERE ELSE!"
echo -e "#######################################################"
echo -e "Your private key for the ssh access: $PRIVATE_KEY_FILE"
echo -e "Your public key for the ssh access: $PRIVATE_KEY_FILE.pub"
echo -e "Your borg keyfile: backup_key_$NAME.borgkey"
echo -e "Your borg passphrase!"
echo -e "#######################################################\n"
echo -e "If everything went well, you are ready to customize your .env file with the above information. \nAnd can then make your initial backup."
fi