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.


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

# The working directory of the PBX

# The directory where backups are stored

# The temporary directory name for restoring a backup

# Number of days to store the backups

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

# Own name
THIS=$(basename $0)

# cron file name

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

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
d=$(date +'%Y%m%d%H%M')
echo "Generate backup $BCKDIR/backup$d.tgz"
for i in $EXCLUDE; do
excludes="$excludes --exclude $i"
tar cfz $BCKDIR/backup$d.tgz $excludes .

restore () {
# Stop the service

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

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

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

install () {
$FULL --purge $DAYS
$FULL --backup
exit 0
chmod 700 $CRONFILE
case $1 in
restore $2
purge $2