Skip to content

Commit

Permalink
Merge pull request #8327 from inverse-inc/feature/backup_as_export
Browse files Browse the repository at this point in the history
Add exportable backup for PF
  • Loading branch information
JeGoi authored Oct 24, 2024
2 parents 681d2b0 + f6bdcc2 commit 40ec83b
Show file tree
Hide file tree
Showing 15 changed files with 409 additions and 227 deletions.
182 changes: 109 additions & 73 deletions addons/backup-and-maintenance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,65 @@
# Licensed under the GPL
#

source /usr/local/pf/addons/functions/helpers.functions

#############################################################################
### Help
#############################################################################

help(){
cat <<-EOF
$0 Backup a PacketFence instance
Usage: $0 -f /path/to/backup_file.tgz [OPTION]...
Options:
-h,--help Display this help
--db Backup only database from PacketFence database
--conf Backup only configuration from PacketFence configuration
EOF
}

#############################################################################
### Handle args
#############################################################################
do_full_backup=1
do_db_backup=0
do_config_backup=0
do_replication=0
mariabackup_installed=false

# Parse option
TEMP=$(getopt -o f:h --long file:,help,db,conf \
-n "$0" -- "$@") || (echo "getopt failed." && exit 1)

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
case "$1" in
-h|--help)
help ; exit 0 ; shift
;;
--db)
do_db_backup=1 ; do_full_backup=0 ; shift
;;
--conf)
do_config_backup=1 ; do_full_backup=0 ; shift
;;
--)
shift ; break
;;
*)
echo "Wrong usage !" ; help ; exit 1
;;
esac
done

#############################################################################
### Variables
#############################################################################

