Melanjutkan artikel Saya sebelumnya mengenai Backup Seluruh Database MySQL ke File Terpisah Secara Otomatis . Kali ini Saya akan menulis bagaimana cara Backup Semua Tabel MySQL ke File Terpisah Secara Otomatis. Kembali lagi, bagi seorang Database Administrator pasting seringkali diminta mengambil data semua / beberapa tabel secara terpisah terntu untuk dilakukan analisa ataupun pengolahan. Agar tidak ribet tiap kali mengetikan perintah mysqldump, berikut Saya telah membuat skrip 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 tableDump , letakkan di /usr/bin/
vim /usr/bin/tableDump
vim /usr/bin/tableDump
lalu isikan dengan skrip dibawah ini :
Skrip untuk MySQL
[ $# -lt 4 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && echo "For example: $(basename $0) 172.0.0.1 root myDatabase /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
## [id] menginputkan password pengguna database
## [en] input database user password
if [ -z "$DB_pass" ]; then
## [id] deklarasi opsi untuk perintah mysqldump
## [en] declare option for mysqldump command
[ -n "$DB_options" ] || DB_options="--set-gtid-purged=OFF"
## [id] membuat inputan user untuk pilihan backup semua tabel atau hanya beberapa
## [en] create user input for backup options of all tables or only some
echo "[id] Pilih 1 untuk backup semua tabel pada database $DB_name atau Pilih 2 untuk backup tabel tabel yang diinginkan: [1/2]"
echo "[en] Choose 1 to backup all tables in the database $DB_name or Choose 2 to backup the desired tables : [1/2]"
## [id] validasi inputan hanya boleh 1 atau 2
## [en] input validation can only be 1 or 2
if [[ $optionBackup == 1 ]] || [[ $optionBackup == 2 ]];
## [id] menjalankan fungsi sesuai inputan pengguna
## [en] perform functions according to user input
if [[ $optionBackup == 1 ]];
TableDB=$(mysql -NBA -h localhost -u $DB_user -p$DB_pass -D $DB_name -e 'show tables')
listTable=$(IFS=, ; echo "$TableDB")
## [id] pengguna memasukkan nilai array untuk tabel apa saja yang mau di cadangkan
## [en] user inputs array value for any table to be backed up
echo "[id] Ketik nama Tabel yang ingin di Backup (gunakan spasi jika lebih dari 1) :"
echo "[en] Type the name of the Table you want to backup (use spaces if more than 1) :"
listTable=$(IFS=, ; echo "${TableDB[*]}")
## [id] opsi apabila database ingin di arsipkan menjadi file 7zip
## [en] option if the database wants to be archived into a 7zip file
[id] Apakah kamu ingin mengarsipkan backupan jadi file 7zip? [Y/n] :
[en] Do you want to archive your backup into a 7zip file? [Y/n] : " COMP
if [[ $COMP == Y ]] || [[ $COMP == y ]];
[id] Okay, data akan diarsipkan!
[en] Okay, the data will be archived!
[id] Backupan tidak diarsipkan
[en] Your backup not archived
## [id] cetak informasi memulai pencadangan
## [en] print backup start information
echo "[id] Mencadangkan table database '$DB_name' ke dalam file SQL terpisah kedalam folder='$DIR'"
echo "[en] Dumping table of database '$DB_name' into separate SQL command files into dir=$DIR"
## [id] menjalankan fungsi backup dengan kondisi pilihan pengguna
## [en] performs the backup function with the conditions of the user's choice
if [[ $COMP == Y ]] || [[ $COMP == y ]];
for str in ${TableDB[@]};
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump --host="$DB_host" --result-file="$DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql" --user="$DB_user" --password="$DB_pass" $DB_options $DB_name $str
7za a $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.7z $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
rm -f $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
du $DIR/$DB_name-$str-* -sh
for str in ${TableDB[@]};
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump --host="$DB_host" --result-file="$DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql" --user="$DB_user" --password="$DB_pass" $DB_options $DB_name $str
du $DIR/$DB_name-$str-* -sh
## [id] cetak informasi pencadangan selesai
## [en] print backup information complete
echo "[id] ($tbl_count) tabel pada database $DB_name telah dicadangkan kedalam folder=$DIR"
echo "[en] ($tbl_count) table on database $DB_name dumped into dir=$DIR"
echo "========================================================================"
#!/bin/bash
[ $# -lt 4 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && echo "For example: $(basename $0) 172.0.0.1 root myDatabase /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
DB_name=$3
DIR=$4
## [id] menginputkan password pengguna database
## [en] input database user password
if [ -z "$DB_pass" ]; then
echo
echo -n "DB password: "
read -s DB_pass
echo
fi
## [id] deklarasi opsi untuk perintah mysqldump
## [en] declare option for mysqldump command
[ -n "$DB_options" ] || DB_options="--set-gtid-purged=OFF"
[ -n "$DIR" ] || DIR=.
## [id] membuat inputan user untuk pilihan backup semua tabel atau hanya beberapa
## [en] create user input for backup options of all tables or only some
echo "[id] Pilih 1 untuk backup semua tabel pada database $DB_name atau Pilih 2 untuk backup tabel tabel yang diinginkan: [1/2]"
echo "[en] Choose 1 to backup all tables in the database $DB_name or Choose 2 to backup the desired tables : [1/2]"
read -n 1 optionBackup
echo
## [id] validasi inputan hanya boleh 1 atau 2
## [en] input validation can only be 1 or 2
while true; do
if [[ $optionBackup == 1 ]] || [[ $optionBackup == 2 ]];
then
break
else
echo '
[id] Pilih 1 atau 2 :
[en] choose 1 or 2 :
'
read -n 1 optionBackup
echo
continue
fi
done
## [id] menjalankan fungsi sesuai inputan pengguna
## [en] perform functions according to user input
if [[ $optionBackup == 1 ]];
then
TableDB=$(mysql -NBA -h localhost -u $DB_user -p$DB_pass -D $DB_name -e 'show tables')
listTable=$(IFS=, ; echo "$TableDB")
else
## [id] pengguna memasukkan nilai array untuk tabel apa saja yang mau di cadangkan
## [en] user inputs array value for any table to be backed up
echo "[id] Ketik nama Tabel yang ingin di Backup (gunakan spasi jika lebih dari 1) :"
echo "[en] Type the name of the Table you want to backup (use spaces if more than 1) :"
read -a TableDB
listTable=$(IFS=, ; echo "${TableDB[*]}")
fi
## [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 backupan jadi file 7zip? [Y/n] :
[en] Do you want to archive your backup 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] Backupan tidak diarsipkan
[en] Your backup not archived
"
fi
## [id] cetak informasi memulai pencadangan
## [en] print backup start information
echo
echo "[id] Mencadangkan table database '$DB_name' ke dalam file SQL terpisah kedalam folder='$DIR'"
echo "[en] Dumping table of database '$DB_name' into separate SQL command files into dir=$DIR"
echo
## [id] menjalankan fungsi backup dengan kondisi pilihan pengguna
## [en] performs the backup function with the conditions of the user's choice
if [[ $COMP == Y ]] || [[ $COMP == y ]];
then
tbl_count=0
for str in ${TableDB[@]};
do
(( tbl_count++ ))
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump --host="$DB_host" --result-file="$DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql" --user="$DB_user" --password="$DB_pass" $DB_options $DB_name $str
7za a $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.7z $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
rm -f $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
du $DIR/$DB_name-$str-* -sh
done
else
tbl_count=0
for str in ${TableDB[@]};
do
(( tbl_count++ ))
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump --host="$DB_host" --result-file="$DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql" --user="$DB_user" --password="$DB_pass" $DB_options $DB_name $str
du $DIR/$DB_name-$str-* -sh
done
fi
## [id] cetak informasi pencadangan selesai
## [en] print backup information complete
echo
echo "[id] ($tbl_count) tabel pada database $DB_name telah dicadangkan kedalam folder=$DIR"
echo "[en] ($tbl_count) table on database $DB_name dumped into dir=$DIR"
echo "========================================================================"
ls -l $DIR
#!/bin/bash
[ $# -lt 4 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && echo "For example: $(basename $0) 172.0.0.1 root myDatabase /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
DB_name=$3
DIR=$4
## [id] menginputkan password pengguna database
## [en] input database user password
if [ -z "$DB_pass" ]; then
echo
echo -n "DB password: "
read -s DB_pass
echo
fi
## [id] deklarasi opsi untuk perintah mysqldump
## [en] declare option for mysqldump command
[ -n "$DB_options" ] || DB_options="--set-gtid-purged=OFF"
[ -n "$DIR" ] || DIR=.
## [id] membuat inputan user untuk pilihan backup semua tabel atau hanya beberapa
## [en] create user input for backup options of all tables or only some
echo "[id] Pilih 1 untuk backup semua tabel pada database $DB_name atau Pilih 2 untuk backup tabel tabel yang diinginkan: [1/2]"
echo "[en] Choose 1 to backup all tables in the database $DB_name or Choose 2 to backup the desired tables : [1/2]"
read -n 1 optionBackup
echo
## [id] validasi inputan hanya boleh 1 atau 2
## [en] input validation can only be 1 or 2
while true; do
if [[ $optionBackup == 1 ]] || [[ $optionBackup == 2 ]];
then
break
else
echo '
[id] Pilih 1 atau 2 :
[en] choose 1 or 2 :
'
read -n 1 optionBackup
echo
continue
fi
done
## [id] menjalankan fungsi sesuai inputan pengguna
## [en] perform functions according to user input
if [[ $optionBackup == 1 ]];
then
TableDB=$(mysql -NBA -h localhost -u $DB_user -p$DB_pass -D $DB_name -e 'show tables')
listTable=$(IFS=, ; echo "$TableDB")
else
## [id] pengguna memasukkan nilai array untuk tabel apa saja yang mau di cadangkan
## [en] user inputs array value for any table to be backed up
echo "[id] Ketik nama Tabel yang ingin di Backup (gunakan spasi jika lebih dari 1) :"
echo "[en] Type the name of the Table you want to backup (use spaces if more than 1) :"
read -a TableDB
listTable=$(IFS=, ; echo "${TableDB[*]}")
fi
## [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 backupan jadi file 7zip? [Y/n] :
[en] Do you want to archive your backup 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] Backupan tidak diarsipkan
[en] Your backup not archived
"
fi
## [id] cetak informasi memulai pencadangan
## [en] print backup start information
echo
echo "[id] Mencadangkan table database '$DB_name' ke dalam file SQL terpisah kedalam folder='$DIR'"
echo "[en] Dumping table of database '$DB_name' into separate SQL command files into dir=$DIR"
echo
## [id] menjalankan fungsi backup dengan kondisi pilihan pengguna
## [en] performs the backup function with the conditions of the user's choice
if [[ $COMP == Y ]] || [[ $COMP == y ]];
then
tbl_count=0
for str in ${TableDB[@]};
do
(( tbl_count++ ))
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump --host="$DB_host" --result-file="$DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql" --user="$DB_user" --password="$DB_pass" $DB_options $DB_name $str
7za a $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.7z $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
rm -f $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
du $DIR/$DB_name-$str-* -sh
done
else
tbl_count=0
for str in ${TableDB[@]};
do
(( tbl_count++ ))
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump --host="$DB_host" --result-file="$DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql" --user="$DB_user" --password="$DB_pass" $DB_options $DB_name $str
du $DIR/$DB_name-$str-* -sh
done
fi
## [id] cetak informasi pencadangan selesai
## [en] print backup information complete
echo
echo "[id] ($tbl_count) tabel pada database $DB_name telah dicadangkan kedalam folder=$DIR"
echo "[en] ($tbl_count) table on database $DB_name dumped into dir=$DIR"
echo "========================================================================"
ls -l $DIR
Skrip untuk MariaDB
[ $# -lt 4 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && echo "For example: $(basename $0) 172.0.0.1 root myDatabase /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
## [id] menginputkan password pengguna database
## [en] input database user password
if [ -z "$DB_pass" ]; then
## [id] deklarasi opsi untuk perintah mysqldump
## [en] declare option for mysqldump command
[ -n "$DB_options" ] || DB_options=""
## [id] membuat inputan user untuk pilihan backup semua tabel atau hanya beberapa
## [en] create user input for backup options of all tables or only some
echo "[id] Pilih 1 untuk backup semua tabel pada database $DB_name atau Pilih 2 untuk backup tabel tabel yang diinginkan: [1/2]"
echo "[en] Choose 1 to backup all tables in the database $DB_name or Choose 2 to backup the desired tables : [1/2]"
## [id] validasi inputan hanya boleh 1 atau 2
## [en] input validation can only be 1 or 2
if [[ $optionBackup == 1 ]] || [[ $optionBackup == 2 ]];
## [id] menjalankan fungsi sesuai inputan pengguna
## [en] perform functions according to user input
if [[ $optionBackup == 1 ]];
TableDB=$(mysql -NBA -h $DB_host -u $DB_user -p$DB_pass -D $DB_name -e 'show tables')
listTable=$(IFS=, ; echo "$TableDB")
## [id] pengguna memasukkan nilai array untuk tabel apa saja yang mau di cadangkan
## [en] user inputs array value for any table to be backed up
echo "[id] Ketik nama Tabel yang ingin di Backup (gunakan spasi jika lebih dari 1) :"
echo "[en] Type the name of the Table you want to backup (use spaces if more than 1) :"
listTable=$(IFS=, ; echo "${TableDB[*]}")
## [id] opsi apabila database ingin di arsipkan menjadi file 7zip
## [en] option if the database wants to be archived into a 7zip file
[id] Apakah kamu ingin mengarsipkan backupan jadi file 7zip? [Y/n] :
[en] Do you want to archive your backup into a 7zip file? [Y/n] : " COMP
if [[ $COMP == Y ]] || [[ $COMP == y ]];
[id] Okay, data akan diarsipkan!
[en] Okay, the data will be archived!
[id] Backupan tidak diarsipkan
[en] Your backup not archived
## [id] cetak informasi memulai pencadangan
## [en] print backup start information
echo "[id] Mencadangkan table database '$DB_name' ke dalam file SQL terpisah kedalam folder='$DIR'"
echo "[en] Dumping table of database '$DB_name' into separate SQL command files into dir=$DIR"
## [id] menjalankan fungsi backup dengan kondisi pilihan pengguna
## [en] performs the backup function with the conditions of the user's choice
if [[ $COMP == Y ]] || [[ $COMP == y ]];
for str in ${TableDB[@]};
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump -u $DB_user -h $DB_host -p$DB_pass $DB_options $DB_name $str > $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql
7za a $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.7z $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
rm -f $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
du $DIR/$DB_name-$str-* -sh
for str in ${TableDB[@]};
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump -u $DB_user -h $DB_host -p$DB_pass $DB_options $DB_name $str > $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql
du $DIR/$DB_name-$str-* -sh
## [id] cetak informasi pencadangan selesai
## [en] print backup information complete
echo "[id] ($tbl_count) tabel pada database $DB_name telah dicadangkan kedalam folder=$DIR"
echo "[en] ($tbl_count) table on database $DB_name dumped into dir=$DIR"
echo "========================================================================"
#!/bin/bash
[ $# -lt 4 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && echo "For example: $(basename $0) 172.0.0.1 root myDatabase /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
DB_name=$3
DIR=$4
## [id] menginputkan password pengguna database
## [en] input database user password
if [ -z "$DB_pass" ]; then
echo
echo -n "DB password: "
read -s DB_pass
echo
fi
## [id] deklarasi opsi untuk perintah mysqldump
## [en] declare option for mysqldump command
[ -n "$DB_options" ] || DB_options=""
[ -n "$DIR" ] || DIR=.
## [id] membuat inputan user untuk pilihan backup semua tabel atau hanya beberapa
## [en] create user input for backup options of all tables or only some
echo "[id] Pilih 1 untuk backup semua tabel pada database $DB_name atau Pilih 2 untuk backup tabel tabel yang diinginkan: [1/2]"
echo "[en] Choose 1 to backup all tables in the database $DB_name or Choose 2 to backup the desired tables : [1/2]"
read -n 1 optionBackup
echo
## [id] validasi inputan hanya boleh 1 atau 2
## [en] input validation can only be 1 or 2
while true; do
if [[ $optionBackup == 1 ]] || [[ $optionBackup == 2 ]];
then
break
else
echo '
[id] Pilih 1 atau 2 :
[en] choose 1 or 2 :
'
read -n 1 optionBackup
echo
continue
fi
done
## [id] menjalankan fungsi sesuai inputan pengguna
## [en] perform functions according to user input
if [[ $optionBackup == 1 ]];
then
TableDB=$(mysql -NBA -h $DB_host -u $DB_user -p$DB_pass -D $DB_name -e 'show tables')
listTable=$(IFS=, ; echo "$TableDB")
else
## [id] pengguna memasukkan nilai array untuk tabel apa saja yang mau di cadangkan
## [en] user inputs array value for any table to be backed up
echo "[id] Ketik nama Tabel yang ingin di Backup (gunakan spasi jika lebih dari 1) :"
echo "[en] Type the name of the Table you want to backup (use spaces if more than 1) :"
read -a TableDB
listTable=$(IFS=, ; echo "${TableDB[*]}")
fi
## [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 backupan jadi file 7zip? [Y/n] :
[en] Do you want to archive your backup 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] Backupan tidak diarsipkan
[en] Your backup not archived
"
fi
## [id] cetak informasi memulai pencadangan
## [en] print backup start information
echo
echo "[id] Mencadangkan table database '$DB_name' ke dalam file SQL terpisah kedalam folder='$DIR'"
echo "[en] Dumping table of database '$DB_name' into separate SQL command files into dir=$DIR"
echo
## [id] menjalankan fungsi backup dengan kondisi pilihan pengguna
## [en] performs the backup function with the conditions of the user's choice
if [[ $COMP == Y ]] || [[ $COMP == y ]];
then
tbl_count=0
for str in ${TableDB[@]};
do
(( tbl_count++ ))
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump -u $DB_user -h $DB_host -p$DB_pass $DB_options $DB_name $str > $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql
7za a $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.7z $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
rm -f $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
du $DIR/$DB_name-$str-* -sh
done
else
tbl_count=0
for str in ${TableDB[@]};
do
(( tbl_count++ ))
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump -u $DB_user -h $DB_host -p$DB_pass $DB_options $DB_name $str > $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql
du $DIR/$DB_name-$str-* -sh
done
fi
## [id] cetak informasi pencadangan selesai
## [en] print backup information complete
echo
echo "[id] ($tbl_count) tabel pada database $DB_name telah dicadangkan kedalam folder=$DIR"
echo "[en] ($tbl_count) table on database $DB_name dumped into dir=$DIR"
echo "========================================================================"
ls -l $DIR
#!/bin/bash
[ $# -lt 4 ] && echo "Usage: $(basename $0) <DB_HOST> <DB_USER> <DB_NAME> [<DIR>]" && echo "For example: $(basename $0) 172.0.0.1 root myDatabase /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
DB_name=$3
DIR=$4
## [id] menginputkan password pengguna database
## [en] input database user password
if [ -z "$DB_pass" ]; then
echo
echo -n "DB password: "
read -s DB_pass
echo
fi
## [id] deklarasi opsi untuk perintah mysqldump
## [en] declare option for mysqldump command
[ -n "$DB_options" ] || DB_options=""
[ -n "$DIR" ] || DIR=.
## [id] membuat inputan user untuk pilihan backup semua tabel atau hanya beberapa
## [en] create user input for backup options of all tables or only some
echo "[id] Pilih 1 untuk backup semua tabel pada database $DB_name atau Pilih 2 untuk backup tabel tabel yang diinginkan: [1/2]"
echo "[en] Choose 1 to backup all tables in the database $DB_name or Choose 2 to backup the desired tables : [1/2]"
read -n 1 optionBackup
echo
## [id] validasi inputan hanya boleh 1 atau 2
## [en] input validation can only be 1 or 2
while true; do
if [[ $optionBackup == 1 ]] || [[ $optionBackup == 2 ]];
then
break
else
echo '
[id] Pilih 1 atau 2 :
[en] choose 1 or 2 :
'
read -n 1 optionBackup
echo
continue
fi
done
## [id] menjalankan fungsi sesuai inputan pengguna
## [en] perform functions according to user input
if [[ $optionBackup == 1 ]];
then
TableDB=$(mysql -NBA -h $DB_host -u $DB_user -p$DB_pass -D $DB_name -e 'show tables')
listTable=$(IFS=, ; echo "$TableDB")
else
## [id] pengguna memasukkan nilai array untuk tabel apa saja yang mau di cadangkan
## [en] user inputs array value for any table to be backed up
echo "[id] Ketik nama Tabel yang ingin di Backup (gunakan spasi jika lebih dari 1) :"
echo "[en] Type the name of the Table you want to backup (use spaces if more than 1) :"
read -a TableDB
listTable=$(IFS=, ; echo "${TableDB[*]}")
fi
## [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 backupan jadi file 7zip? [Y/n] :
[en] Do you want to archive your backup 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] Backupan tidak diarsipkan
[en] Your backup not archived
"
fi
## [id] cetak informasi memulai pencadangan
## [en] print backup start information
echo
echo "[id] Mencadangkan table database '$DB_name' ke dalam file SQL terpisah kedalam folder='$DIR'"
echo "[en] Dumping table of database '$DB_name' into separate SQL command files into dir=$DIR"
echo
## [id] menjalankan fungsi backup dengan kondisi pilihan pengguna
## [en] performs the backup function with the conditions of the user's choice
if [[ $COMP == Y ]] || [[ $COMP == y ]];
then
tbl_count=0
for str in ${TableDB[@]};
do
(( tbl_count++ ))
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump -u $DB_user -h $DB_host -p$DB_pass $DB_options $DB_name $str > $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql
7za a $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.7z $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
rm -f $DIR/$DB_name-$str-`date '+%Y%m%d'`*.sql
du $DIR/$DB_name-$str-* -sh
done
else
tbl_count=0
for str in ${TableDB[@]};
do
(( tbl_count++ ))
echo "DUMPING DATABASE: $tbl_count $str"
mysqldump -u $DB_user -h $DB_host -p$DB_pass $DB_options $DB_name $str > $DIR/$DB_name-$str-`date '+%Y%m%d_%H%m%d'`.sql
du $DIR/$DB_name-$str-* -sh
done
fi
## [id] cetak informasi pencadangan selesai
## [en] print backup information complete
echo
echo "[id] ($tbl_count) tabel pada database $DB_name telah dicadangkan kedalam folder=$DIR"
echo "[en] ($tbl_count) table on database $DB_name dumped into dir=$DIR"
echo "========================================================================"
ls -l $DIR
Setelah disimpan, update permissionnya dengan perintah :
chmod +rwx /usr/bin/tableDump
chmod +rwx /usr/bin/tableDump
Jika sudah, kamu bisa menggunakan skrip tersebut dengan menjalankan
tableDump
tableDump
Prosesnya akan seperti animasi GIF dibawah in :
Backup semua tabel database lalu di arsipkan

Backup semua tabel database tanpa di arsipkan

Backup tabel-tabel tertentu lalu di arsipkan

Backup tabel-tabel tertentu tanpa di arsipkan

Okay, itulah cara backup semua tabel mysql ke file terpisah secara otomatis dengan menggunakan skrip yang telah dibuat untuk mempermudah melakukannya. Semoga informasi ini dapat bermanfaat. Terutama bagi Database Administrator yang ingin menggunakan cara cepat untuk mencadangkan tabel tabel yang di inginkan. Terima kasih banyak telah membaca 😀
Terkait
0 Komentar