Friday, August 15, 2025

.::: Script Backup & Restore MariaDB using Confirm before Action Execute :::.

 



A. Action 


1. test restore not found
 

 
2. run backup with confirm
 


3. restore with confirm
 



B. Script 

1. Script backup with confirm
 
 root@teguhth blogconfirm]# cat backup_confirm.sh

## fill variable dbname

dbprod='teguhth'

MARIA_USER="admin" # change user
MARIA_PASS="admin"  # change password
MARIA_CMD="mysqldump -u$MARIA_USER -p$MARIA_PASS"
MARIA_ACCESS="mysql -u$MARIA_USER -p$MARIA_PASS"

## backup manual production

$MARIA_ACCESS -e "show databases"

echo "Backup database '$dbprod' with file name '${dbprod}.sql.gz'"
read -p "Do you want to Backup it? [Y/N]: " Backup_confirmation

case "$Backup_confirmation" in
    [Yy]|[Yy][Ee][Ss])
        echo "Melanjutkan proses Backup database "

        # Backup MAIN
        echo "Backup full $dbprod..."
        date
        $MARIA_CMD -CfQq --max-allowed-packet=1G --hex-blob --order-by-primary --single-transaction --routines=true --triggers=true --no-data=false $dbprod | gzip -c > ${dbprod}.sql.gz;
        date
        pwd
                ls -lh ${dbprod}.sql.gz

        echo "Backup selesai"
        date
        ;;
    [Nn]|[Nn][Oo])
        echo "Backup dibatalkan oleh user."
        exit 0
        ;;
    *)
        echo "Invalid input. Please answer with 'Y' or 'N'."
        exit 1
        ;;
esac

echo "Copyright by           : Teguh Triharto"
echo "Website                : https://www.linkedin.com/in/teguhth"
 
[root@teguhth blogconfirm]#


2. Script restore with confirm

 [root@teguhth blogconfirm]# cat restore_confirm.sh
#!/bin/bash

## backup manual secondary training

# === Konfigurasi ===
file_backup="teguhth.sql.gz"

dbprod='sampledb'

MARIA_USER="admin" # change user
MARIA_PASS="admin"  # change password
MARIA_CMD="mysql -u$MARIA_USER -p$MARIA_PASS"
MARIA_ACCESS="mysql -u$MARIA_USER -p$MARIA_PASS"

# === Cek keberadaan file backup ===
file_backup_find="$file_backup"

if [[ ! -f "$file_backup_find" ]]; then
    echo "File backup tidak ditemukan!"
    exit 1
fi

$MARIA_ACCESS -e "show databases"

echo "Archived files of '$file_backup' on $(date -r "$file_backup_find") have been found"
echo "Restore database to '$dbprod'"
read -p "Do you want to restore it? [Y/N]: " restore_confirmation

case "$restore_confirmation" in
    [Yy]|[Yy][Ee][Ss])
        echo "Melanjutkan proses restore database"

        # Restore MAIN
        echo "Drop & Create database $dbprod..."
        $MARIA_CMD -e "drop database if exists $dbprod;"
        $MARIA_CMD -e "create database $dbprod;"
        echo "Restore full $dbprod..."
        date
        gunzip -c "$file_backup_find" | $MARIA_CMD "$dbprod"
        date

        echo "Restore selesai"
        date
        ;;
    [Nn]|[Nn][Oo])
        echo "Restore dibatalkan oleh user."
        exit 0
        ;;
    *)
        echo "Invalid input. Please answer with 'Y' or 'N'."
        exit 1
        ;;
esac

echo ""
$MARIA_ACCESS -e "show databases"
echo ""
echo "Copyright by           : Teguh Triharto"
echo "Website                : https://www.linkedin.com/in/teguhth"
 
root@teguhth blogconfirm]#

 

No comments:

Post a Comment

Popular Posts