Skip to content

Instantly share code, notes, and snippets.

@maxwellpower
Last active July 19, 2017 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxwellpower/b087e19b73dd13aeed4c9f825fc5b3c1 to your computer and use it in GitHub Desktop.
Save maxwellpower/b087e19b73dd13aeed4c9f825fc5b3c1 to your computer and use it in GitHub Desktop.
NGCP Backup Script
#!/bin/bash
# SIP SERVER Backup and Sync - Version 3.0 - June 2015
# @copyright (C) 2015-2017 Maxwell Power <https://github.com/maxwellpower>
# @license MIT
# Define Variables
BACKUP_TMP_DIR=/root/backups/tmp
BACKUP_DIR=/root/backups
MYSQL_USR=root
MYSQL_PW=
NGCP_DIR=/etc/ngcp-config
SQLCONF_DIR=/etc/mysql
NAS_DIR=/mnt/backups
LOGS_DIR=/var/log/ngcp
CDR_DIR=/home/jail/home/cdrexport
SSL_DIR=/opt/ssl
REMOTE_HOST=
# Grab the date
DATE=`date -I`
# Let's Go!
echo "Beginning Backup ... this might take a while!"
# Remove Old Backups (7 days)
echo "Removing backup files older than 20 days"
find $BACKUP_DIR -type f -mtime +20 -exec rm -f {} \;
# Create TMP Folder
mkdir $BACKUP_TMP_DIR
# Backup All MySQL Databases
echo "Dumping MySQL Databases"
mysqldump -u$MYSQL_USR -p$MYSQL_PW --all-databases --events --routines --master-data > $BACKUP_TMP_DIR/mysql.sql
# Backup NGCP Config
echo "Dumping NGCP"
tar -cvf $BACKUP_TMP_DIR/NGCP.tar $NGCP_DIR > /dev/null 2>&1
# Backup SQL Config
echo "Dumping SQL Config"
tar -cvf $BACKUP_TMP_DIR/SQLCONF.tar $SQLCONF_DIR > /dev/null 2>&1
# Backup Logs
echo "Dumping LOGS"
tar -cvf $BACKUP_TMP_DIR/LOGS.tar $LOGS_DIR > /dev/null 2>&1
# Backup CDRs
echo "Dumping CDRs"
tar -cvf $BACKUP_TMP_DIR/CDR.tar $CDR_DIR > /dev/null 2>&1
# Backup Logs
echo "Dumping SSL Config"
tar -cvf $BACKUP_TMP_DIR/SSL.tar $SSL_DIR > /dev/null 2>&1
# Grab some other stuff ...
echo "Getting other stuff"
cp /etc/issue.net $BACKUP_TMP_DIR
cp /root/.ssh/authorized_keys $BACKUP_TMP_DIR
cp /root/backup.sh $BACKUP_TMP_DIR
# Combine Backup Files to Daily Package
echo "Packaging and Compressing Archive"
tar -cvzf $BACKUP_DIR/$DATE.tar.gz $BACKUP_TMP_DIR/ > /dev/null 2>&1
# Sync Dump Files
echo "Syncronizing config with remote"
echo "Sync MYSQL"
rsync -avz --progress $BACKUP_TMP_DIR/mysql.sql $REMOTE_HOST:/root/sqlSync
echo "Sync config files"
echo "ngcp"
rsync -avz --progress /etc/ngcp-config/ngcpcfg.cfg $REMOTE_HOST:/etc/ngcp-config/ngcpcfg.cfg
echo "constants"
rsync -avz --progress /etc/ngcp-config/constants.yml $REMOTE_HOST:/etc/ngcp-config/constants.yml
echo "sql"
rsync -avz --progress $SQLCONF_DIR $REMOTE_HOST:$SQLCONF_DIR
echo "SSL"
rsync -avz --progress /opt/ssl $REMOTE_HOST:/opt/ssl
# Restore MySQL data
echo "Restoring SQL data"
ssh $REMOTE_HOST 'mysql -u'$MYSQL_USR' -p'$MYSQL_PW' < /root/sqlSync/mysql.sql'
echo "Flushing Logs"
ssh $REMOTE_HOST 'mysql -u'$MYSQL_USR' -p'$MYSQL_PW' -e "FLUSH LOGS; FLUSH MASTER;"'
echo "Restarting"
ssh $REMOTE_HOST 'service mysql restart'
echo "Done with SQL data"
# Remove Temp Files
echo "Cleaning up TMP files"
rm -r $BACKUP_TMP_DIR
# Sync Dump Files
echo "Syncronizing Backups with Remote"
rsync -avz --progress --delete-after /root/backups/ $REMOTE_HOST:/root/backups/
echo "Copying to remote backup disk"
ssh $REMOTE_HOST 'rsync -avz --progress --delete-after /root/backups/ /mnt/backups/'
echo "Applying config changes and restarting kamailio"
ssh $REMOTE_HOST 'ngcp-update-db-schema && ngcp-update-cfg-schema && ngcpcfg apply && service kamailio-lb restart && service kamailio-proxy restart'
# All Done
echo "Backup Complete!"
MIT License
Copyright (c) 2015-2017 Maxwell Power
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment