Backup Asterisk Configs
Backup Asterisk Configs
Backs up all critical Asterisk data: configuration files, AstDB, voicemail messages, custom recordings, and TLS keys. Keeps N days of local backups with automatic rotation.
Usage
# Manual run
sudo ./backup-asterisk.sh
# Cron (daily at 2 AM)
# 0 2 * * * /usr/local/bin/backup-asterisk.sh >> /var/log/asterisk-backup.log 2>&1
Script
#!/bin/bash
set -euo pipefail
# Configuration
BACKUP_DIR="/var/backups/asterisk"
KEEP_DAYS=14
DATE=$(date +%Y%m%d-%H%M%S)
BACKUP_NAME="asterisk-${DATE}"
# What to back up
CONF_DIR="/etc/asterisk"
SPOOL_DIR="/var/spool/asterisk"
SOUNDS_CUSTOM="/var/lib/asterisk/sounds/custom"
KEYS_DIR="/etc/asterisk/keys"
ASTDB="/var/lib/asterisk/astdb.sqlite3"
# Create backup directory
mkdir -p "${BACKUP_DIR}/${BACKUP_NAME}"
echo "[$(date)] Starting Asterisk backup: ${BACKUP_NAME}"
# 1. Configuration files
echo " Backing up configs..."
tar czf "${BACKUP_DIR}/${BACKUP_NAME}/configs.tar.gz" -C / "${CONF_DIR#/}" 2>>"${BACKUP_DIR}/${BACKUP_NAME}/backup.log"
# 2. AstDB (SQLite database -- requires sqlite3)
echo " Backing up AstDB..."
if [ -f "${ASTDB}" ]; then
# Use sqlite3 .backup for a consistent snapshot (safe while Asterisk is running)
if command -v sqlite3 >/dev/null 2>&1; then
sqlite3 "${ASTDB}" ".backup '${BACKUP_DIR}/${BACKUP_NAME}/astdb.sqlite3'"
else
echo " Warning: AstDB not found at ${ASTDB}"
fi
# 3. Voicemail spool
echo " Backing up voicemail..."
if [ -d "${SPOOL_DIR}/voicemail" ]; then
tar czf "${BACKUP_DIR}/${BACKUP_NAME}/voicemail.tar.gz" -C / "${SPOOL_DIR#/}/voicemail" 2>>"${BACKUP_DIR}/${BACKUP_NAME}/backup.log"
fi
# 4. Custom sound files
echo " Backing up custom sounds..."
if [ -d "${SOUNDS_CUSTOM}" ]; then
tar czf "${BACKUP_DIR}/${BACKUP_NAME}/sounds-custom.tar.gz" -C / "${SOUNDS_CUSTOM#/}" 2>>"${BACKUP_DIR}/${BACKUP_NAME}/backup.log"
fi
# 5. TLS keys/certificates
echo " Backing up TLS keys..."
if [ -d "${KEYS_DIR}" ]; then
tar czf "${BACKUP_DIR}/${BACKUP_NAME}/keys.tar.gz" -C / "${KEYS_DIR#/}" 2>>"${BACKUP_DIR}/${BACKUP_NAME}/backup.log"
fi
# Create a single archive of everything
echo " Creating combined archive..."
tar czf "${BACKUP_DIR}/${BACKUP_NAME}.tar.gz" -C "${BACKUP_DIR}" "${BACKUP_NAME}"
rm -rf "${BACKUP_DIR}/${BACKUP_NAME}"
# Rotate old backups
echo " Rotating backups older than ${KEEP_DAYS} days..."
find "${BACKUP_DIR}" -name "asterisk-*.tar.gz" -mtime +${KEEP_DAYS} -delete
FINAL_SIZE=$(du -sh "${BACKUP_DIR}/${BACKUP_NAME}.tar.gz" | cut -f1)
echo "[$(date)] Backup complete: ${BACKUP_NAME}.tar.gz (${FINAL_SIZE})"
How it works
- AstDB backup: Uses
sqlite3 .backupinstead of copying the file. This creates a consistent snapshot even while Asterisk is running and holding locks on the database. - Tar with
-C /: The-C /flag combined with stripping the leading/from paths ensures the archive preserves the full directory structure without absolute path warnings. - Rotation:
find -mtime +N -deleteremoves archives older than N days. AdjustKEEP_DAYSbased on your storage budget. - set -euo pipefail: Makes the script fail-fast on any error (
-e), treat unset variables as errors (-u), and propagate pipeline failures (-o pipefail).
Tips
- Add remote sync at the end:
rsync -az "${BACKUP_DIR}/${BACKUP_NAME}.tar.gz" user@remote:/backups/oraws s3 cpfor S3. - To restore: extract the archive, then
cp -aconfigs back and runasterisk -rx "core reload". - For AstDB restore: stop Asterisk, replace
astdb.sqlite3, restart Asterisk. - Monitor backup success by checking the cron log or adding a heartbeat ping (e.g., healthchecks.io).
User Notes
No notes yet. Be the first to contribute a tip or example.
Contribute a note
Share a tip, gotcha, or practical example. Keep it under 2000 characters. No questions (use the Asterisk community forums for support). Wrap code in backticks.