Bagi seorang Database Administrator, kegiatan mencadangkan database pastilah hal yang sudah biasa di lakukan dan menjadi rutinitas. Baik secara manual ataupun otomatis, sangatlah penting melakukan pencadangan terhadap database yang di kelola. Lalu, jika dalam 1 server database terdapat banyak sekali database dan kamu perlu mencadangkan beberapa dari mereka secara terpisah, biasanya akan dilakukan dengan mencadangkannya satu persatu. Untuk mempermudah hal tersebut, Saya telah membuat sebuah skrip untuk backup seluruh database MySQL ke file terpisah secara otomatis. Dimana dengan menjalankan skrip ini, kita tidak perlu melakukan pencadangan satu persatu lagi. Baiklah, kamu bisa menggunakan skrip di bawah ini :
Buatlah file skrip dengan nama databaseDump , letakkan di /usr/bin/
vim /usr/bin/databaseDump
lalu isikan dengan skrip dibawah ini :
Skrip Untuk MySQL
#!/bin/bash [ $# -lt 2 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> [<DIR>]" && echo "For example: $(basename $0) 172.0.0.1 root /home/backup" && exit 1 ## [id] deklarasi variabel untuk perintah mysqldump, nilai diambil saat menjalankan skrip ## [en] declare variable for mysqldump command, value is fetched when running script DB_host=$1 DB_user=$2 DIR=$3 ## [id] deklarasi opsi untuk perintah mysqldump ## [en] declare option for mysqldump command [ -n "$DB_options" ] || DB_options="--set-gtid-purged=OFF --add-drop-database --routines" [ -n "$DIR" ] || DIR=. ## [id] pengguna memasukkan nilai array untuk database apa saja yang mau di cadangkan ## [en] user inputs array value for any database to be backed up echo "[id] Ketik nama database yang ingin di Backup (gunakan spasi jika lebih dari 1) :" echo "[en] Type the name of the database you want to backup (use spaces if more than 1) :" read -a DB listDB=$(IFS=, ; echo "${DB[*]}") ## [id] opsi apabila database ingin di arsipkan menjadi file 7zip ## [en] option if the database wants to be archived into a 7zip file read -n 1 -p " [id] Apakah kamu ingin mengarsipkan database jadi file 7zip? [Y/n] : [en] Do you want to archive the database into a 7zip file? [Y/n] : " COMP if [[ $COMP == Y ]] || [[ $COMP == y ]]; then echo echo " [id] Okay, data akan diarsipkan! [en] Okay, the data will be archived! " else echo echo " [id] Database tidak diarsipkan [en] Database not archived " fi ## [id] menginputkan password pengguna database ## [en] input database user password if [ -z "$DB_pass" ]; then echo echo -n "DB password: " read -s DB_pass fi ## [id] cetak informasi memulai pencadangan ## [en] print backup start information echo echo "[id] Mencadangkan database ke dalam file SQL terpisah untuk database '$listDB' kedalam folder='$DIR'" echo "[en] Dumping database into separate SQL command files for database '$listDB' into dir=$DIR" if [[ $COMP == Y ]] || [[ $COMP == y ]]; then tbl_count=0 for str in ${DB[@]}; do (( tbl_count++ )) echo "DUMPING DATABASE: $tbl_count $str" mysqldump --host="$DB_host" --result-file="$DIR/$str-`date '+%Y%m%d_%H%m%d'`.sql" --user="$DB_user" --password="$DB_pass" $DB_options $str 7za a $DIR/$str-`date '+%Y%m%d_%H%m%d'`.7z $DIR/$str-`date '+%Y%m%d'`*.sql rm -f $DIR/$str-`date '+%Y%m%d'`*.sql du $DIR/$str-* -sh done else tbl_count=0 for str in ${DB[@]}; do (( tbl_count++ )) echo "DUMPING DATABASE: $tbl_count $str" mysqldump --host="$DB_host" --result-file="$DIR/$str-`date '+%Y%m%d_%H%m%d'`.sql" --user="$DB_user" --password="$DB_pass" $DB_options $str du $DIR/$str-* -sh done fi ## [id] cetak informasi pencadangan selesai ## [en] print backup information complete echo "[id] $tbl_count database ($listDB) telah dicadangkan kedalam folder=$DIR" echo "[en] $tbl_count database ($listDB) dumped into dir=$DIR" echo "========================================================================" ls -l $DIR
Skrip Untuk MariaDB
#!/bin/bash [ $# -lt 2 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> [<DIR>]" && echo "For example: $(basename $0) 172.0.0.1 root /home/backup" && exit 1 ## [id] deklarasi variabel untuk perintah mysqldump, nilai diambil saat menjalankan skrip ## [en] declare variable for mysqldump command, value is fetched when running script DB_host=$1 DB_user=$2 DIR=$3 ## [id] deklarasi opsi untuk perintah mysqldump ## [en] declare option for mysqldump command [ -n "$DB_options" ] || DB_options="--add-drop-database --routines" [ -n "$DIR" ] || DIR=. ## [id] pengguna memasukkan nilai array untuk database apa saja yang mau di cadangkan ## [en] user inputs array value for any database to be backed up echo "[id] Ketik nama database yang ingin di Backup (gunakan spasi jika lebih dari 1) :" echo "[en] Type the name of the database you want to backup (use spaces if more than 1) :" read -a DB listDB=$(IFS=, ; echo "${DB[*]}") ## [id] opsi apabila database ingin di arsipkan menjadi file 7zip ## [en] option if the database wants to be archived into a 7zip file read -n 1 -p " [id] Apakah kamu ingin mengarsipkan database jadi file 7zip? [Y/n] : [en] Do you want to archive the database into a 7zip file? [Y/n] : " COMP if [[ $COMP == Y ]] || [[ $COMP == y ]]; then echo echo " [id] Okay, data akan diarsipkan! [en] Okay, the data will be archived! " else echo echo " [id] Database tidak diarsipkan [en] Database not archived " fi ## [id] menginputkan password pengguna database ## [en] input database user password if [ -z "$DB_pass" ]; then echo echo -n "DB password: " read -s DB_pass fi ## [id] cetak informasi memulai pencadangan ## [en] print backup start information echo echo "[id] Mencadangkan database ke dalam file SQL terpisah untuk database '$listDB' kedalam folder='$DIR'" echo "[en] Dumping database into separate SQL command files for database '$listDB' into dir=$DIR" if [[ $COMP == Y ]] || [[ $COMP == y ]]; then tbl_count=0 for str in ${DB[@]}; do (( tbl_count++ )) echo "DUMPING DATABASE: $tbl_count $str" mysqldump -u $DB_user -h $DB_host -p$DB_pass $DB_options $str > $DIR/$str-`date '+%Y%m%d_%H%m%d'`.sql 7za a $DIR/$str-`date '+%Y%m%d_%H%m%d'`.7z $DIR/$str-`date '+%Y%m%d'`*.sql rm -f $DIR/$str-`date '+%Y%m%d'`*.sql du $DIR/$str-* -sh done else tbl_count=0 for str in ${DB[@]}; do (( tbl_count++ )) echo "DUMPING DATABASE: $tbl_count $str" mysqldump -u $DB_user -h $DB_host -p$DB_pass $DB_options $str > $DIR/$str-`date '+%Y%m%d_%H%m%d'`.sql du $DIR/$str-* -sh done fi ## [id] cetak informasi pencadangan selesai ## [en] print backup information complete echo "[id] $tbl_count database ($listDB) telah dicadangkan kedalam folder=$DIR" echo "[en] $tbl_count database ($listDB) dumped into dir=$DIR" echo "========================================================================" ls -l $DIR
Setelah disimpan, update permissionnya dengan perintah :
chmod +rwx /usr/bin/databaseDump
Jika sudah, kamu bisa menggunakan skrip tersebut dengan menjalankan
databaseDump
Prosesnya akan seperti animasi GIF dibawah in :
Backup tanpa Arsipkan File
Okay, itulah cara backup seluruh database mysql ke file terpisah secara otomatis dengan menggunakan skrip yang telah dibuat untuk mempermudah melakukannya. Semoga informasi ini dapat bermanfaat. Terima kasih 😀
Baca Juga :