[LINUX] backup mysql and send to remote server - fourslickz/notes GitHub Wiki

#!/bin/bash

# credentials
host=yourHost
user=yourUsername
pass=YourPassword

dump=/usr/bin/mysqldump
dir=/root/backup_db

remote_dir=/home/user/backup_db/mysql/
remote_host=ipOfServer
remote_user=usernameOfServer
remote_ssh_port=sshPortOfServer


# backup function
backup(){
  echo "backup DB: $dbs [proceed]";

  date=$(date +%Y%m%d-%H%M)

  # dump
  $dump $dbs -u$user -p$pass -h$host > $dir/$dbs-$date.sql
  zip -m $dir/$dbs-$date.zip $dir/$dbs-$date.sql

  # send to archive server
  scp -P $remote_ssh_port $dir/$dbs-$date.zip $remote_user@$remote_host:$remote_dir

  echo "done";
  echo "";
}

# backup process
database="db1 db2"
for dbs in $database
do
  backup
done


# remove old file
find $dir -type f -mtime +7 -delete



exit 0;