Como hacer Backup de Asterisk y FreePBX
Un sencillo script para realizar un backup de las configuraciones de
Asterisk, freePBX, recordings, sonidos y archivos TFTP para
configuraciones de los teléfonos, está pensado automatizar el proceso
utilizando cron jobs y enviar un informe por e-mail si disponemos de un
MTA configurado.
También nos permite definir el tamaño del histórico de configuraciones que queremos guardar (en la variable max_files), aquí queda el script:
#!/bin/bash
#
# Asterisk and freePBX Backup
# http://bytecoders.homelinux.com
# License GNU GPL v2
backkupfreePBX=yes
backuppartition=/dev/drbd0 # (e.g.: backuppartition=/dev/hda1)
astconfdir=/etc/asterisk/
recordingsdir=/var/spool/asterisk/
# Leave empty if yot do not have any "special" sound to backup
sounddir= # (e.g.: sounddir=/var/lib/asterisk/sounds/)
# Polycom and Grandstream phone boot files, etc.
phonebootfiles= # (e.g.: phonebootfiles=/tftpboot/)
# FreePBX Database connection information
cdrdbname=asteriskcdrdb # (e.g.: cdrdbname=asteriskcdrdb)
freepbxdbname=asterisk
dbhost=127.0.0.1
dbuser=asteriskuser # (e.g.: dbuser=asteriskuser)
dbpassword=amp109 # (e.g.: dbpassword=amp109)
# FreePBX Files
webrootdir=/var/www/html # (e.g.: webrootdir=/var/www/html)
# Admin email and subject, to send email log
subject="astbackup_V0.1"
email_adress="user@mail.com"
# Maximum number of backup files
max_files=10
#
# Variables
#
# Default TAR Output File Base Name
tarnamebase=asteriskbackup-
datestamp=`date +'%d-%m-%Y'`
# Execution directory (script start point)
startdir=`pwd`
# Temporary Directory
tempdir=tmpbckdir$datestamp
#
# Input Parameter Check
#
if test "$1" = ""
then
tarname=$tarnamebase$datestamp.tgz
else
tarname=$1
fi
#
# Banner
#
echo "" > $startdir/backup.log
echo "astbackup V0.1" >> $startdir/backup.log
prevdir=`pwd`
cd $startdir
#
# Show disk Space
#
backup_size=`du -h --max-depth=0 $startdir | cut -d/ -f1`
echo "" >> $startdir/backup.log
echo "Tamaño de los backups: $backup_size" >> $startdir/backup.log
echo "Espacio en disco:" >> $startdir/backup.log
df -h | head -1 >> $startdir/backup.log
df -h | grep $backuppartition >> $startdir/backup.log
#
# Check if we have rached the file quota
#
echo "" >> $startdir/backup.log
echo "Checking if we have reached quota:" >> $startdir/backup.log
total_files=`ls $startdir/sit* | wc -l`
if [ $total_files = $max_files ]; then
oldest_file=`ls $startdir/sit* | sort | head -1`
echo "Quota reached: limit $max_files files" >> $startdir/backup.log
echo "Deleting oldest file: $oldest_file" >> $startdir/backup.log
rm $oldest_file
else
echo "Not yet: $total_files backup files in disk" >> $startdir/backup.log
fi
#
# Create temporary working directory
#
echo " .. Setup" >> $startdir/backup.log
mkdir $tempdir
echo " done" >> $startdir/backup.log
#
# TAR freePBX files
#
echo " .. TARing FreePBX files in $webrootdir" >> $startdir/backup.log
cd $webrootdir
tar cf $startdir/$tempdir/freepbx.tar .
echo " done" >> $startdir/backup.log
# TAR all config files
#
if test "$astconfdir" = ""
then
echo "Please specify your Asterisk config directory: (in astconfdir var)" >> $startdir/backup.log
else
echo " .. TARing Asterisk config files in $astconfdir" >> $startdir/backup.log
cd $astconfdir
tar cf $startdir/$tempdir/astconfdir.tar .
echo " done" >> $startdir/backup.log
fi
if test "$recordingsdir" = ""
then
echo "Please specify your Asterisk recordings directory: (in recordingsdir var)" >> $startdir/backup.log
else
echo " .. TARing Asterisk recordings files in $recordingsdir" >> $startdir/backup.log
cd $recordingsdir
tar cf $startdir/$tempdir/recordingsdir.tar .
echo " done" >> $startdir/backup.log
fi
if test "$sounddir" = ""
then
echo "Skipping sounds directory: sounddir" >> $startdir/backup.log
else
echo " .. TARing Asterisk sounds files in $sounddir" >> $startdir/backup.log
cd $sounddir
tar cf $startdir/$tempdir/sounddir.tar .
echo " done" >> $startdir/backup.log
fi
if test "$phonebootfiles" = ""
then
echo "Skipping phone boot files directory: phonebootfiles" >> $startdir/backup.log
else
echo " .. TARing Asterisk phone boot files in $phonebootfiles" >> $startdir/backup.log
cd $phonebootfiles
tar cf $startdir/$tempdir/phonebootfiles.tar .
echo " done" >> $startdir/backup.log
fi
#
# sqldump database information
#
if test "$cdrdbname" = ""
then
echo " .. Skipping CDR database:" >> $startdir/backup.log
else
echo " .. sqldump'ing CDR database:" >> $startdir/backup.log
echo " user: $dbuser; database: $cdrdbname; host: $dbhost" >> $startdir/backup.log
cd $startdir/$tempdir
mysqldump -p --user=$dbuser --password=$dbpassword --host=$dbhost --add-drop-table $cdrdbname > $cdrdbname.sql
echo " done" >> $startdir/backup.log
fi
if test "$freepbxdbname" = ""
then
echo " .. Skipping FreePBX database:" >> $startdir/backup.log
else
echo " .. sqldump'ing FreePBX database:" >> $startdir/backup.log
echo " user: $dbuser; database: $freepbxdbname; host: $dbhost" >> $startdir/backup.log
cd $startdir/$tempdir
mysqldump -p --user=$dbuser --password=$dbpassword --host=$dbhost --add-drop-table $freepbxdbname > $freepbxdbname.sql
echo " done" >> $startdir/backup.log
fi
#
# Create final backup file
#
echo " .. Creating final compressed (tgz) TAR file: $tarname" >> $startdir/backup.log
tar czf $startdir/$tarname *
tar_size=`ls -l -h $startdir/$tarname | cut -d' ' -f5`
echo " Created file: $tarname Size=$tar_size" >> $startdir/backup.log
echo " done" >> $startdir/backup.log
#
# Change Permissions
#
chown asterisk:asterisk $startdir/*
#
# Cleanup
#
echo " .. Clean-up" >> $startdir/backup.log
cd $startdir
rm -r $tempdir
echo " done" >> $startdir/backup.log
#
# Exit banner
#
echo " .. Full site backup complete" >> $startdir/backup.log
echo "" >> $startdir/backup.log
cd $prevdir
#
# Send Report
#
mail -s $subject $email_adress < $startdir/backup.log
echo " .. Full site backup complete"
El script se ha probado en una Debian Etch y hace la labor, aunque
no hace falta decir que se aceptan recomendaciones y sugerencias. 