Ubuntu Linux Backup MySQL Server Shell Script

1. Install lftp (buat naruh di server ftp tmpt backup) :

sudo apt-get install lftp

2. Bikin script untuk nge dump database dan upload ke ftp server

scriptnya :

#!/bin/bash
### MySQL Server Login Info ###
MUSER=”root”
MPASS=”MYSQL-ROOT-PASSWORD”
MHOST=”localhost”
MYSQL=”$(which mysql)”
MYSQLDUMP=”$(which mysqldump)”
BAK=”/backup/mysql”
GZIP=”$(which gzip)”
### FTP SERVER Login info ###
FTPU=”FTP-SERVER-USER-NAME”
FTPP=”FTP-SERVER-PASSWORD”
FTPS=”FTP-SERVER-IP-ADDRESS”
NOW=$(date +”%d-%m-%Y”)

### See comments below ###
### [ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/* ###
[ ! -d “$BAK” ] && mkdir -p “$BAK”

DBS=”$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse ‘show databases’)”
for db in $DBS
do
FILE=$BAK/$db.$NOW-$(date +”%T”).gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

lftp -u $FTPU,$FTPP -e “mkdir /mysql/$NOW;cd /mysql/$NOW; mput /backup/mysql/*; quit” $FTPS

3.  Simpan script  /home/nama-kamu/mysql.backup.sh

4.  Tambahkan mod ijin eksekusi :

$ chmod +x /home/your-name/mysql.backup.sh

5. Tambahkan di  cron job, biar jalan sendiri di tengah malam

$ sudo crontab -e

Tambahkan cron job berikut :

@midnight /home/you/mysql.backup.sh >/dev/null 2>&1

6. Selesai

Kalau mau dijalankan manual tinggal dikasih perintah :

/home/nama-kamu/mysql.backup.sh

atau

sudo /home/nama-kamu/mysql.backup.sh

sumber :http://www.cyberciti.biz/faq/ubuntu-linux-mysql-nas-ftp-backup-script/

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: