#! /bin/bash . /usr/src/k_framework/main.sh k_config_details # ---- VARS ----- VERSION="1.0.1" SCRIPT_NAME="SaaS DB backup to backup.kolmisoft.com" UPLOAD_SERVER="backup.kolmisoft.com" GREEN='\033[0;32m' NC='\033[0m' # ---- FUNCTIONS ----- upload_db() { report "Ready to upload? Press enter" 3 read report "After successful connection to sftp paste these commands in sftp shell:" 3 echo "-----------------------------------------------------------------------" echo -e " ${GREEN}cd Backups${NC}" echo -e " ${GREEN}put ${BACKUP_NAME}${NC}" echo -e " ${GREEN}exit${NC}" echo "-----------------------------------------------------------------------" if sftp support@"$UPLOAD_SERVER"; then report "Data archive was Successfully uploaded" 3 EXIT_CODE=0 return $EXIT_CODE else report "Failed to upload DB backup to $UPLOAD_SERVER" 1 EXIT_CODE=1 return $EXIT_CODE fi } make_backup() { report "Making DB Backup, it can take a while" 3 if MYSQL_PWD=$DB_PASSWORD /usr/bin/mysqldump --single-transaction -h $DB_HOST -u $DB_USERNAME $P_OPT $DB_NAME | bzip2 > "$BACKUP_NAME"; then report "DB backup created to $BACKUP_NAME" 0 EXIT_CODE=0 return $EXIT_CODE else EXIT_CODE=1 return $EXIT_CODE fi } # ---- MAIN ----- k_start if [[ $DB_PRESENT != 1 ]]; then report "DB_PRESENT != 1. This script should be used on DB server" 1 k_exit 1 fi if [[ -z $1 ]]; then report "Usage: $0 klientoID_paskutiniaIPskaiciai [mysql_stop]" 3 k_exit 1 elif [[ ! $1 =~ [0-9]{1,10}_[0-9]{1,3} ]]; then report "Usage: $0 klientoID_paskutiniaIPskaiciai [mysql_stop]" 3 report "Example: $0 12345_18 mysql_stop" 2 report "$1 is NOT in correct format" 1 k_exit 1 else BACKUP_NAME="/root/$1.bz2" fi if make_backup; then upload_db else report "DB backup failed. Check manually" 1 fi if [[ $2 == mysql_stop ]]; then service mysqld stop fi k_exit $EXIT_CODE