Zum Hauptinhalt springen

Backup Script

Backup Script

Having a backup is very useful in cases when the PBX directory needs to be restored. This can be a hardware failure or a accidential deletion of the PBX configuration. It is highly recommended to automatically replicate the backup directory with some external data storage, so that in the case of hardware failure that backup is stil available.

Many hosting companies offer taking automatic snapshots on daily or weekly basis. This can be a easy and convenient way to make sure that a previous state of the PBX can be easily restored and it is highly recommended to take advantage of such a servicwe.

The following script can be used to generate automatic backup every day from the file system. This can be combined with the snapshot method; especially when adding a line that copies the backup to a server that resides outside of the data center, this can help adding an extra layer of security, e.g. in case the data center becomes unavailable.

There are three directories of interest.

  • The PBXDIR directory is the working directory of the PBX. If the script is in the same location like the PBX the value aill be automatically the right directory.
  • The BCKDIR is the place where the script will write backups. If possible, this directory should be automatically syncronized with some outside data storage.
  • The OLDDIR will be used as a temporary place in case that the script is used to restore a backup.

By default, the cron job will purge backups that are older than 3 days. This can be changed according to needs.

In order to avoid excess backup, there is a lost of directories that will not be backed up. By default these are the audio directories. Other candidates would be pcap directories or audio recordings.

To install the script use backup.sh --install. This will generate an entry into the /etc/cron.daily directory, assuming that there should be one backup per day.

To restore a backup, use the backup.sh --restore filename function. This will take care about stopping the service, moving the excluded directories and starting the service after restoring.

You can at any time trigger a backup with backup.sh --backup. You can also explictly call the purse function with backup.sh --purge days.

#!/bin/bash

#
# Generate backups for the Vodia PBX
#
# Copyright (C) 2024 Vodia Networks, Inc.
# Email <info@vodia.com>
#

# The working directory of the PBX
PBXDIR=$(pwd)

# The directory where backups are stored
BCKDIR=$PBXDIR/../backup

# The temporary directory name for restoring a backup
OLDDIR=$PBXDIR/../pbx-old

# Number of days to store the backups
DAYS=3

# Exclude directories
EXCLUDE="audio_*"
# EXCLUDE="audio_* recordings pcap"

# Own name
THIS=$(basename $0)

# cron file name
CRONFILE=/etc/cron.daily/pbx

# How to start and stop the PBX:
if command -v systemctl >/dev/null; then
START="systemctl pbx start"
STOP="systemctl pbx stop"
else
START="/etc/init.d/pbx start"
STOP="/etc/init.d/pbx stop"
fi

usage () {
echo "Usage:"
echo "$THIS --restore <file>: Restore a backup"
echo "$THIS --purge <days>: Delete outdated backups"
echo "$THIS --backup: Create a backup"
echo "$THIS --install: Install cron job"
exit 1
}

backup () {
mkdir -p $BCKDIR
cd $PBXDIR
d=$(date +'%Y%m%d%H%M')
echo "Generate backup $BCKDIR/backup$d.tgz"
excludes=""
for i in $EXCLUDE; do
excludes="$excludes --exclude $i"
done
tar cfz $BCKDIR/backup$d.tgz $excludes .
}

restore () {
# Stop the service
$STOP

# Move the old directory out of the way
rm -Rf $OLDDIR
mv $PBXDIR $OLDDIR
mkdir $PBXDIR
cd $PBXDIR
for i in $EXCLUDE; do
mv -R $OLDDIR/$i .
done

// Restore the directory
tar xfz $1
// Restart the service
$START
}

purge () {
days=$1
if [ "$days" -gt 0 ]; then
find $BCKDIR -type f -mtime "+$days" -name '*.tgz' -execdir rm -- '{}' \;
else
usage
fi
}

install () {
FULL=$(pwd)/$THIS
cat >$CRONFILE <<EOF
#!/bin/sh
PATH=$PATH
cd $PBXDIR
$FULL --purge $DAYS
$FULL --backup
exit 0
EOF
chmod 700 $CRONFILE
}
case $1 in
--restore)
restore $2
;;
--purge)
purge $2
;;
--backup)
backup
;;
--install)
install
;;
*)
usage
;;
esac