diff --git a/scripts/backup.sh b/scripts/backup.sh index a632a81..65f6f15 100755 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -1,7 +1,5 @@ #!/bin/bash - -backupdir=pivpnbackup -date=$(date +%Y%m%d-%H%M%S) +# PiVPN: Backup Script # 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) @@ -15,23 +13,45 @@ c=$(( columns / 2 )) r=$(( r < 20 ? 20 : r )) 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) - VPNChooseOptions=(WireGuard "" on - OpenVPN "" off) +backupdir=pivpnbackup +date=$(date +%Y%m%d-%H%M%S) +setupVarsFile="setupVars.conf" +setupConfigDir="/etc/pivpn" - if VPN=$("${chooseVPNCmd[@]}" "${VPNChooseOptions[@]}" 2>&1 >/dev/tty) ; then - echo "::: Using VPN: $VPN" - VPN="${VPN,,}" - else - echo "::: Cancel selected, exiting...." - exit 1 - fi +if [ -r "${setupConfigDir}/wireguard/${setupVarsFile}" ] && [ -r "${setupConfigDir}/openvpn/${setupVarsFile}" ]; then -setupVars="/etc/pivpn/${VPN}/setupVars.conf" + # 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 + OpenVPN "" off) + + if VPN=$("${chooseVPNCmd[@]}" "${VPNChooseOptions[@]}" 2>&1 >/dev/tty) ; then + echo "::: Backing up VPN: $VPN" + VPN="${VPN,,}" + else + echo "::: Cancel selected, exiting...." + exit 1 + fi + fi + + 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 - echo "::: Missing setup vars file!" - exit 1 + echo "::: Missing setup vars file!" + exit 1 fi # shellcheck disable=SC1090 @@ -53,7 +73,7 @@ backup_openvpn(){ backupzip=$date-pivpnovpnbackup.tgz # shellcheck disable=SC2210 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 backupzip=$date-pivpnwgbackup.tgz 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" } diff --git a/scripts/openvpn/pivpn.sh b/scripts/openvpn/pivpn.sh index 58fdca1..0b7feb7 100755 --- a/scripts/openvpn/pivpn.sh +++ b/scripts/openvpn/pivpn.sh @@ -58,7 +58,7 @@ function update { } function backup { - $SUDO ${scriptDir}/backup.sh + $SUDO ${scriptDir}/backup.sh "${vpn}" exit 0 } diff --git a/scripts/pivpn b/scripts/pivpn index c43ece5..f63c285 100755 --- a/scripts/pivpn +++ b/scripts/pivpn @@ -17,6 +17,11 @@ uninstallServer(){ exit 0 } +backup(){ + $SUDO ${scriptDir}/backup.sh + exit 0 +} + showHelp(){ echo "::: To pass off to the pivpn command for each protocol" echo ":::" @@ -25,6 +30,7 @@ showHelp(){ echo ":::" echo "::: -h, help Show this help dialog" echo "::: -u, uninstall Uninstall pivpn from your system!" + echo "::: -bk, backup Backup VPN configs and user profiles" exit 0 } @@ -38,5 +44,6 @@ case "$1" in ovpn ) "${scriptDir}/openvpn/pivpn.sh" "${@:2}";; "-h" | "help" ) showHelp;; "-u" | "uninstall" ) uninstallServer;; +"-bk" | "backup" ) backup ;; * ) showHelp;; esac diff --git a/scripts/wireguard/pivpn.sh b/scripts/wireguard/pivpn.sh index e000f25..61db6d9 100755 --- a/scripts/wireguard/pivpn.sh +++ b/scripts/wireguard/pivpn.sh @@ -59,7 +59,8 @@ updateScripts(){ } backup(){ - $SUDO ${scriptdir}/backup.sh + $SUDO ${scriptdir}/backup.sh "${vpn}" + exit 0 } showHelp(){