Backup script :
#!/bin/sh
export PGPASSWORD=$(cat /etc/fusionpbx/config.conf | grep database.0.password | awk {'print $3'} | tr -d /\'/\
db_host=127.0.0.1
db_port=5432
now=$(date +%Y-%m-%d)
mkdir -p /var/backups/fusionpbx/postgresql
echo "Backup Started"
#delete postgres backups
find /var/backups/fusionpbx/postgresql/fusionpbx_pgsql* -mtime +4 -exec rm {} \;
#delete the main backup
find /var/backups/fusionpbx/*.tgz -mtime +2 -exec rm {} \;
#backup the database
pg_dump --verbose -Fc --host=$db_host --port=$db_port -U fusionpbx fusionpbx --schema=public -f /var/backups/fusionpbx/postgresql/fusionpbx_pgsql_$now.sql
#package
#tar --exclude='/var/lib/freeswitch/recordings/*/archive' -zvcf /var/backups/fusionpbx/backup_$now.tgz /var/backups/fusionpbx/postgresql/fusionpbx_pgsql_$now.sql /var/www/fusionpbx /usr/share/freeswitch/scripts /var/lib/freeswitch/storage /var/lib/freeswitch/recordings /etc/fusionpbx /etc/freeswitch /usr/share/freeswitch/sounds/music/
#source
#tar -zvcf /var/backups/fusionpbx/backup_$now.tgz /var/backups/fusionpbx/postgresql/fusionpbx_pgsql_$now.sql /var/www/fusionpbx /usr/local/freeswitch/scripts /usr/local/freeswitch/storage /usr/local/freeswitch/recordings /etc/fusionpbx /usr/local/freeswitch/conf /usr/local/freeswitch/sounds/music/
and Restore script:
#!/bin/sh
now=$(date +%Y-%m-%d)
# Retrieve the public IPv4 address
public_ipv4=$(curl -4 -s ifconfig.me | awk '{print $1}')
# Retrieve the private IPv4 address
private_ipv4=$(hostname -I | awk '{print $1}')
# Note change to public or private ip, it depends on your server
ssh_server=$public_ipv4
database_host=127.0.0.1
database_port=5432
export PGPASSWORD=$(cat /etc/fusionpbx/config.conf | grep database.0.password | awk {'print $3'} | tr -d /\'/\
#run the remote backup
ssh -p 22 root@$ssh_server "nice -n -20 /etc/cron.daily/./fusionpbx-backup.sh"
#delete freeswitch logs older 7 days
find /var/log/freeswitch/freeswitch.log.* -mtime +7 -exec rm {} \;
#synchronize the backup directory
#rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/backups/fusionpbx /var/backups
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/backups/fusionpbx/postgresql /var/backups/fusionpbx
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/www/fusionpbx /var/www
rsync -avz -e 'ssh -p 22' root@$ssh_server:/etc/fusionpbx /etc
find /var/backups/fusionpbx/postgresql -mtime +2 -exec rm {} \;
rsync -avz -e 'ssh -p 22' root@$ssh_server:/etc/freeswitch/ /etc
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/lib/freeswitch/storage /var/lib/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/var/lib/freeswitch/recordings /var/lib/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/usr/share/freeswitch/scripts /usr/share/freeswitch
rsync -avz -e 'ssh -p 22' root@$ssh_server:/usr/share/freeswitch/sounds /usr/share/freeswitch
echo "Restoring the Backup"
#extract the backup from the tgz file
#tar -xvpzf /var/backups/fusionpbx/backup_$now.tgz -C /
#remove the old database
psql --host=$database_host --port=$database_port --username=fusionpbx -c 'drop schema public cascade;'
psql --host=$database_host --port=$database_port --username=fusionpbx -c 'create schema public;'
#restore the database
pg_restore -v -Fc --host=$database_host --port=$database_port --dbname=fusionpbx --username=fusionpbx /var/backups/fusionpbx/postgresql/fusionpbx_pgsql_$now.sql
#restart freeswitch
service freeswitch restart
echo "Restore Complete";