vicigeeksimple guides
Browse

Platform maintenance

ViciBox backup and restore

Older ViciBox backup and restore notes of unverified provenance; check the version and test recovery separately.

vicibox-backup-restore-guide.md567 linesDocument text unchanged

Reference text

ViciBox backup and restore

This is an older ViciBox note of unverified provenance. Its commands and package information may be outdated. Use your browser's Find command to locate a topic, then verify the procedure against your installed version before making changes.
ViciBox backup and restore source document
# VICIdial/ViciBox Complete Backup & Restore Guide

## Overview

This guide covers backing up a VICIdial system and restoring it to a new server without breaking anything.

---

## What Needs to be Backed Up

### Critical Components

| Component | Location | Size (typical) | Priority |
|-----------|----------|----------------|----------|
| MySQL Database | `asterisk` database | 50MB - 50GB | **CRITICAL** |
| Asterisk Config | `/etc/asterisk/` | ~5MB | **CRITICAL** |
| AGI Scripts | `/var/lib/asterisk/agi-bin/` | ~5MB | **CRITICAL** |
| Web Files | `/srv/www/htdocs/` (ViciBox) | ~50MB | **CRITICAL** |
| astguiclient Config | `/etc/astguiclient.conf` | 1KB | **CRITICAL** |
| Perl Scripts | `/usr/share/astguiclient/` | ~5MB | HIGH |
| Sound Files | `/var/lib/asterisk/sounds/` | 50MB-500MB | HIGH |
| Recordings | `/var/spool/asterisk/monitor/` | 1GB-1TB+ | MEDIUM |
| Voicemail | `/var/spool/asterisk/voicemail/` | Variable | MEDIUM |
| Crontab | System crontab | 1KB | HIGH |
| SSL Certificates | `/etc/apache2/ssl/` or `/etc/pki/` | 10KB | HIGH |

---

## Method 1: Using ADMIN_backup.pl (Recommended)

### Full Backup Command

```bash
# Full backup (database + all files)
/usr/share/astguiclient/ADMIN_backup.pl --archive_path=/backup

# Backup with FTP transfer
/usr/share/astguiclient/ADMIN_backup.pl --ftp-transfer

# Database only (fastest)
/usr/share/astguiclient/ADMIN_backup.pl --db-only --archive_path=/backup

# Database without large log tables
/usr/share/astguiclient/ADMIN_backup.pl --db-without-logs --archive_path=/backup

# Settings only (no leads/logs) - good for config migration
/usr/share/astguiclient/ADMIN_backup.pl --db-settings-only --archive_path=/backup
```

### Backup Script Options

```
--db-only                 Only backup database
--db-settings-only        Database without leads, logs, servers, phones
--db-without-logs         Skip log tables (much smaller backup)
--db-without-archives     Skip archive tables
--conf-only               Only backup Asterisk conf files
--without-db              Skip database
--without-conf            Skip conf files
--without-web             Skip web files
--without-sounds          Skip sound files
--without-voicemail       Skip voicemail
--without-crontab         Skip crontab
--ftp-transfer            Send to FTP server
--archive_path=/path      Custom backup location
--db_raw_files_copy       Copy raw MySQL files (stops MySQL!)
```

---

## Method 2: Manual Backup (Step-by-Step)

### Step 1: Create Backup Directory

```bash
BACKUP_DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/backup/vici_backup_$BACKUP_DATE"
mkdir -p $BACKUP_DIR
cd $BACKUP_DIR
```

### Step 2: Backup Database

```bash
# Full database backup
mysqldump -u root -p asterisk > asterisk_full.sql

# Backup with routines and triggers
mysqldump -u root -p --routines --triggers asterisk > asterisk_full.sql

# Compressed backup (recommended for large DBs)
mysqldump -u root -p asterisk | gzip > asterisk_full.sql.gz

# Backup without log tables (much smaller)
# First, get list of log tables to exclude
mysql -u root -p -N -e "SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema='asterisk' AND TABLE_NAME LIKE '%_log'" > log_tables.txt

# Create ignore string
IGNORE_TABLES=$(cat log_tables.txt | xargs -I {} echo "--ignore-table=asterisk.{}" | tr '\n' ' ')

# Backup without logs
mysqldump -u root -p $IGNORE_TABLES asterisk | gzip > asterisk_nologs.sql.gz

# Backup ALL databases (if multiple)
mysqldump -u root -p --all-databases > all_databases.sql
```

### Step 3: Backup Asterisk Configuration

```bash
# Asterisk configs
tar czvf asterisk_conf.tar.gz /etc/asterisk/

# DAHDI/Zaptel config (if hardware cards)
tar czvf dahdi_conf.tar.gz /etc/dahdi.conf /etc/modprobe.d/dahdi* 2>/dev/null
```

### Step 4: Backup AGI Scripts

```bash
tar czvf agi_scripts.tar.gz /var/lib/asterisk/agi-bin/
```

### Step 5: Backup Web Files

```bash
# ViciBox 10+
tar czvf web_files.tar.gz /srv/www/htdocs/

# Older ViciBox / CentOS
tar czvf web_files.tar.gz /var/www/html/
```

### Step 6: Backup Perl Scripts

```bash
tar czvf astguiclient.tar.gz /usr/share/astguiclient/
```

### Step 7: Backup astguiclient.conf

```bash
cp /etc/astguiclient.conf ./astguiclient.conf
```

### Step 8: Backup Sound Files

```bash
tar czvf sounds.tar.gz /var/lib/asterisk/sounds/
```

### Step 9: Backup Recordings (Optional - Large)

```bash
# Check size first
du -sh /var/spool/asterisk/monitor/

# Backup recordings
tar czvf recordings.tar.gz /var/spool/asterisk/monitor/

# Or use rsync for large recording archives
rsync -avz /var/spool/asterisk/monitor/ $BACKUP_DIR/recordings/
```

### Step 10: Backup Voicemail

```bash
tar czvf voicemail.tar.gz /var/spool/asterisk/voicemail/
```

### Step 11: Backup Crontab

```bash
crontab -l > crontab_backup.txt
cp /etc/crontab system_crontab.txt
```

### Step 12: Backup SSL Certificates

```bash
# Apache SSL certs
tar czvf ssl_certs.tar.gz /etc/apache2/ssl/ 2>/dev/null || \
tar czvf ssl_certs.tar.gz /etc/pki/tls/ 2>/dev/null
```

### Step 13: Create Final Archive

```bash
cd /backup
tar czvf vici_complete_backup_$BACKUP_DATE.tar.gz vici_backup_$BACKUP_DATE/
```

---

## Restore to New Server

### Prerequisites on New Server

1. Fresh ViciBox installation (same or newer version)
2. Network configured with new IP
3. Root access

### Step 1: Transfer Backup to New Server

```bash
# From old server
scp /backup/vici_complete_backup_*.tar.gz root@NEW_SERVER_IP:/backup/

# Or use rsync
rsync -avz --progress /backup/vici_complete_backup_*.tar.gz root@NEW_SERVER_IP:/backup/
```

### Step 2: Extract Backup

```bash
# On new server
cd /backup
tar xzvf vici_complete_backup_*.tar.gz
cd vici_backup_*/
```

### Step 3: Stop VICIdial Services

```bash
# Stop keepalive and processes
/usr/share/astguiclient/ADMIN_keepalive_ALL.pl --stop

# Stop Asterisk
systemctl stop asterisk

# Stop Apache
systemctl stop apache2   # ViciBox
systemctl stop httpd     # CentOS
```

### Step 4: Restore Database

```bash
# Drop and recreate database (CAREFUL!)
mysql -u root -p -e "DROP DATABASE IF EXISTS asterisk; CREATE DATABASE asterisk;"

# Restore from SQL dump
mysql -u root -p asterisk < asterisk_full.sql

# Or from compressed
gunzip -c asterisk_full.sql.gz | mysql -u root -p asterisk

# Grant permissions
mysql -u root -p -e "GRANT ALL ON asterisk.* TO 'cron'@'localhost' IDENTIFIED BY '1234';"
mysql -u root -p -e "GRANT ALL ON asterisk.* TO 'custom'@'localhost' IDENTIFIED BY 'custom1234';"
mysql -u root -p -e "FLUSH PRIVILEGES;"
```

### Step 5: Restore Asterisk Configuration

```bash
# Backup existing conf first
mv /etc/asterisk /etc/asterisk.orig

# Restore
tar xzvf asterisk_conf.tar.gz -C /
```

### Step 6: Restore AGI Scripts

```bash
tar xzvf agi_scripts.tar.gz -C /
chown -R asterisk:asterisk /var/lib/asterisk/agi-bin/
chmod +x /var/lib/asterisk/agi-bin/*.agi
```

### Step 7: Restore Web Files

```bash
# ViciBox 10+
tar xzvf web_files.tar.gz -C /

# Set permissions
chown -R wwwrun:www /srv/www/htdocs/
chmod -R 755 /srv/www/htdocs/
```

### Step 8: Restore Perl Scripts

```bash
tar xzvf astguiclient.tar.gz -C /
chmod +x /usr/share/astguiclient/*.pl
```

### Step 9: Update astguiclient.conf for New Server

```bash
# Edit with new server IP
vi /etc/astguiclient.conf

# Change VARserver_ip to new IP
# VARserver_ip => NEW_IP_ADDRESS
```

### Step 10: Restore Sounds

```bash
tar xzvf sounds.tar.gz -C /
chown -R asterisk:asterisk /var/lib/asterisk/sounds/
```

### Step 11: Restore Recordings (if applicable)

```bash
tar xzvf recordings.tar.gz -C /
chown -R asterisk:asterisk /var/spool/asterisk/monitor/
```

### Step 12: Restore Crontab

```bash
crontab crontab_backup.txt
```

---

## Update Database for New Server IP

### Critical Database Updates

```sql
-- Connect to MySQL
mysql -u root -p asterisk

-- Update server IP in servers table
UPDATE servers SET server_ip='NEW_IP' WHERE server_ip='OLD_IP';

-- Update phones table
UPDATE phones SET server_ip='NEW_IP' WHERE server_ip='OLD_IP';

-- Update conferences table  
UPDATE conferences SET server_ip='NEW_IP' WHERE server_ip='OLD_IP';

-- Update vicidial_conferences
UPDATE vicidial_conferences SET server_ip='NEW_IP' WHERE server_ip='OLD_IP';

-- Update system_settings if single server
UPDATE system_settings SET active_voicemail_server='NEW_IP';

-- Verify changes
SELECT server_ip, server_description FROM servers;
SELECT extension, server_ip FROM phones LIMIT 5;
```

### Script to Update All Server IPs

```bash
#!/bin/bash
OLD_IP="192.168.0.100"
NEW_IP="192.168.0.200"
DB_PASS="your_root_password"

mysql -u root -p$DB_PASS asterisk << EOF
UPDATE servers SET server_ip='$NEW_IP' WHERE server_ip='$OLD_IP';
UPDATE phones SET server_ip='$NEW_IP' WHERE server_ip='$OLD_IP';
UPDATE conferences SET server_ip='$NEW_IP' WHERE server_ip='$OLD_IP';
UPDATE vicidial_conferences SET server_ip='$NEW_IP' WHERE server_ip='$OLD_IP';
UPDATE vicidial_server_trunks SET server_ip='$NEW_IP' WHERE server_ip='$OLD_IP';
SELECT 'Server IP updated in all tables' AS result;
EOF
```

---

## Update Asterisk Configuration for New IP

### sip.conf / pjsip.conf

```bash
# Edit SIP config
vi /etc/asterisk/sip.conf

# Update these lines:
# externip=NEW_PUBLIC_IP
# localnet=NEW_LOCAL_SUBNET/24

# For PJSIP
vi /etc/asterisk/pjsip.conf
```

### extensions.conf

```bash
# Usually no changes needed unless hardcoded IPs
grep -r "OLD_IP" /etc/asterisk/
# Replace any found
sed -i 's/OLD_IP/NEW_IP/g' /etc/asterisk/*.conf
```

---

## Start Services and Verify

### Step 1: Start Services

```bash
# Start MySQL (should already be running)
systemctl start mariadb

# Start Asterisk
systemctl start asterisk

# Start Apache
systemctl start apache2   # ViciBox
systemctl start httpd     # CentOS

# Start keepalive
/usr/share/astguiclient/ADMIN_keepalive_ALL.pl --cu3way
```

### Step 2: Verify Asterisk

```bash
asterisk -rx 'core show version'
asterisk -rx 'sip show peers'
asterisk -rx 'core show channels count'
```

### Step 3: Verify Database

```bash
mysql -u cron -p1234 asterisk -e "SELECT count(*) FROM vicidial_users;"
mysql -u cron -p1234 asterisk -e "SELECT count(*) FROM vicidial_list;"
```

### Step 4: Test Web Interface

```bash
curl -s http://localhost/agc/vicidial.php | head -20
```

### Step 5: Verify Cron Jobs Running

```bash
ps aux | grep keepalive
ps aux | grep AST_
```

---

## Post-Migration Checklist

- [ ] Database restored and accessible
- [ ] Server IP updated in database tables
- [ ] Server IP updated in astguiclient.conf
- [ ] Asterisk running and peers registered
- [ ] Web interface accessible
- [ ] Agent login works
- [ ] Campaigns visible
- [ ] Outbound calls connect
- [ ] Inbound calls route correctly
- [ ] Recordings saving properly
- [ ] Reports generating
- [ ] Cron jobs running

---

## Automated Backup Script

Save as `/usr/local/bin/vici_backup.sh`:

```bash
#!/bin/bash
# VICIdial Automated Backup Script

BACKUP_DIR="/backup"
DATE=$(date +%Y%m%d_%H%M%S)
KEEP_DAYS=7
DB_USER="root"
DB_PASS="your_password"

# Create backup directory
mkdir -p $BACKUP_DIR/$DATE

# Backup database (without logs for speed)
echo "Backing up database..."
mysqldump -u $DB_USER -p$DB_PASS \
  --ignore-table=asterisk.call_log \
  --ignore-table=asterisk.vicidial_log \
  --ignore-table=asterisk.vicidial_closer_log \
  --ignore-table=asterisk.vicidial_agent_log \
  --ignore-table=asterisk.vicidial_xfer_log \
  asterisk | gzip > $BACKUP_DIR/$DATE/asterisk.sql.gz

# Backup configs
echo "Backing up configs..."
tar czf $BACKUP_DIR/$DATE/configs.tar.gz \
  /etc/asterisk \
  /etc/astguiclient.conf \
  /var/lib/asterisk/agi-bin \
  /usr/share/astguiclient \
  2>/dev/null

# Backup web files
echo "Backing up web files..."
tar czf $BACKUP_DIR/$DATE/web.tar.gz /srv/www/htdocs 2>/dev/null || \
tar czf $BACKUP_DIR/$DATE/web.tar.gz /var/www/html 2>/dev/null

# Save crontab
crontab -l > $BACKUP_DIR/$DATE/crontab.txt

# Create single archive
cd $BACKUP_DIR
tar czf vici_backup_$DATE.tar.gz $DATE/
rm -rf $DATE/

# Cleanup old backups
find $BACKUP_DIR -name "vici_backup_*.tar.gz" -mtime +$KEEP_DAYS -delete

echo "Backup complete: $BACKUP_DIR/vici_backup_$DATE.tar.gz"
```

Make executable and add to cron:

```bash
chmod +x /usr/local/bin/vici_backup.sh

# Add to crontab (daily at 2 AM)
echo "0 2 * * * /usr/local/bin/vici_backup.sh >> /var/log/vici_backup.log 2>&1" | crontab -
```

---

## Troubleshooting

### "Access denied" on restore
```bash
mysql -u root -p -e "GRANT ALL ON asterisk.* TO 'cron'@'localhost';"
mysql -u root -p -e "FLUSH PRIVILEGES;"
```

### Asterisk won't start after restore
```bash
# Check config syntax
asterisk -rx 'core show settings'
# Check logs
tail -100 /var/log/asterisk/messages
```

### Web interface 500 error
```bash
# Check permissions
chown -R wwwrun:www /srv/www/htdocs/
# Check Apache logs
tail -100 /var/log/apache2/error_log
```

### Agents can't login after migration
```bash
# Verify server in database
mysql -e "SELECT * FROM asterisk.servers;"
# Update if needed
mysql -e "UPDATE asterisk.servers SET server_ip='NEW_IP';"
```

### Phones not registering
```bash
# Check SIP config
asterisk -rx 'sip show peers'
# Verify externip/localnet in sip.conf
```