July 17, 2024

Tools Backup Restore Database Menggunakan Shell Programming (Part 2 Backup Restore)

5 min read

Di postingan sebelumnya saya pernah menuliskan tools backup restore database menggunakan shell programming part 1 backup di buat pada file-file terpisah. Sekarang saya coba tuliskan dalam bentuk function dalam 1 file yang terdiri dari backup dan restore database:

Langkah 1

Buat file baru untuk menyimpan script program yang dibuat, berikut script yang saya gunakan :

[sourcecode]
#digunakan untuk menghapus file cache dalam direktory /tmp
rm /tmp/input.* /tmp/nama /tmp/pass /tmp/host /tmp/dir

#pembuatan function menu utama (menu backup dan restore)
menu_utama(){
dialog –title " menu backup and restore database" –backtitle "backup and \
restore utility" –menu "Use the buttons [UP] or [DOWN] to select" 10 38 2 Backup \
"Backup database" Restore "Restore database" 2>/tmp/input.$$;
pilihan=`cat /tmp/input.$$`
case $pilihan in
Backup) backup;;
Restore) restore;;
esac
}

#script untuk fuction backup (tampilan menu pilihan backup)
backup(){
dialog –title "Backup Database" –backtitle "backup database menu" \
–menu "Use the buttons [UP] or [DOWN] to select" 10 45 3 \
one_database "Backup one database" multi_database "multi database max 3" \
all_database "all database" 2>/tmp/input.$$;
chose=`cat /tmp/input.$$`
case $chose in
one_database) username;;
multi_database) username2;;
all_database) username3;;
esac
}

# script untuk functio restore (tampilan menu restore)
restore(){
dialog –title "Backup Database" –backtitle "backup database menu" \
–menu "Use the buttons [UP] or [DOWN] to select" 10 45 2 \
one_file "Backup one database" multi_file "multi database max 3" \
2>/tmp/input.$$
chose=`cat /tmp/input.$$`
case $chose in
one_file) restore_one_uname;;
multi_file) restore_multi_uname;;
esac
}

# script untuk functio restore (halaman nama admin database)
restore_one_uname(){
dialog –title "restore database form" –backtitle "restore database" \
–inputbox "masukkan nama anda " 10 40 2>/tmp/nama
pilihan=$?
case $pilihan in
0) restore_one_pass;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio restore (halaman password admin database)
restore_one_pass(){
dialog –title "restore database form" –backtitle "restore database" \
–clear –insecure –passwordbox "masukkan password " 10 40 2>/tmp/pass
pilihan=$?
case $pilihan in
0) restore_one_host;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

## script untuk functio restore (halaman host database server)
restore_one_host(){
dialog –title "restore database form" –backtitle "restore database" \
–clear –inputbox "masukkan host database " 10 40 2>/tmp/host
pilihan=$?
case $pilihan in
0) restore_file;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio restore (lokasi file database untuk di import)
restore_file(){
dialog –title "restore database form" –backtitle "restore database" \
–clear –inputbox "masukkan alamat penyimpanan" 10 40 2>/tmp/dir
pilihan=$?
simnama=`cat /tmp/nama`
simpass=`cat /tmp/pass`
simpanhost=`cat /tmp/host`
#simpandname=`cat /tmp/dname`
simpandir=`cat /tmp/dir`
date=`date +%y%m%d`
hapus=`rm /tmp/nama /tmp/pass /tmp/host /tmp/dir /tmp/input.*`
mysql -u ${simnama} -p${simpass} -h ${simpanhost} < ${simpandir}
case $pilihan in
0) echo "backup database sukses"
$hapus;;
1) echo "stop"
$hapus;;
255) echo "dibatalkan"
$hapus;;
esac
}

