Created
January 7, 2019 21:04
-
-
Save Alives/333f0ae1b7407ea44c3dd9a4ccd85901 to your computer and use it in GitHub Desktop.
pfsense backup script - Change constants at the top
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eo pipefail | |
PFSENSE_HOSTNAME=pfsense | |
DIR=/var/run/user/$(/usr/bin/id -u)/pfsense_backup | |
FILENAME=/root/backup/pfsense/config-pfsense-$(/bin/date +%Y%m%d_%H%M%S).xml | |
URL=https://pfsense.eyyit.com/diag_backup.php | |
CREDS=/root/creds/pfsense | |
LOGIN=$(/usr/bin/awk -F= '/login/ {print $NF}' $CREDS) | |
PASSWORD=$(/usr/bin/awk -F= '/password/ {print $NF}' $CREDS) | |
/usr/bin/fping -q $PFSENSE_HOSTNAME \ | |
| exit | |
test -d "$(dirname "$DIR")" \ | |
|| /bin/loginctl enable-linger "$(/usr/bin/id -un)" | |
test -d "$DIR" && /bin/rm -rf "$DIR" | |
/bin/mkdir "$DIR" | |
/usr/bin/wget -4qSO- \ | |
--keep-session-cookies \ | |
--save-cookies "${DIR}/cookies.txt" \ | |
$URL \ | |
2> "${DIR}/url_1_status" \ | |
| tee "${DIR}/url_1_content" \ | |
| grep "name='__csrf_magic'" \ | |
| sed 's/.*value="\(.*\)".*/\1/' \ | |
> "${DIR}/pfsense_csrf.txt" | |
/usr/bin/wget -4qSO- \ | |
--keep-session-cookies \ | |
--load-cookies "${DIR}/cookies.txt" \ | |
--save-cookies "${DIR}/cookies.txt" \ | |
--post-data \ | |
"login=Login&usernamefld=${LOGIN}&passwordfld=${PASSWORD}&__csrf_magic=$(cat "${DIR}/pfsense_csrf.txt")" \ | |
$URL \ | |
2> "${DIR}/url_2_status" \ | |
| tee "${DIR}/url_2_content" \ | |
| grep "name='__csrf_magic'" \ | |
| sed 's/.*value="\(.*\)".*/\1/' \ | |
> "${DIR}/pfsense_csrf2.txt" | |
/usr/bin/wget -4qSO "$FILENAME" \ | |
--keep-session-cookies \ | |
--load-cookies "${DIR}/cookies.txt" \ | |
--post-data \ | |
"download=download&donotbackuprrd=yes&__csrf_magic=$(head -n 1 "${DIR}/pfsense_csrf2.txt")" \ | |
$URL \ | |
> "${DIR}/url_3_content" \ | |
2> "${DIR}/url_3_status" | |
/usr/bin/wget -4qSO /dev/null \ | |
--keep-session-cookies \ | |
--load-cookies "${DIR}/cookies.txt" \ | |
--post-data \ | |
"download=download&donotbackuprrd=yes&__csrf_magic=$(head -n 1 "${DIR}/pfsense_csrf2.txt")" \ | |
'https://pfsense.eyyit.com/index.php?logout' \ | |
> "${DIR}/url_4_content" \ | |
2> "${DIR}/url_4_status" | |
/bin/grep -qi 'st0' "$FILENAME" || echo "Backup failed." | |
/bin/rm -rf "$DIR" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment