Search code examples
mysqlbashshellbackupsynology

Backup Mysql database with shell script - bind-address set to localhost


I'm using a shell script to backup my databases from a remote server to my Synology NAS. The script is working fine but when I set the Bind Address in de mysql config @ the remote server to 'localhost', the script isn't working anymore. The script needs a SSH verification.

The question: how can I create the SSH connection / credentials in this script?

Underneath the script I use:

#! /bin/bash

TIMESTAMP=$(date +"%Y-%m-%d")
PAST=$(date +"%Y-%m-%d" -d "30 days ago")
BACKUP_DIR="/volume1/dir/dir2/$TIMESTAMP"
PAST_DIR="/volume1/dir/di2/$PAST"
HOST="myip"
MYSQL_USER="username"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="password"
MYSQLDUMP=/usr/bin/mysqldump


rm -rv $PAST_DIR 

mkdir -p $BACKUP_DIR

databases=`$MYSQL -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"`

for db in $databases; do
  $MYSQLDUMP --force --opt -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip > "$BACKUP_DIR/$db.sql.gz"
done

Solution

  • You need to send the command by ssh. The modified version of your script should do the task.

    #! /bin/bash
    
    TIMESTAMP=$(date +"%Y-%m-%d")
    PAST=$(date +"%Y-%m-%d" -d "30 days ago")
    BACKUP_DIR="/volume1/dir/dir2/$TIMESTAMP"
    PAST_DIR="/volume1/dir/di2/$PAST"
    HOST="myip"
    MYSQL_USER="username"
    MYSQL=/usr/bin/mysql
    MYSQL_PASSWORD="password"
    MYSQLDUMP=/usr/bin/mysqldump
    
    SSH_KEY=""
    SSH_USER=""
    SSH_HOST=""
    
    REMOTE_COMMAND="$MYSQL -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD -s -e 'SHOW DATABASES' | grep -v 'information_schema'"
    databases=$(ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND")
    
    created=0
    
    for db in $databases; do
      DUMP="$BACKUP_DIR/$db.sql"
    
      # To save backup on remote machine
      case $created in
        0) REMOTE_COMMAND="rm -rv $PAST_DIR; mkdir -p $BACKUP_DIR"; created=1
           ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND" ;;
        *) ;;
      esac
    
      REMOTE_COMMAND="$MYSQLDUMP --force --opt -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db | gzip -9 > $DUMP.gz"
      ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND"
    
      # To save backup on local machine
      # case $created in
      #   0) rm -rv $PAST_DIR; mkdir -p $BACKUP_DIR; created=1 ;;
      #   *) ;;
      # esac
      # REMOTE_COMMAND="$MYSQLDUMP --force --opt -h $HOST --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db"
      # ssh -i $SSH_KEY ${SSH_USER}@${SSH_HOST} "$REMOTE_COMMAND" | gzip -9 > $DUMP.gz
    
    done
    

    NOTE: you need to set the SSH_KEY (if you have ssh key based authentication), SSH_USER, SSH_HOST variables. Also I provided two solutions in case you want to save the backup on your local machine, from where you started the command. Also note the -s flag with the mysql command (SHOW DATABASES part), which will not print out the colum name (here Database).