#!/bin/bash # Updated by A.Lang on 2012-09-15 (v2.0) # This script is used for upgrading/fresh install A2B v1.9.4 # # System requirement: Ubuntu 11 + Asterisk 1.6/1.8 (No matter if FreePBX installed) # Usage: chmod 0744 # ./ # version="1.9.4" # MySQL root password, it's passw0rd by default. DBPASS='osslab' GET_URL='https://github.com/Star2Billing/a2billing/tarball/v1.9.4' WEB_ROOT="/var/www" LOG="$(basename "$0").log" #DATETAG=`date +%Y%m%d%H%k%M%S` ( # Make sure only root can run our script [ "$(id -u)" != "0" ] && { echo "Abort: This script must be run as root" 1>&2 exit 1 } # Check if FreePBX was installed FREEPBX_EXISTED=false [ -f /etc/amportal ] && FREEPBX_EXISTED=true # checkifdone() { if [ $1 -eq 0 ];then echo "---> done" echo "" else echo "*** Aborting the process, please try it again after you made the correction." exit 1 fi } clear echo "*******************************************************************************" echo "" echo " Installing A2Billing $version for Ubuntu 11+Asterisk 1.8/1.6" echo "" echo " (c)alang.hsu[AT]gmail.com." echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" echo "WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING" echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" echo "This script assumes you have not changed the default root username For MySQL" echo "If you have, e.g. if you use PBX-Management, press CTRL-C to exit" echo "Then change the MySQL root password in the script to suit your installation." echo " " echo "If an earlier copy of A2Billing is already installed, this script will clean " echo "out origianal DB and entire configuration related to a2billing." echo "*******************************************************************************" echo "" read -p "Press any key to continue or ctrl-C to exit" echo "" # Step1: Removing A2billing Old files echo "Step#1: Removing A2billing Old files" rm -f -R $WEB_ROOT/A2Billing_UI/ 2>/dev/null rm -f -R $WEB_ROOT/A2BCustomer_UI/ 2>/dev/null rm -f -R /var/lib/asterisk/agi-bin/a2billing*.php 2>/dev/null rm -f -R /var/lib/asterisk/agi-bin/lib/ 2>/dev/null rm -f /etc/asterisk/additional_a2billing_iax.conf 2>/dev/null rm -f /etc/asterisk/additional_a2billing_sip.conf 2>/dev/null rm -f /etc/asterisk/extensions_a2billing*.conf 2>/dev/null rm -f /etc/asterisk/a2billing.conf 2>/dev/null rm -f /etc/a2billing.conf 2>/dev/null rm -f -R $WEB_ROOT/agent/ 2>/dev/null rm -f -R $WEB_ROOT/common/ 2>/dev/null rm -f -R /usr/src/a2billing/ 2>/dev/null rm -f /usr/src/Star2Billing-a2billing-*.tar.gz 2>/dev/null rm -f -R /var/log/a2billing 2>/dev/null rm -f -R /usr/local/a2billing 2>/dev/null # Step2: Remove the old cron jobs that included the path name '.../libs_a2billing/crontjob/...'. echo "Step#2: Removing the cron jobs relevant to a2billing from /var/spool/cron/crontabs/asterisk" sed -i "/a2billing/d" /var/spool/cron/crontabs/asterisk 2>/dev/null checkifdone $? # Step3: Drop the old database (if it exists) echo "Step#3: Dropping DB if DB existed... you have to type 'y' so go on. " # Checking if DB mya2billing existed # if existed, return 0 mysql -uroot -p$DBPASS --batch --skip-column-names -e "SHOW DATABASES LIKE '"mya2billing"';" | grep "mya2billing" > /dev/null if [ $? -eq 0 ];then mysqladmin drop mya2billing -u root -p$DBPASS checkifdone $? else checkifdone 0 fi #[ ! $? -eq 0 ] && echo "Abort the process!! you can try it agian after you made change." && exit 1 # Step4: A2billing installation cd /usr/src rm -f -R a2billing/ echo "Step#4: Downloading the source files, please wait a moment..." wget -c --no-check-certificate $GET_URL -O Star2Billing-a2billing-$version.tar.gz tar xzf Star2Billing-a2billing-$version.tar.gz mv Star2Billing-a2billing-*/ a2billing/ checkifdone $? # Step5: Importing database echo "Step#5: Importing the database..." mysql -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/a2billing-createdb-user.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/a2billing-mysql-schema-v1.7.0.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.7.0-to-v1.7.1.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.7.1-to-v1.7.2.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.7.2-to-v1.8.0.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.8.0-to-v1.8.1.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.8.1-to-v1.8.2.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.8.2-to-v1.8.3.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.8.3-to-v1.8.4.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.8.4-to-v1.8.5.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.8.5-to-v1.8.6.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.8.6-to-v1.9.0.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.9.0-to-v1.9.1.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.9.1-to-v1.9.2.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.9.2-to-v1.9.3.sql && mysql mya2billing -u root -p$DBPASS < /usr/src/a2billing/DataBase/mysql-5.x/UPDATE-a2billing-v1.9.3-to-v1.9.4.sql && checkifdone $? # Step6: A2billing web interfaces installation echo "Step#6: A2billing web interfaces installation." cp -Rf /usr/src/a2billing/customer $WEB_ROOT/A2BCustomer_UI chmod 777 $WEB_ROOT/A2BCustomer_UI/templates_c cp -Rf /usr/src/a2billing/agent $WEB_ROOT chmod 777 $WEB_ROOT/agent/templates_c cp -Rf /usr/src/a2billing/admin $WEB_ROOT/A2Billing_UI chmod 777 $WEB_ROOT/A2Billing_UI/templates_c cp -Rf /usr/src/a2billing/common $WEB_ROOT cp /usr/src/a2billing/AGI/a2billing*.php /var/lib/asterisk/agi-bin/ ln -sf $WEB_ROOT/common/lib /var/lib/asterisk/agi-bin/lib chmod +x /var/lib/asterisk/agi-bin/a2billing*.php chown -R asterisk:asterisk /var/lib/asterisk/agi-bin/a2billing*.php [ -d /usr/local/a2billing ] || mkdir -p /usr/local/a2billing cp -R /usr/src/a2billing/Cronjobs /usr/local/a2billing/ ln -sf $WEB_ROOT/common/lib /usr/local/a2billing/Cronjobs/lib chown -R asterisk:asterisk /usr/local/a2billing checkifdone $? # Step7: A2billing configuration files echo "Step#7: A2billing configuration files." cp /usr/src/a2billing/a2billing.conf /etc/asterisk chown asterisk:asterisk /etc/asterisk/a2billing.conf ln -sf /etc/asterisk/a2billing.conf /etc/a2billing.conf cp /usr/src/a2billing/addons/Doc/extensions_a2billing_1_6.conf /etc/asterisk chown asterisk:asterisk /etc/asterisk/extensions_a2billing_1_6.conf sed -i "s/\(port *= *\)\(.*\)/\13306/" /etc/asterisk/a2billing.conf sed -i "s/\(user *= *\)\(.*\)/\1a2billinguser/" /etc/asterisk/a2billing.conf sed -i "s/\(password *= *\)\(.*\)/\1a2billing/" /etc/asterisk/a2billing.conf sed -i "s/\(dbname *= *\)\(.*\)/\1mya2billing/" /etc/asterisk/a2billing.conf checkifdone $? # Step8: A2billing Extra sounds echo "Step#8: A2billing Extra sounds" cd /usr/src/a2billing/addons/sounds ./install_a2b_sounds.sh checkifdone $? # Step9: Asterisk files echo "" echo "Step#9: Setting Asterisk" cd /etc/asterisk/ touch additional_a2billing_iax.conf touch additional_a2billing_sip.conf if $FREEPBX_EXISTED; then # If FreePBX existed [ "$(grep '#include additional_a2billing_sip.conf' /etc/asterisk/sip_custom.conf)xx" == "xx" ] && echo "#include additional_a2billing_sip.conf" >> /etc/asterisk/sip_custom.conf [ "$(grep '#include additional_a2billing_iax.conf' /etc/asterisk/iax_custom.conf)xx" == "xx" ] && echo "#include additional_a2billing_iax.conf" >> /etc/asterisk/iax_custom.conf if [ "$(grep '#include extensions_a2billing_1_6.conf' /etc/asterisk/extensions_custom.conf)xx" == "xx" ]; then echo -e >> /etc/asterisk/extensions_custom.conf echo "#include extensions_a2billing_1_6.conf" >> /etc/asterisk/extensions_custom.conf fi if [ "$(grep '\[myasterisk\]' /etc/asterisk/manager_custom.conf)xx" == "xx" ]; then echo -e >> /etc/asterisk/manager_custom.conf echo " [myasterisk] secret = mycode deny = 0.0.0.0/0.0.0.0 permit = 127.0.0.1/255.255.255.0 read = system,call,log,verbose,command,agent,user write = system,call,log,verbose,command,agent,user" >> /etc/asterisk/manager.conf fi else # If FreePBX NOT existed [ "$(grep '#include additional_a2billing_sip.conf' /etc/asterisk/sip.conf)xx" == "xx" ] && echo "#include additional_a2billing_sip.conf" >> /etc/asterisk/sip.conf [ "$(grep '#include additional_a2billing_iax.conf' /etc/asterisk/iax.conf)xx" == "xx" ] && echo "#include additional_a2billing_iax.conf" >> /etc/asterisk/iax.conf if [ "$(grep '#include extensions_a2billing_1_6.conf' /etc/asterisk/extensions.conf)xx" == "xx" ]; then echo -e >> /etc/asterisk/extensions.conf echo "#include extensions_a2billing_1_6.conf" >> /etc/asterisk/extensions.conf fi if [ "$(grep '\[myasterisk\]' /etc/asterisk/manager.conf)xx" == "xx" ]; then echo -e >> /etc/asterisk/manager.conf echo " [myasterisk] secret = mycode deny = 0.0.0.0/0.0.0.0 permit = 127.0.0.1/255.255.255.0 read = system,call,log,verbose,command,agent,user write = system,call,log,verbose,command,agent,user" >> /etc/asterisk/manager.conf fi fi checkifdone $? # Step10: A2billing file permissions echo "Step#10: Setting file permissions" chmod 664 /etc/asterisk/additional_a2billing_iax.conf chmod 664 /etc/asterisk/additional_a2billing_sip.conf chmod 664 /etc/asterisk/extensions_a2billing_1_6.conf chown -R asterisk:asterisk /etc/asterisk/ chown -R asterisk:asterisk $WEB_ROOT/common chown -R asterisk:asterisk $WEB_ROOT/A2Billing_UI chown -R asterisk:asterisk $WEB_ROOT/A2BCustomer_UI chown -R asterisk:asterisk $WEB_ROOT/agent chown -R asterisk:asterisk /var/lib/asterisk/ checkifdone $? # Step11: Install cron jobs echo "Step#11: Setting Cronjobs" echo "# Automatically added for A2Billing 0 * * * * php /usr/src/a2billing/Cronjobs/a2billing_alarm.php 0 12 * * * php /usr/src/a2billing/Cronjobs/a2billing_archive_data_cront.php 0 10 21 * * php /usr/src/a2billing/Cronjobs/a2billing_autorefill.php #Batch process at 00:20 each day 20 0 * * * php /usr/src/a2billing/Cronjobs/a2billing_batch_process.php #Bill DID usage at 00:00 each day 0 0 * * * php /usr/src/a2billing/Cronjobs/a2billing_bill_diduse.php #Generate Invoices at 6am everyday 0 6 * * * php /usr/src/a2billing/Cronjobs/a2billing_batch_billing.php #Check if balance below preset value, and email user if so. 1 * * * * php /usr/src/a2billing/Cronjobs/a2billing_notify_account.php #Charge subscriptions at 06:05 on the 1st of each month 0 6 1 * * php /usr/src/a2billing/Cronjobs/a2billing_subscription_fee.php #Update currencies at 01:00 each day 0 1 * * * php /usr/src/a2billing/Cronjobs/currencies_update_yahoo.php " >> /var/spool/cron/crontabs/asterisk checkifdone $? # Step12: Create Log Files # Log files and Permissions echo "Step#12: Create Log files" [ -d /var/log/a2billing ] || mkdir -p /var/log/a2billing touch /var/log/asterisk/a2billing-daemon-callback.log touch /var/log/a2billing/a2billing-daemon-callback.log touch /var/log/a2billing/cront_a2b_alarm.log touch /var/log/a2billing/cront_a2b_autorefill.log touch /var/log/a2billing/cront_a2b_batch_process.log touch /var/log/a2billing/cront_a2b_bill_diduse.log touch /var/log/a2billing/cront_a2b_subscription_fee.log touch /var/log/a2billing/cront_a2b_currency_update.log touch /var/log/a2billing/cront_a2b_invoice.log touch /var/log/a2billing/cront_a2b_check_account.log touch /var/log/a2billing/a2billing_paypal.log touch /var/log/a2billing/a2billing_epayment.log touch /var/log/a2billing/api_ecommerce_request.log touch /var/log/a2billing/api_callback_request.log touch /var/log/a2billing/a2billing_agi.log chown asterisk:asterisk /var/log/asterisk/a2billing-daemon-callback.log chown -R asterisk:asterisk /var/log/a2billing chmod 0777 /var/log/a2billing/*.log checkifdone $? # Step13: for Predictive Call(Campaigns) echo "Step#13: For predictive call required" [ -d /var/run/a2billing ] || mkdir /var/run/a2billing chown asterisk:asterisk /var/run/a2billing checkifdone $? echo "" echo "" echo "" echo "*******************************************************************" echo "A2Billing installation done" echo "You can see the log on the file $LOG" echo "" echo "Please Restart the Asterisk service" echo "" echo "The default username is [root]" echo "The default password is [changepassword]" echo "*******************************************************************" ) 2>&1 | tee -a $LOG #EOF