#!/bin/sh
export PGPASSWORD=$(cat /etc/fusionpbx/config.php | grep db_password | awk {'print $3'} | tr -d /\'/\;)
db_host=127.0.0.1
db_port=5432
now=$(date +%Y-%m-%d)
bucket=wasabi:wasabibucket/pbxbackups/`hostname`
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/
tar --exclude='/var/lib/freeswitch/recordings/*/archive' -zvcf /var/backups/fusionpbx_backup_$now.tgz /var/backups/fusionpbx /var/www/fusionpbx /etc/fusionpbx /etc/freeswitch /var/lib/freeswitch /usr/share/freeswitch
rclone copy /var/backups/fusionpbx_backup_$now.tgz $bucket/
#rclone sync /var/backups/fusionpbx $bucket/var/backups/fusionpbx
#rclone sync /var/www/fusionpbx $bucket/var/www/fusionpbx
#rclone sync /etc/fusionpbx $bucket/etc/fusionpbx
#rclone sync /etc/freeswitch $bucket/etc/freeswitch
#rclone sync /var/lib/freeswitch $bucket/var/lib/freeswitch
#rclone sync /usr/share/freeswitch $bucket/usr/share/freeswitch
echo "Backup Completed"