# script untuk functio backup (username admin database (1 database))
username(){
dialog –title "backup database form" –backtitle "form one database" \
–inputbox "masukkan nama anda" 10 40 2>/tmp/nama
pilihan=$?
case $pilihan in
0) password;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (username admin database (2 database))
username2(){
dialog –title "backup database form" –backtitle "form one database" \
–inputbox "masukkan nama anda " 10 40 2>/tmp/nama
pilihan=$?
case $pilihan in
0) password2;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (username admin database (3 database))
username3(){
dialog –title "backup database form" –backtitle "form one database" \
–inputbox "masukkan nama anda " 10 40 2>/tmp/nama
pilihan=$?
case $pilihan in
0) password3;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (password admin database (1 database))
password(){
dialog –title "backup database form" –backtitle "form one database" \
–clear –insecure –passwordbox "masukkan password " 10 40 2>/tmp/pass
pilihan=$?
case $pilihan in
0) host;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (password admin database (2 database))
password2(){
dialog –title "backup database form" –backtitle "form one database" \
–clear –insecure –passwordbox "masukkan password " 10 40 2>/tmp/pass
pilihan=$?
case $pilihan in
0) host2;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (password admin database (3 database))
password3(){
dialog –title "backup database form" –backtitle "form one database" \
–clear –insecure –passwordbox "masukkan password " 10 40 2>/tmp/pass
pilihan=$?
case $pilihan in
0) host3;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (host server database (1 database))
host(){
dialog –title "backup database form" –backtitle "form one database" \
–clear –inputbox "masukkan host database " 10 40 2>/tmp/host
pilihan=$?
case $pilihan in
0) dbname;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (host server database (2 database))
host2(){
dialog –title "backup database form" –backtitle "form one database" \
–clear –inputbox "masukkan host database " 10 40 2>/tmp/host
pilihan=$?
case $pilihan in
0) dbname2;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (host server database (3 database))
host3(){
dialog –title "backup database form" –backtitle "form one database" \
–clear –inputbox "masukkan host database " 10 40 2>/tmp/host
pilihan=$?
case $pilihan in
0) dbname3;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (nama database (1 database))
dbname(){
dialog –title "backup database form" –backtitle "form one database" \
–clear –inputbox "masukkan nama database " 10 40 2>/tmp/dname
pilihan=$?
case $pilihan in
0) dir;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (nama database (3 database))
dbname2(){
dialog –title "backup database form" –backtitle "form one database" \
–clear –inputbox "masukkan nama database " 10 40 2>/tmp/dname
pilihan=$?
case $pilihan in
0) dbname2b;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (nama database (3 database))
dbname2b(){
dialog –title "backup database form" –backtitle "form one database" \
–clear –inputbox "masukkan nama database " 10 40 2>/tmp/dname2b
pilihan=$?
case $pilihan in
0) dbname2c;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (nama database (3 database))
dbname2c(){
dialog –title "backup database form" –backtitle "form one database" \
–clear –inputbox "masukkan nama database " 10 40 2>/tmp/dname2c
pilihan=$?
case $pilihan in
0) dir2;;
1) echo "stop";;
255) echo "dibatalkan";;
esac
}

# script untuk functio backup (lokasi penyimpanan database backup)
dir(){
dialog –title "backup database form" –backtitle "form one database" \
–clear –inputbox "masukkan alamat penyimpanan" 10 40 2>/tmp/dir
pilihan=$?
simnama=`cat /tmp/nama`
simpass=`cat /tmp/pass`
simpanhost=`cat /tmp/host`
simpandname=`cat /tmp/dname`
simpandir=`cat /tmp/dir`
date=`date +%y%m%d`
hapus=`rm /tmp/nama /tmp/pass /tmp/host /tmp/dname /tmp/dir /tmp/input.**`
mysqldump -u ${simnama} -p${simpass} -h ${simpanhost} ${simpandname} > \
${simpandir}/${simpandname}-${date}.sql
case $pilihan in
0) echo "backup database sukses"
$hapus;;
1) echo "stop"
$hapus;;
255) echo "dibatalkan"
$hapus;;
esac
}
menu_utama
[/sourcecode]

Dari script tersebut akan dihasilkan bentuk program seperti pada post sebelumnya (Program Bantu Pembackupan Database Menggunakan Shell Programming (Dialog Part 1)). Kemudian ada penambahan tampilan restore seperti gambar di bawah ini :

backup1

backup2

backup3backup4

backup5

backup6

-Selamat Mencoba-

0 thoughts on “Tools Backup Restore Database Menggunakan Shell Programming (Part 2 Backup Restore)

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © All rights reserved. | Newsphere by AF themes.