diff --git a/Dockerfile b/Dockerfile index 3b03aec..4215064 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,9 +12,12 @@ RUN apk add --no-cache wget tzdata bash bash-completion \ borgbackup RUN rm -rf /var/cache/apk/* +RUN mkdir -p /scripts + +COPY scripts/ /scripts COPY pfsense-backup.sh / -COPY borgBackup.sh / +COPY backup.sh / VOLUME ["/data"] CMD ["/pfsense-backup.sh"] diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..72a53a6 --- /dev/null +++ b/backup.sh @@ -0,0 +1,8 @@ +#!/bin/sh +source "/scripts/borgBackup.sh" +source "/scripts/functions.sh" + +url=${PFSENSE_SCHEME}://${PFSENSE_IP} +timestamp=$(date +%Y%m%d%H%M%S) + +run_backups diff --git a/pfsense-backup.sh b/pfsense-backup.sh index b74ee53..b95e1cd 100755 --- a/pfsense-backup.sh +++ b/pfsense-backup.sh @@ -1,124 +1,10 @@ #!/bin/sh -source "/borgBackup.sh" +source "/scripts/borgBackup.sh" +source "/scripts/functions.sh" ############################################################################################################################## -# Funktionen +# Main Execution ############################################################################################################################## -function sepurator { - echo "======================================================================================" -} - -function check_pfSense_vars_set() { - local errors=0 - - if [ -z "$PFSENSE_IP" ]; then echo "Must provide PFSENSE_IP" ; errors=$(($errors + 1)) ; fi - if [ -z "$PFSENSE_USER" ]; then echo "Must provide PFSENSE_USER" ; errors=$(($errors + 1)); fi - if [ -z "$PFSENSE_PASS" ]; then echo "Must provide PFSENSE_PASS" ; errors=$(($errors + 1)); fi - if [ -z "$PFSENSE_SCHEME" ]; then echo "Must provide PFSENSE_SCHEME" ; errors=$(($errors + 1)); fi - if [ -z "$BACKUPNAME" ]; then BACKUPNAME=$PFSENSE_IP; fi - - if [ $errors -ne 0 ]; then exit 1; fi -} - -function check_pfSense_optional_vars() { - if [ -z "$PFSENSE_CRON_SCHEDULE" ]; then cron=0 ; else cron=1 ; fi - if [ -z "$PFSENSE_BACK_UP_RRD_DATA" ]; then - getrrd="" - else - if [ "$PFSENSE_BACK_UP_RRD_DATA" == "0" ] ; then - getrrd="&donotbackuprrd=yes" - else - getrrd="" - fi - fi - if [ -z "$PFSENSE_BACKUP_DESTINATION_DIR" ]; then - destination="/data" - else - destination="$PFSENSE_BACKUP_DESTINATION_DIR" - fi -} - -function check_borg_backup_vars() { - local errors=0 - - if [ -z "$BORG_BACKUP_TRUE" ]; then - if [ "$BORG_REPO" ]; then echo "Musst provice BORG_REPO"; errors=$(($errors + 1)); fi - if [ "$BORG_CREATE_PARAMS" ]; then echo "Musst provice BORG_CREATE_PARAMS"; errors=$(($errors + 1)); fi - if [ "$BORG_PRUNE_PARAMS" ]; then echo "Musst provice BORG_PRUNE_PARAMS"; errors=$(($errors + 1)); fi - fi - - if [ $errors -ne 0 ]; then exit 1; fi -} - -function load_crontab_when_exists_or_create() { - if [ -f "$destination/crontab.txt" ]; then - echo "* Load Crontab $destination/crontab.txt" - crontab "$destination/crontab.txt" - else - echo "* Create $destination/crontab.txt" - echo "$PFSENSE_CRON_SCHEDULE FROM_CRON=1 /pfsense-backup.sh" >> "$destination/crontab.txt" - crontab "$destination/crontab.txt" - fi - sepurator - crond -f -} - -function do_backup() { - wget -qO- --keep-session-cookies --save-cookies cookies.txt \ - --no-check-certificate ${url}/diag_backup.php \ - | grep "name='__csrf_magic'" | sed 's/.*value="\(.*\)".*/\1/' > csrf.txt - - wget -qO- --keep-session-cookies --load-cookies cookies.txt \ - --save-cookies cookies.txt --no-check-certificate \ - --post-data "login=Login&usernamefld=${PFSENSE_USER}&passwordfld=${PFSENSE_PASS}&__csrf_magic=$(cat csrf.txt)" \ - ${url}/diag_backup.php | grep "name='__csrf_magic'" \ - | sed 's/.*value="\(.*\)".*/\1/' > csrf2.txt - - wget --keep-session-cookies --load-cookies cookies.txt --no-check-certificate \ - --post-data "download=download${getrrd}&__csrf_magic=$(head -n 1 csrf2.txt)" \ - ${url}/diag_backup.php -q -O ${destination}/config-${BACKUPNAME}-${timestamp}.xml - return_value=$? - if [ $return_value -eq 0 ]; then - echo "Backup saved as ${destination}/config-${BACKUPNAME}-${timestamp}.xml" - else - echo "Backup failed" - exit 1 - fi - - rm cookies.txt csrf.txt csrf2.txt -} - -function run_backups() { - echo "* Running backups" - do_backup - if [ ! -z "$BORG_BACKUP_TRUE" ]; then - create_borg_backup "$BACKUPNAME" "${destination}/config-${BACKUPNAME}-${timestamp}.xml" - purge_borg_backup "$BACKUPNAME" - fi - sepurator -} - -function cleanup_old_backups_when_set() { - if [ ! -z $keepfiles ]; then - remove=$(ls -d -1tr $destination/*.xml | tail -n +$keepfiles | head -n1) - if [ ! -z $remove ]; then - del=$(ls $destination/*.xml | head -n -$keepfiles) - if [ ! -z $del ]; then - rm -f $del - echo "Backup removed at $del" - fi - fi - fi -} - -function print_container_info { - sepurator - echo "* Backup Url: $url" - echo "* Backup Timestamp: $timestamp" - sepurator -} - -# main execution sepurator echo "Starting Docker Container..." sepurator diff --git a/borgBackup.sh b/scripts/borgBackup.sh similarity index 82% rename from borgBackup.sh rename to scripts/borgBackup.sh index fd5f3cb..33125c4 100644 --- a/borgBackup.sh +++ b/scripts/borgBackup.sh @@ -1,4 +1,6 @@ -#!/bin/sh +############################################################################################################################## +# Borg Backup Funktionen +############################################################################################################################## function create_borg_backup { # $1 = ARCHIVE NAME # $2 = FOLDER TO BACKUP diff --git a/scripts/functions.sh b/scripts/functions.sh new file mode 100644 index 0000000..a94fc19 --- /dev/null +++ b/scripts/functions.sh @@ -0,0 +1,118 @@ +############################################################################################################################## +# Funktionen +############################################################################################################################## +function sepurator { + echo "======================================================================================" +} + +function check_pfSense_vars_set() { + local errors=0 + + if [ -z "$PFSENSE_IP" ]; then echo "Must provide PFSENSE_IP" ; errors=$(($errors + 1)) ; fi + if [ -z "$PFSENSE_USER" ]; then echo "Must provide PFSENSE_USER" ; errors=$(($errors + 1)); fi + if [ -z "$PFSENSE_PASS" ]; then echo "Must provide PFSENSE_PASS" ; errors=$(($errors + 1)); fi + if [ -z "$PFSENSE_SCHEME" ]; then echo "Must provide PFSENSE_SCHEME" ; errors=$(($errors + 1)); fi + if [ -z "$BACKUPNAME" ]; then BACKUPNAME=$PFSENSE_IP; fi + + if [ $errors -ne 0 ]; then exit 1; fi +} + +function check_pfSense_optional_vars() { + if [ -z "$PFSENSE_CRON_SCHEDULE" ]; then cron=0 ; else cron=1 ; fi + if [ -z "$PFSENSE_BACK_UP_RRD_DATA" ]; then + getrrd="" + else + if [ "$PFSENSE_BACK_UP_RRD_DATA" == "0" ] ; then + getrrd="&donotbackuprrd=yes" + else + getrrd="" + fi + fi + if [ -z "$PFSENSE_BACKUP_DESTINATION_DIR" ]; then + destination="/data" + else + destination="$PFSENSE_BACKUP_DESTINATION_DIR" + fi +} + +function check_borg_backup_vars() { + local errors=0 + + if [ -z "$BORG_BACKUP_TRUE" ]; then + if [ "$BORG_REPO" ]; then echo "Musst provice BORG_REPO"; errors=$(($errors + 1)); fi + if [ "$BORG_CREATE_PARAMS" ]; then echo "Musst provice BORG_CREATE_PARAMS"; errors=$(($errors + 1)); fi + if [ "$BORG_PRUNE_PARAMS" ]; then echo "Musst provice BORG_PRUNE_PARAMS"; errors=$(($errors + 1)); fi + fi + + if [ $errors -ne 0 ]; then exit 1; fi +} + +function load_crontab_when_exists_or_create() { + if [ -f "$destination/crontab.txt" ]; then + echo "* Load Crontab $destination/crontab.txt" + crontab "$destination/crontab.txt" + else + echo "* Create $destination/crontab.txt" + echo "$PFSENSE_CRON_SCHEDULE FROM_CRON=1 /pfsense-backup.sh" >> "$destination/crontab.txt" + crontab "$destination/crontab.txt" + fi + sepurator + crond -f +} + +function do_backup() { + wget -qO- --keep-session-cookies --save-cookies cookies.txt \ + --no-check-certificate ${url}/diag_backup.php \ + | grep "name='__csrf_magic'" | sed 's/.*value="\(.*\)".*/\1/' > csrf.txt + + wget -qO- --keep-session-cookies --load-cookies cookies.txt \ + --save-cookies cookies.txt --no-check-certificate \ + --post-data "login=Login&usernamefld=${PFSENSE_USER}&passwordfld=${PFSENSE_PASS}&__csrf_magic=$(cat csrf.txt)" \ + ${url}/diag_backup.php | grep "name='__csrf_magic'" \ + | sed 's/.*value="\(.*\)".*/\1/' > csrf2.txt + + wget --keep-session-cookies --load-cookies cookies.txt --no-check-certificate \ + --post-data "download=download${getrrd}&__csrf_magic=$(head -n 1 csrf2.txt)" \ + ${url}/diag_backup.php -q -O ${destination}/config-${BACKUPNAME}-${timestamp}.xml + return_value=$? + if [ $return_value -eq 0 ]; then + echo "Backup saved as ${destination}/config-${BACKUPNAME}-${timestamp}.xml" + else + echo "Backup failed" + exit 1 + fi + + rm cookies.txt csrf.txt csrf2.txt +} + +function run_backups() { + echo "* Running backups" + do_backup + if [ ! -z "$BORG_BACKUP_TRUE" ]; then + create_borg_backup "$BACKUPNAME" "${destination}/config-${BACKUPNAME}-${timestamp}.xml" + purge_borg_backup "$BACKUPNAME" + fi + sepurator +} + +function cleanup_old_backups_when_set() { + if [ ! -z $keepfiles ]; then + remove=$(ls -d -1tr $destination/*.xml | tail -n +$keepfiles | head -n1) + if [ ! -z $remove ]; then + del=$(ls $destination/*.xml | head -n -$keepfiles) + if [ ! -z $del ]; then + rm -f $del + echo "Backup removed at $del" + fi + fi + fi +} + +function print_container_info { + sepurator + echo "* Backup - Name: $BACKUPNAME" + sepurator + echo "* pfSense - Url: $url" + echo "* pfSense - User: $PFSENSE_USER" + sepurator +}