NB_DAYS_TO_KEEP_DB=7
NB_DAYS_TO_KEEP_FILES=7
PF_DIRECTORY='/usr/local/pf/'
Expand All @@ -23,72 +82,62 @@ REP_USER=$($PF_DIRECTORY/bin/get_pf_conf active_active galera_replication_userna
REP_PWD=$($PF_DIRECTORY/bin/get_pf_conf active_active galera_replication_password)
BACKUP_DIRECTORY=${BACKUP_DIRECTORY:-/root/backup/}
BACKUP_DB_FILENAME='packetfence-db-dump'
BACKUP_PF_FILENAME='packetfence-files-dump'
BACKUP_CONF_FILENAME='packetfence-conf-dump'
ARCHIVE_DIRECTORY=$BACKUP_DIRECTORY
ARCHIVE_DB_FILENAME='packetfence-archive'
MARIABACKUP_INSTALLED=0
BACKUPRC=1

# For replication
ACTIVATE_REPLICATION=0
REPLICATION_USER=''
NODE1_HOSTNAME=''
NODE2_HOSTNAME=''
NODE1_IP=''
NODE2_IP=''

# to detect MariaDB remote DB
if [ "$DB_HOST" != "localhost" ] && [ "$DB_HOST" != "100.64.0.1" ]; then
MARIADB_REMOTE_CLUSTER=1
else
MARIADB_REMOTE_CLUSTER=0
fi

# Create the backup directory
if [ ! -d "$BACKUP_DIRECTORY" ]; then
mkdir -p $BACKUP_DIRECTORY
echo -e "$BACKUP_DIRECTORY , created. \n"
else
echo -e "$BACKUP_DIRECTORY , folder already created. \n"
fi

PF_USED_SPACE=`du -s $PF_DIRECTORY --exclude=logs --exclude=var | awk '{ print $1 }'`
BACKUPS_AVAILABLE_SPACE=`df --output=avail $BACKUP_DIRECTORY | awk 'NR == 2 { print $1 }'`

if (( $BACKUPS_AVAILABLE_SPACE > (( $PF_USED_SPACE / 2 )) )); then
# Backup complete PacketFence installation except logs
current_tgz=$BACKUP_DIRECTORY/$BACKUP_PF_FILENAME-`date +%F_%Hh%M`.tgz
if [ ! -f $BACKUP_DIRECTORY$BACKUP_PF_FILENAME ]; then
tar -czf $current_tgz --exclude='logs/*' --exclude='var/*' --exclude='.git/*' --exclude='conf/certmanager/*' --directory $PF_DIRECTORY .
BACKUPRC=$?
if (( $BACKUPRC > 0 )); then
echo "ERROR: PacketFence files backup was not successful" >&2
echo "ERROR: PacketFence files backup was not successful" > /usr/local/pf/var/backup_files.status
else
echo -e $BACKUP_PF_FILENAME "have been created in $BACKUP_DIRECTORY \n"
echo "OK" > /usr/local/pf/var/backup_files.status
find $BACKUP_DIRECTORY -name "packetfence-files-dump-*.tgz" -mtime +$NB_DAYS_TO_KEEP_FILES -print0 | xargs -0r rm -f
echo -e "$BACKUP_PF_FILENAME older than $NB_DAYS_TO_KEEP_FILES days have been removed. \n"
fi
die() {
echo "$(basename $0): $@" >&2 ; exit 1
}

create_backup_directory(){
# Create the backup directory
if [ ! -d "$BACKUP_DIRECTORY" ]; then
mkdir -p $BACKUP_DIRECTORY
echo -e "$BACKUP_DIRECTORY , created. \n"
else
echo -e $BACKUP_DIRECTORY$BACKUP_PF_FILENAME ", file already created. \n"
echo -e "$BACKUP_DIRECTORY , folder already created. \n"
fi
else
echo "ERROR: There is not enough space in $BACKUP_DIRECTORY to safely backup files. Skipping the backup." >&2
echo "ERROR: There is not enough space in $BACKUP_DIRECTORY to safely backup files. Skipping the backup." > /usr/local/pf/var/backup_files.status
fi
}

die() {
echo "$(basename $0): $@" >&2 ; exit 1
should_backup_config(){
PF_USED_SPACE=`du -s $PF_DIRECTORY --exclude=logs --exclude=var | awk '{ print $1 }'`
BACKUPS_AVAILABLE_SPACE=`df --output=avail $BACKUP_DIRECTORY | awk 'NR == 2 { print $1 }'`

if (( $BACKUPS_AVAILABLE_SPACE > (( $PF_USED_SPACE / 2 )) )); then
# Backup complete PacketFence installation except logs
current_tgz=$BACKUP_DIRECTORY/$BACKUP_CONF_FILENAME-`date +%F_%Hh%M`.tgz
if [ ! -f $BACKUP_DIRECTORY$BACKUP_CONF_FILENAME ]; then
tar -czf $current_tgz --exclude='logs/*' --exclude='var/*' --exclude='.git/*' --exclude='conf/certmanager/*' --directory $PF_DIRECTORY .
BACKUPRC=$?
if (( $BACKUPRC > 0 )); then
echo "ERROR: PacketFence files backup was not successful" >&2
echo "ERROR: PacketFence files backup was not successful" > /usr/local/pf/var/backup_files.status
else
echo -e $BACKUP_CONF_FILENAME "have been created in $BACKUP_DIRECTORY \n"
echo "OK" > /usr/local/pf/var/backup_files.status
find $BACKUP_DIRECTORY -name "packetfence-conf-dump-*.tgz" -mtime +$NB_DAYS_TO_KEEP_FILES -print0 | xargs -0r rm -f
echo -e "$BACKUP_CONF_FILENAME older than $NB_DAYS_TO_KEEP_FILES days have been removed. \n"
fi
else
echo -e $BACKUP_DIRECTORY$BACKUP_CONF_FILENAME ", file already created. \n"
fi
else
echo "ERROR: There is not enough space in $BACKUP_DIRECTORY to safely backup files. Skipping the backup." >&2
echo "ERROR: There is not enough space in $BACKUP_DIRECTORY to safely backup files. Skipping the backup." > /usr/local/pf/var/backup_files.status
fi
}

should_backup(){
should_backup_database(){
# Default choices
SHOULD_BACKUP=1
MARIADB_LOCAL_CLUSTER=0
MARIADB_DISABLE_GALERA=1

if [ $MARIADB_REMOTE_CLUSTER -eq 1 ]; then
# to detect MariaDB remote DB
if [ "$DB_HOST" != "localhost" ] && [ "$DB_HOST" != "100.64.0.1" ]; then
echo "Remote database detected: backup should be done on database server itself."
exit $BACKUPRC
fi
Expand All @@ -107,12 +156,17 @@ should_backup(){
else
echo -e "First server of the cluster : database backup will start.\n"
fi
else
fi
# Is the database running on the current server and should we be running a backup ?
if [ $SHOULD_BACKUP -eq 1 ] && { [ -f /var/run/mysqld/mysqld.pid ] || [ -f /var/run/mariadb/mariadb.pid ] || [ -f /var/lib/mysql/`hostname`.pid ]; }; then
echo "Database backup will start"
backup_database
else
echo "Nothing to do"
fi
}

backup_db(){
backup_database(){
# Check to see if Mariabackup is installed
if hash mariabackup 2>/dev/null; then
echo -e "Mariabackup is available. Will proceed using it for DB backup to avoid locking tables and easier recovery process. \n"
Expand Down Expand Up @@ -176,25 +230,7 @@ backup_db(){
fi
}

should_backup
# Is the database running on the current server and should we be running a backup ?
if [ $SHOULD_BACKUP -eq 1 ] && { [ -f /var/run/mysqld/mysqld.pid ] || [ -f /var/run/mariadb/mariadb.pid ] || [ -f /var/lib/mysql/`hostname`.pid ]; }; then
backup_db
else
echo "Nothing to do"
fi

# Replicate the db backups between both servers
if [ $ACTIVATE_REPLICATION == 1 ]; then
if [ $HOSTNAME == $NODE1_HOSTNAME ]; then
replicate_to=$NODE2_IP
elif [ $HOSTNAME == $NODE2_HOSTNAME ]; then
replicate_to=$NODE1_IP
else
echo "Cannot recognize hostname. This script is made for $NODE1_HOSTNAME and $NODE2_HOSTNAME. Exiting" >&2
exit 1
fi;
eval "rsync -auv -e ssh --delete --include '$BACKUP_DB_FILENAME*' --exclude='*' $BACKUP_DIRECTORY $REPLICATION_USER@$replicate_to:$BACKUP_DIRECTORY"
fi
should_backup_config
should_backup_database

exit $BACKUPRC
Loading

0 comments on commit 40ec83b

Please sign in to comment.