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

#!/bin/bash

# credentials
host=ip-server
port=mysql-port
user=mysql-username
pass=mysql-password

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

remote_dir=/home/user/backup_db/mysql
remote_host=ServerIp
remote_user=ServerUser
remote_ssh_port=serverSshPort


# backup function
backup(){
  echo "backup DB: $dbs [proceed]";
  date=$(date +%Y%m%d-%H%M%S)
  tanggal=$(date +%d)
  bulan=$(date +%m)
  tahun=$(date +%Y)

  if [ ! -d "$dir/$tahun/$bulan/$tanggal/" ];
    then mkdir --parents $dir/$tahun/$bulan/$tanggal;
  fi

  dump=/usr/bin/mysqldump

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

  cd $dir
  rsync -avz -e "ssh -p 2244" --relative ./$tahun/$bulan/$tanggal/$dbs-$date.zip $remote_user@$remote_host:$remote_dir

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

  # remove old file
  # rm -f $dir/$dbs-$date.zip

  echo "done";
  echo "";
  cd ~;
}

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