Articles Comments

{ Berbagi, Menulis, Dan Mengajar } Ilmu… » 7. Operating System » Backup MYSQL dan Lakukan Mirror ke Server Lain

Backup MYSQL dan Lakukan Mirror ke Server Lain

Sekenario Lakukan Backup

1. Lakukan Backup Seluruh Database

@daily /bin/sh /root/script/BackupDB.sh

—Isi File BackupDB.sh—

#! /bin/bash

TIMESTAMP=$(date +”%F”)
BACKUP_DIR=”/home/database_only/backup/$TIMESTAMP”
MYSQL_USER=”root”
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD=”pr*******”
MYSQLDUMP=/usr/bin/mysqldump

mkdir -p “$BACKUP_DIR/mysql”

databases=`$MYSQL –user=$MYSQL_USER -p$MYSQL_PASSWORD -e “SHOW DATABASES;” | grep -Ev “(Database|information_schema|performance_schema)”`

for db in $databases; do
$MYSQLDUMP –force –opt –user=$MYSQL_USER -p$MYSQL_PASSWORD –databases $db | gzip > “$BACKUP_DIR/mysql/$db.gz”

2. Lakukan Mirror ke Group Database yang akan dikopikan ke Server

*/15 * * * * /bin/sh /root/script/MirrorDB.sh

—Isi File MirrorDB.Sh–

#! /bin/bash

TIMESTAMP=$(date +”%F”)
BACKUP_DIR=”/home/database_only/backup/$TIMESTAMP/mysql”
MYSQL_USER=”dimas”
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD=”pr*******”
HOST=192.168.1.100
MYSQLDUMP=/usr/bin/mysqldump

echo $BACKUP_DIR;
echo $TIMESTAMP;

cd $BACKUP_DIR
gunzip -f 0328*.gz;gunzip -f 0324*.gz;gunzip -f 0335*.gz;gunzip -f db_0323*.gz
ls 0328* > dbPemda.txt;ls 0324* >> dbPemda.txt;ls 0335* >> dbPemda.txt;ls db_0323* >> dbPemda.txt
dbPemda=$BACKUP_DIR/dbPemda.txt
echo $dbPemda;

while read line
do
echo “$line”
$MYSQL -u $MYSQL_USER -p$MYSQL_PASSWORD -h $HOST < $line;
done < $dbPemda

Filed under: 7. Operating System

Leave a Reply

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>