Updated backup scripts for dual VPN

This commit is contained in:
Orazio 2020-05-29 17:46:04 +02:00
parent ba7c46aae8
commit 427c484cac
4 changed files with 48 additions and 20 deletions

View file

@ -1,7 +1,5 @@
#!/bin/bash #!/bin/bash
# PiVPN: Backup Script
backupdir=pivpnbackup
date=$(date +%Y%m%d-%H%M%S)
# Find the rows and columns. Will default to 80x24 if it can not be detected. # Find the rows and columns. Will default to 80x24 if it can not be detected.
screen_size=$(stty size 2>/dev/null || echo 24 80) screen_size=$(stty size 2>/dev/null || echo 24 80)
@ -15,19 +13,41 @@ c=$(( columns / 2 ))
r=$(( r < 20 ? 20 : r )) r=$(( r < 20 ? 20 : r ))
c=$(( c < 70 ? 70 : c )) c=$(( c < 70 ? 70 : c ))
chooseVPNCmd=(whiptail --backtitle "Setup PiVPN" --title "Installation mode" --separate-output --radiolist "Choose a VPN configuration to backup (press space to select):" "${r}" "${c}" 2) backupdir=pivpnbackup
date=$(date +%Y%m%d-%H%M%S)
setupVarsFile="setupVars.conf"
setupConfigDir="/etc/pivpn"
if [ -r "${setupConfigDir}/wireguard/${setupVarsFile}" ] && [ -r "${setupConfigDir}/openvpn/${setupVarsFile}" ]; then
# Two protocols have been installed, check if the script has passed
# an argument, otherwise ask the user which one he wants to remove
if [ $# -ge 1 ]; then
VPN="$1"
echo "::: Backing up VPN: $VPN"
else
chooseVPNCmd=(whiptail --backtitle "Setup PiVPN" --title "Backup" --separate-output --radiolist "Both OpenVPN and WireGuard are installed, choose a VPN to backup (press space to select):" "${r}" "${c}" 2)
VPNChooseOptions=(WireGuard "" on VPNChooseOptions=(WireGuard "" on
OpenVPN "" off) OpenVPN "" off)
if VPN=$("${chooseVPNCmd[@]}" "${VPNChooseOptions[@]}" 2>&1 >/dev/tty) ; then if VPN=$("${chooseVPNCmd[@]}" "${VPNChooseOptions[@]}" 2>&1 >/dev/tty) ; then
echo "::: Using VPN: $VPN" echo "::: Backing up VPN: $VPN"
VPN="${VPN,,}" VPN="${VPN,,}"
else else
echo "::: Cancel selected, exiting...." echo "::: Cancel selected, exiting...."
exit 1 exit 1
fi fi
fi
setupVars="/etc/pivpn/${VPN}/setupVars.conf" setupVars="${setupConfigDir}/${VPN}/${setupVarsFile}"
else
if [ -r "${setupConfigDir}/wireguard/${setupVarsFile}" ]; then
setupVars="${setupConfigDir}/wireguard/${setupVarsFile}"
elif [ -r "${setupConfigDir}/openvpn/${setupVarsFile}" ]; then
setupVars="${setupConfigDir}/openvpn/${setupVarsFile}"
fi
fi
if [ ! -f "${setupVars}" ]; then if [ ! -f "${setupVars}" ]; then
echo "::: Missing setup vars file!" echo "::: Missing setup vars file!"
@ -53,7 +73,7 @@ backup_openvpn(){
backupzip=$date-pivpnovpnbackup.tgz backupzip=$date-pivpnovpnbackup.tgz
# shellcheck disable=SC2210 # shellcheck disable=SC2210
tar czpf "$install_home"/"$backupdir"/"$backupzip" "$openvpndir" "$ovpnsdir" > /dev/null 2>&1 tar czpf "$install_home"/"$backupdir"/"$backupzip" "$openvpndir" "$ovpnsdir" > /dev/null 2>&1
echo -e "Backup created in $install_home/$backupdir/$backupzip \nTo restore the backup, follow instructions at:\nhttps://github.com/pivpn/pivpn/wiki/FAQ#how-can-i-migrate-my-configs-to-another-pivpn-instance" echo -e "Backup created in $install_home/$backupdir/$backupzip \nTo restore the backup, follow instructions at:\nhttps://github.com/pivpn/pivpn/wiki/OpenVPN#how-can-i-migrate-my-configs-to-another-pivpn-instance"
} }
@ -64,7 +84,7 @@ backup_wireguard(){
checkbackupdir checkbackupdir
backupzip=$date-pivpnwgbackup.tgz backupzip=$date-pivpnwgbackup.tgz
tar czpf "$install_home"/"$backupdir"/"$backupzip" "$wireguarddir" "$configsdir" > /dev/null 2>&1 tar czpf "$install_home"/"$backupdir"/"$backupzip" "$wireguarddir" "$configsdir" > /dev/null 2>&1
echo -e "Backup created in $install_home/$backupdir/$backupzip \nTo restore the backup, follow instructions at:\nhttps://github.com/pivpn/pivpn/wiki/FAQ#how-can-i-migrate-my-configs-to-another-pivpn-instance" echo -e "Backup created in $install_home/$backupdir/$backupzip \nTo restore the backup, follow instructions at:\nhttps://github.com/pivpn/pivpn/wiki/WireGuard#how-can-i-migrate-my-configs-to-another-pivpn-instance"
} }

View file

@ -58,7 +58,7 @@ function update {
} }
function backup { function backup {
$SUDO ${scriptDir}/backup.sh $SUDO ${scriptDir}/backup.sh "${vpn}"
exit 0 exit 0
} }

View file

@ -17,6 +17,11 @@ uninstallServer(){
exit 0 exit 0
} }
backup(){
$SUDO ${scriptDir}/backup.sh
exit 0
}
showHelp(){ showHelp(){
echo "::: To pass off to the pivpn command for each protocol" echo "::: To pass off to the pivpn command for each protocol"
echo ":::" echo ":::"
@ -25,6 +30,7 @@ showHelp(){
echo ":::" echo ":::"
echo "::: -h, help Show this help dialog" echo "::: -h, help Show this help dialog"
echo "::: -u, uninstall Uninstall pivpn from your system!" echo "::: -u, uninstall Uninstall pivpn from your system!"
echo "::: -bk, backup Backup VPN configs and user profiles"
exit 0 exit 0
} }
@ -38,5 +44,6 @@ case "$1" in
ovpn ) "${scriptDir}/openvpn/pivpn.sh" "${@:2}";; ovpn ) "${scriptDir}/openvpn/pivpn.sh" "${@:2}";;
"-h" | "help" ) showHelp;; "-h" | "help" ) showHelp;;
"-u" | "uninstall" ) uninstallServer;; "-u" | "uninstall" ) uninstallServer;;
"-bk" | "backup" ) backup ;;
* ) showHelp;; * ) showHelp;;
esac esac

View file

@ -59,7 +59,8 @@ updateScripts(){
} }
backup(){ backup(){
$SUDO ${scriptdir}/backup.sh $SUDO ${scriptdir}/backup.sh "${vpn}"
exit 0
} }
showHelp(){ showHelp(){