#!/bin/bash

# sed -i -n -e '/`mysql`/,/`timeclock`/!p' full-backup.sql

# Incredible Backup (c) Copyright 2005-2020 Ward Mundy & Associates LLC. All rights reserved.
#                Incredible Backup 2021 is licensed under the GPL2 license
#
#  For a copy of license, visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#

# Unless required by applicable law or agreed to in writing, software distributed under this License 
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
# See the License for the specific language governing permissions and limitations under the License.

PROCEEDNOW=false

clear
echo "BY USING INCREDIBLE BACKUP 2021, YOU AGREE TO ASSUME ALL RESPONSIBILITY"
echo "FOR USE OF THE PROGRAMS INCLUDED IN THIS INSTALLATION. NO WARRANTIES"
echo "EXPRESS OR IMPLIED INCLUDING MERCHANTABILITY AND FITNESS FOR PARTICULAR"
echo "USE ARE PROVIDED. YOU ASSUME ALL RISKS KNOWN AND UNKNOWN AND AGREE TO"
echo "HOLD WARD MUNDY, WARD MUNDY & ASSOCIATES LLC, NERD VITTLES, AND THE PBX"
echo "IN A FLASH DEVELOPMENT TEAM HARMLESS FROM ANY AND ALL LOSS OR DAMAGE"
echo "WHICH RESULTS FROM YOUR USE OF THIS SOFTWARE. IF ANY OF THESE TERMS"
echo "AND CONDITIONS ARE RULED TO BE UNENFORCEABLE, YOU AGREE TO ACCEPT ONE"
echo "DOLLAR IN U.S. CURRENCY AS COMPENSATORY AND PUNITIVE LIQUIDATED DAMAGES"
echo "FOR ANY AND ALL CLAIMS YOU AND ANY USERS OF THIS SOFTWARE MIGHT HAVE."
echo " "

echo "Incredible Backup takes a snapshot of your VoIP server."
echo "You can edit tar command to add additional directories."
echo "Just remove the read command to run script as cron job."
echo "Asterisk, MySQL, and Apache are OFF LINE during backup."
echo "Restore to server with same Asterisk + FreePBX version."
echo "If used, reload IncrediblePBX on server before restore."
echo " "
echo "File Name is PLATFORM-DATE-Asterisk-FreePBX-Incredible."
echo "Move backup from /backup to remote storage when done."

NOW=`date +'%Y.%m.%d.%H.%M'`
VER=`asterisk -V`
if [ -e "/etc/debian_version" ]; then
 PLATFORM="RP"
else
 PLATFORM="CN"
fi
FPBX=`cat /var/www/html/admin/modules/framework/module.xml | grep "<version>" | tail -1 | cut -f 2 -d ">" | sed 's|</version||'`
if [ -e "/etc/pbx/.version" ]; then
 IPBX=`cat /etc/pbx/.version`
 BACKUP="$PLATFORM-$NOW-A${VER:9}-F$FPBX-I$IPBX.tar"
else
 IPBX=`cat /etc/pbx/.incredible`
 BACKUP="$PLATFORM-$NOW-A${VER:9}-F$FPBX-I$IPBX.tar"
fi

echo "BACKUP: $BACKUP.gz"
echo " "
if ! $PROCEEDNOW ; then
 read -p "To proceed at your own risk, press the Enter key."
fi

if [ -e "/etc/debian_version" ]; then
 systemctl stop apache2
 fwconsole stop
 if [ ! -d "/var/spool/asterisk/backup" ]; then
  mkdir /var/spool/asterisk/backup
  chown asterisk:asterisk /var/spool/asterisk/backup
 fi
 mysqldump -u root -ppassw0rd --all-databases --single-transaction --quick --lock-tables=true --events --ignore-table=mysql.event > /root/full-backup.sql
 systemctl stop mysqld
else
 echo "Backing up your data. One moment please..."
 systemctl stop httpd
 fwconsole stop
 if [ ! -d "/var/spool/asterisk/backup" ]; then
  mkdir /var/spool/asterisk/backup
  chown asterisk:asterisk /var/spool/asterisk/backup
 fi
 mysqldump -u root -ppassw0rd --all-databases --single-transaction --quick --lock-tables=true --events --ignore-table=mysql.event > /root/full-backup.sql
 systemctl stop mysqld
fi

if [ ! -d "/backup" ]; then
 mkdir /backup
fi
touch /etc/knockd.conf
touch /etc/dahdi
mkdir /etc/modprobe.d
touch /etc/modprobe.d/dahdi.conf

cd /backup
echo "Creating backup. Please wait..."
if [ ! -d "/tftpboot" ]; then
 mkdir /tftpboot
 chmod -R 777 /tftpboot
fi

#fwconsole ma disable api
#wait
#fwconsole ma disable webrtc
#wait
#fwconsole ma disable sms
#wait
#fwconsole ma uninstall ucp
#wait
#fwconsole ma uninstall pm2
#wait
#fwconsole ma disable xmpp
#wait


#fwconsole stop
#wait
#systemctl stop mysqld
#wait
#systemctl stop httpd
#wait

if [ -e /etc/debian_version ]; then
 tar -cf $BACKUP /var/www /var/lib/asterisk /tftpboot /root /etc/pbx /etc/asterisk /etc/crontab /var/spool/asterisk /etc/freepbx.conf /etc/amportal.conf /etc/mysql/debian.cnf /etc/dahdi /etc/modprobe.d/dahdi.conf /home/asterisk
 systemctl start mysqld
 systemctl start apache2
 fwconsole start
else
 tar -cf $BACKUP /var/www /var/lib/asterisk /tftpboot /etc/knockd.conf /root /etc/pbx /etc/asterisk /etc/crontab /var/spool/asterisk /etc/freepbx.conf /etc/amportal.conf /etc/dahdi /etc/modprobe.d/dahdi.conf /home/asterisk
 systemctl start httpd
 systemctl start mysqld
 fwconsole start
# fwconsole reload
# fwconsole chown
# rm -Rf /home/asterisk/{.npm,.npmrc,.node-gyp,.package_cache}
# rm -Rf /var/www/html/admin/modules/pm2/node/node_modules
# fwconsole ma install pm2
# rm -Rf /var/www/html/admin/modules/ucp/node/node_modules
# fwconsole ma enable api
# fwconsole ma install ucp
# fwconsole ma enable webrtc
# fwconsole ma enable sms
# fwconsole ma enable xmpp
# fwconsole restart
# fwconsole reload
fi

echo "Asterisk is back on line."
echo "Compressing the backup. Hang in there..."

tar --delete -f $BACKUP 'var/spool/asterisk/backup'

gzip $BACKUP

echo "Backup finished and backup file saved."
echo "BACKUP: /backup/$BACKUP.gz"
# end of script
