2009-09-14 03:10:20 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Pandora FMS Command line Backup Tool
|
|
|
|
# (c) Sancho Lerena <slerena@gmail.com>, Artica Soluciones Tecnologicas 2009
|
|
|
|
|
|
|
|
function help {
|
|
|
|
echo ""
|
|
|
|
echo -e "Pandora FMS Command line backup tool. http://www.pandorafms.org"
|
2015-12-01 12:16:52 +01:00
|
|
|
echo -e "(c) 2009-2015 Sancho Lerena <slerena@gmail.com>, Artica Soluciones Tecnologicas"
|
2009-09-14 03:10:20 +02:00
|
|
|
echo ""
|
|
|
|
echo -e "Syntax:"
|
|
|
|
echo -e "\t\t-c Path to Pandora FMS console, p.e: /srv/www/htdocs/pandora_console"
|
|
|
|
echo -e "\t\t-d Destination path for backup file. p.e: /tmp"
|
|
|
|
echo -e "\t\t-s Source filename for backup restore. p.e: /tmp/pandorafms"
|
|
|
|
echo -e "\t\t-f Restore also files"
|
|
|
|
echo -e "\t\t-q Quiet. No output message (used for scripts/cron)"
|
2009-09-15 18:38:00 +02:00
|
|
|
echo -e "\t\t-b No database backup/restore"
|
|
|
|
|
2009-09-14 03:10:20 +02:00
|
|
|
echo -e "\n\nPlease BE SURE TO USE RESTORE (-s) option. This will OVERWRITE ALL your"
|
2009-09-15 18:38:00 +02:00
|
|
|
echo -e "PandoraFMS install, including files, configuration and data. Please backup first!"
|
2009-09-14 03:10:20 +02:00
|
|
|
echo ""
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if [ $# -eq 0 ]
|
|
|
|
then
|
|
|
|
help
|
|
|
|
fi
|
|
|
|
|
|
|
|
SOURCEBACKUP="thisnotexist"
|
|
|
|
QUIET=0
|
|
|
|
RESTOREFILES=0
|
2009-09-15 18:38:00 +02:00
|
|
|
DATABASE=1
|
2009-09-14 03:10:20 +02:00
|
|
|
TIMESTAMP=`date +"%Y-%m-%d-%H-%M-%S"`
|
2020-05-19 11:52:11 +02:00
|
|
|
MDE=1
|
2009-09-14 03:10:20 +02:00
|
|
|
|
|
|
|
# Main parsing code
|
|
|
|
|
2009-09-15 18:38:00 +02:00
|
|
|
while getopts "bfhqc:d:s:" optname
|
2009-09-14 03:10:20 +02:00
|
|
|
do
|
|
|
|
case "$optname" in
|
|
|
|
"h")
|
|
|
|
help
|
|
|
|
;;
|
|
|
|
"c")
|
|
|
|
PANDORAPATH=$OPTARG
|
|
|
|
;;
|
|
|
|
"f")
|
|
|
|
RESTOREFILES=1
|
|
|
|
;;
|
2009-09-15 18:38:00 +02:00
|
|
|
"b")
|
|
|
|
DATABASE=0
|
|
|
|
;;
|
2009-09-14 03:10:20 +02:00
|
|
|
"d")
|
|
|
|
BACKUPDIR=$OPTARG
|
|
|
|
;;
|
|
|
|
"s")
|
|
|
|
SOURCEBACKUP=$OPTARG
|
|
|
|
;;
|
|
|
|
"q")
|
|
|
|
QUIET=1
|
|
|
|
;;
|
|
|
|
?)
|
|
|
|
help
|
|
|
|
;;
|
|
|
|
default)
|
|
|
|
help
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# Execution
|
|
|
|
|
2020-04-07 16:37:20 +02:00
|
|
|
which mysqldump &>/dev/null
|
2020-05-19 11:52:11 +02:00
|
|
|
[ $? -eq 0 ] || MDE=0
|
|
|
|
|
|
|
|
if [ $MDE == 0 ]
|
|
|
|
then
|
|
|
|
echo -e "Command Mysqldump not found. \nIn order to make backups it is necessary to have mysqldump on your console.\nAborting..."
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-04-07 16:37:20 +02:00
|
|
|
|
2009-09-14 03:10:20 +02:00
|
|
|
if [ ! -e "$PANDORAPATH/include/config.php" ]
|
|
|
|
then
|
|
|
|
echo "Cannot read config file at $PANDORAPATH/include/config.php. Aborting"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
DBUSER=`cat $PANDORAPATH/include/config.php | grep dbuser | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`
|
|
|
|
DBPASS=`cat $PANDORAPATH/include/config.php | grep dbpass | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`
|
|
|
|
DBHOST=`cat $PANDORAPATH/include/config.php | grep dbhost | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`
|
|
|
|
DBNAME=`cat $PANDORAPATH/include/config.php | grep dbname | grep -v "^\/" | grep -o "\=\"[a-zA-Z0-9]*\"" | grep -o "[A-Za-z0-9]*"`
|
|
|
|
|
|
|
|
|
|
|
|
cd /tmp
|
|
|
|
mkdir $TIMESTAMP
|
|
|
|
cd $TIMESTAMP
|
|
|
|
|
|
|
|
# Make the backup
|
|
|
|
|
|
|
|
if [ ! -e "$SOURCEBACKUP" ]
|
|
|
|
then
|
|
|
|
|
|
|
|
rm -Rf $BACKUPDIR/pandorafms_backup_$TIMESTAMP.tar.gz 2> /dev/null
|
|
|
|
|
2009-09-15 18:38:00 +02:00
|
|
|
if [ $DATABASE == 1 ]
|
|
|
|
then
|
2013-12-27 18:26:36 +01:00
|
|
|
mysqldump -u $DBUSER -p$DBPASS --max_allowed_packet=32M -h $DBHOST $DBNAME > pandorafms_backup_$TIMESTAMP.sql
|
2010-10-14 14:08:45 +02:00
|
|
|
tar cvzfh pandorafms_backup_$TIMESTAMP.tar.gz pandorafms_backup_$TIMESTAMP.sql $PANDORAPATH/* /etc/pandora /var/spool/pandora/data_in --exclude .data 2> /dev/null > /dev/null
|
2009-09-15 18:38:00 +02:00
|
|
|
else
|
2010-10-14 14:08:45 +02:00
|
|
|
tar cvzhf pandorafms_backup_$TIMESTAMP.tar.gz $PANDORAPATH/* /etc/pandora /var/spool/pandora/data_in --exclude .data 2> /dev/null > /dev/null
|
2009-09-15 18:38:00 +02:00
|
|
|
fi
|
2009-09-14 03:10:20 +02:00
|
|
|
|
|
|
|
mv /tmp/$TIMESTAMP/pandorafms_backup_$TIMESTAMP.tar.gz $BACKUPDIR
|
|
|
|
cd /tmp
|
|
|
|
rm -Rf /tmp/$TIMESTAMP
|
|
|
|
if [ $QUIET == 0 ]
|
|
|
|
then
|
|
|
|
echo "Backup completed and placed in $BACKUPDIR/pandorafms_backup_$TIMESTAMP.tar.gz"
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
# Make the backup restore process
|
|
|
|
|
|
|
|
echo "Detected Pandora FMS backup at $SOURCEBACKUP, please wait..."
|
|
|
|
tar xvzf $SOURCEBACKUP > /dev/null 2> /dev/null
|
|
|
|
|
2009-09-15 18:38:00 +02:00
|
|
|
if [ $DATABASE == 1 ]
|
|
|
|
then
|
|
|
|
echo "Dropping current database"
|
|
|
|
echo "drop database $DBNAME;" | mysql -u $DBUSER -p$DBPASS -h $DBHOST
|
2009-09-14 03:10:20 +02:00
|
|
|
|
2009-09-15 18:38:00 +02:00
|
|
|
echo "Restoring backup database"
|
|
|
|
echo "CREATE DATABASE $DBNAME;" | mysql -u $DBUSER -p$DBPASS -h $DBHOST
|
|
|
|
cat *.sql | mysql -u $DBUSER -p$DBPASS -h $DBHOST -D $DBNAME
|
|
|
|
fi
|
2009-09-14 03:10:20 +02:00
|
|
|
|
|
|
|
if [ $RESTOREFILES == 1 ]
|
|
|
|
then
|
|
|
|
echo "Restoring files and configuration"
|
2009-09-15 18:38:00 +02:00
|
|
|
cp -R var/spool/pandora/* /var/spool/pandora
|
|
|
|
cp -R etc/pandora/* /etc/pandora
|
|
|
|
BACKUPBASEPATH="`echo $PANDORAPATH | cut -c2-`"
|
|
|
|
cp -R $BACKUPBASEPATH/* $PANDORAPATH
|
|
|
|
|
2009-09-14 03:10:20 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
cd /tmp
|
|
|
|
rm -Rf /tmp/$TIMESTAMP
|
|
|
|
|
|
|
|
echo "Done. Backup in $SOURCEBACKUP restored"
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
exit 0
|