add borg backup to pfsense-backup script

This commit is contained in:
2022-09-27 14:11:30 +02:00
parent a466dfdd58
commit 87eb4c1cb0
4 changed files with 133 additions and 115 deletions
+4 -2
View File
@@ -8,11 +8,13 @@ ENV BACKUPNAME=router
# Install packages # Install packages
RUN apk update ; apk upgrade RUN apk update ; apk upgrade
RUN apk add --no-cache wget tzdata bash RUN apk add --no-cache wget tzdata bash bash-completion \
borgbackup
RUN rm -rf /var/cache/apk/* RUN rm -rf /var/cache/apk/*
COPY pfsense-backup.sh / COPY pfsense-backup.sh /
COPY backup.sh / COPY borgBackup.sh /
VOLUME ["/data"] VOLUME ["/data"]
CMD ["/pfsense-backup.sh"] CMD ["/pfsense-backup.sh"]
-72
View File
@@ -1,72 +0,0 @@
#!/bin/sh
# function definition
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
}
# main execution
# check for required parameters
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
# check for optional parameters
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
# set up variables
url=${PFSENSE_SCHEME}://${PFSENSE_IP}
timestamp=$(date +%Y%m%d%H%M%S)
do_backup
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
+53
View File
@@ -0,0 +1,53 @@
#!/bin/sh
function create_borg_backup {
# $1 = ARCHIVE NAME
# $2 = FOLDER TO BACKUP
local ARCHIVE_NAME="$1"
local FOLDER_TO_BACKUP="$2"
if [ -z "$ARCHIVE_NAME" ]; then
echo "No archive name provided"
return 1
fi
if [ -z "$FOLDER_TO_BACKUP" ]; then
echo "No folder to backup provided"
return 1
fi
if [ -z "$BORG_CREATE_PARAMS" ]; then
echo "No borg create params provided"
return 1
fi
wait_for_borg_backup_done
echo "Creating Borg Backup from $FOLDER_TO_BACKUP into the Archive: $ARCHIVE_NAME"
borg create "${BORG_CREATE_PARAMS[@]}" "$BORG_REPO"::"$ARCHIVE_NAME-{now:%d.%m.%Y_%H:%M}" "$FOLDER_TO_BACKUP"
}
function purge_borg_backup {
# $1 = ARCHIVE NAME
local ARCHIVE_NAME="$1"
if [ -z "$ARCHIVE_NAME" ]; then
echo "No archive name provided"
return 1
fi
if [ -z "$BORG_PRUNE_PARAMS" ]; then
echo "No borg purge params provided"
return 1
fi
wait_for_borg_backup_done
echo "Purging Borg Backup Archive: $ARCHIVE_NAME"
borg prune -a "$ARCHIVE_NAME-*" "${BORG_PRUNE_PARAMS[@]}"
}
function wait_for_borg_backup_done {
local text_output=1
while pidof -x borg >/dev/null; do
if [ $text_output -eq 1 ]; then
echo "Borg already running, waiting that it finishes..."
text_output=0
fi
sleep 10
done
}
+76 -41
View File
@@ -1,8 +1,52 @@
#!/bin/sh #!/bin/sh
source "/borgBackup.sh"
# function definition # function definition
function do_backup() function check_pfSense_vars_set() {
{ 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
}
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() {
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
}
function load_crontab_when_exists_or_create() {
if [ -f "$destination/crontab.txt" ]; then
crontab "$destination/crontab.txt"
else
echo "$PFSENSE_CRON_SCHEDULE FROM_CRON=1 /pfsense-backup.sh" >> "$destination/crontab.txt"
crontab "$destination/crontab.txt"
fi
crond -f
}
function do_backup() {
wget -qO- --keep-session-cookies --save-cookies cookies.txt \ wget -qO- --keep-session-cookies --save-cookies cookies.txt \
--no-check-certificate ${url}/diag_backup.php \ --no-check-certificate ${url}/diag_backup.php \
| grep "name='__csrf_magic'" | sed 's/.*value="\(.*\)".*/\1/' > csrf.txt | grep "name='__csrf_magic'" | sed 's/.*value="\(.*\)".*/\1/' > csrf.txt
@@ -27,32 +71,38 @@ function do_backup()
rm cookies.txt csrf.txt csrf2.txt rm cookies.txt csrf.txt csrf2.txt
} }
function run_backups() {
do_backup
if [ ! -z "$BORG_BACKUP_TRUE" ]; then
create_borg_backup "$BACKUPNAME" "${destination}/config-${BACKUPNAME}-${timestamp}.xml"
purge_borg_backup "$BACKUPNAME"
fi
}
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
}
# main execution # main execution
# check for required parameters # check for required parameters
errors=0 errors=0
if [ -z "$PFSENSE_IP" ]; then echo "Must provide PFSENSE_IP" ; errors=$(($errors + 1)) ; fi check_pfSense_vars_set
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 if [ $errors -ne 0 ]; then exit 1; fi
# check for optional parameters # check for optional parameters
if [ -z "$PFSENSE_CRON_SCHEDULE" ]; then cron=0 ; else cron=1 ; fi check_pfSense_optional_vars
if [ -z "$PFSENSE_BACK_UP_RRD_DATA" ]; then
getrrd="" # borg backups vars set
else check_borg_backup_vars
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
# set up variables # set up variables
url=${PFSENSE_SCHEME}://${PFSENSE_IP} url=${PFSENSE_SCHEME}://${PFSENSE_IP}
@@ -60,26 +110,11 @@ timestamp=$(date +%Y%m%d%H%M%S)
if [ $cron -eq 1 ]; then if [ $cron -eq 1 ]; then
if [ -z "$FROM_CRON" ]; then if [ -z "$FROM_CRON" ]; then
if [ -f $destination/crontab.txt ]; then load_crontab_when_exists_or_create
crontab $destination/crontab.txt
else
echo "$PFSENSE_CRON_SCHEDULE FROM_CRON=1 /pfsense-backup.sh" >> $destination/crontab.txt
crontab $destination/crontab.txt
fi
crond -f
else else
do_backup run_backups
if [ ! -z $keepfiles ]; then cleanup_old_backups_when_set
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
fi fi
else else
do_backup run_backups
fi fi