#!/bin/bash
#
# (c) -2007 Instituto Superior Técnico
#

#
# Dump some databases in preparation for an amdump run.
#
# Set it to run from Cron a bit before Amanda starts
# but do not forget to account for the time this script takes
# in your particular machine.
#
# Based on work by Miguel Cabeca <cabeca@ist.utl.pt>
#
# 2007/04/25 Claudio Martins <ctpm@ist.utl.pt> revision 921
# 2007       Jose Calhariz <jose.calhariz@tagus.ist.utl.pt>
#


#PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
source /etc/cal-scripts/db_dumps.conf

# Set this to what you wish to dump
DUMPS=${DUMPS:-"mysql postgresql deb sfdisk"}
DUMP_DIR=${DUMP_DIR:-/var/dumps}

MYSQLDUMP=${MYSQLDUMP:-mysqldump}
PGDUMP=${PGDUMP:-pg_dumpall}
SLAPCAT=${SLAPCAT:-slapcat}
RPM=${RPM:-rpm}
DPKG=${DPKG:-dpkg}
SFDISK=${SFDISK:-sfdisk}


# All files created should only be readable by us
umask 0077 # umask  u=rwx,o=,g=


##########################################################
# Function to dump Mysql databases
#
#  --opt		# This option is shorthand; it is the same as specifying --add-drop-table --add-locks 
			# --create-options --disable-keys --extended-insert --lock-tables --quick --set-charset. 
			# It should give you a fast dump operation and produce a dump file that can be reloaded 
			# into a MySQL server quickly.
#  --all-databases 	# Dump all the databases. This will be same as --databases with all databases selected. 
#  --all 		# Include all MySQL-specific create options.
#  --flush-logs 	# Flush log file in the MySQL server before starting the dump.
#  --force 		# Continue even if we get an SQL error during a table dump.
#

mysql_dump()
{
  $MYSQLDUMP --opt --all-databases --all  --flush-logs --force  > ${DUMP_DIR}/mysql.dump 2> ${DUMP_DIR}/mysql.dump.log
}


##########################################################
# Function to dump Postgresql databases

postgresql_dump()
{
  su -c "$PGDUMP" postgres > ${DUMP_DIR}/postgresql.dump 2>${DUMP_DIR}/postgresql.dump.log
}


##########################################################
# Function to dump ldap databases
#
#  -c			# enable continue (ignore errors) mode.
#  -l			# Write LDIF to specified file instead of standard output.

ldap_dump()
{
  $SLAPCAT -c -l ${DUMP_DIR}/ldap.dump >${DUMP_DIR}/ldap.dump.log 2>&1
}


##########################################################
# Function to dump rpm packages installed
#

rpm_dump()
{ 
  $RPM -qa > ${DUMP_DIR}/rpm-qa.dump 2>${DUMP_DIR}/rpm-qa.dump.log
}

##########################################################
# Function to dump debian packages installed
#

deb_dump()
{ 
  $DPKG -l > ${DUMP_DIR}/dpkg-l.dump 2>${DUMP_DIR}/dpkg-l.dump.log
  $DPKG --get-selections > ${DUMP_DIR}/dpkg--get-selections.dump 2>${DUMP_DIR}/dpkg--get-selections.dump.log
}

##########################################################
# Function to dump partition tables
#

sfdisk_dump()
{ 
  $SFDISK -d > ${DUMP_DIR}/sfdisk-d.dump 2>${DUMP_DIR}/sfdisk-d.dump.log
}


# Call dump routines
mkdir -p ${DUMP_DIR} 2>/dev/null
for i in $DUMPS
do 
	${i}_dump || echo "$0: ${i}_dump returned a non-zero exit code. Please check the logs in $DUMP_DIR." 1>&2
done 

