Friday, January 5, 2024

.::: Script to check File backup Database MariaDB MySQL , SQL Server MSSQL success or not in NAS or FS :::.

 1. sample for linux backup mariadb & windows sql server

db linux

db_mariadb_teguh;
db_mariadb_teguh_dmart;

db windows

db_mssql_teguh;
db_mssql_teguh_dmart;

 
2. create folder & file for simulation

2.1 create manual testing

mkdir -p /media/backup/mariadb_teguh/2023/12/21/db_mariadb_teguh
mkdir -p /media/backup/mariadb_teguh/2023/12/21/db_mariadb_teguh_dmart
mkdir -p /media/backup/mssql_teguh/2023/12/21/db_mssql_teguh
mkdir -p /media/backup/mssql_teguh/2023/12/21/db_mssql_teguh_dmart

cd /media/backup/mariadb_teguh/2023/12/21/db_mariadb_teguh
touch [FULL]_db_mariadb_teguh_20231221_000500.sql
touch [LOG]_db_mariadb_teguh_20231221_003000.sql
touch [LOG]_db_mariadb_teguh_20231221_010000.sql
touch [LOG]_db_mariadb_teguh_20231221_013000.sql
touch [LOG]_db_mariadb_teguh_20231221_020000.sql

cd /media/backup/mariadb_teguh/2023/12/21/db_mariadb_teguh_dmart
touch [FULL]_db_mariadb_teguh_dmart_20231221_000500.sql
touch [LOG]_db_mariadb_teguh_dmart_20231221_003000.sql
touch [LOG]_db_mariadb_teguh_dmart_20231221_010000.sql
touch [LOG]_db_mariadb_teguh_dmart_20231221_013000.sql
touch [LOG]_db_mariadb_teguh_dmart_20231221_020000.sql

cd /media/backup/mssql_teguh/2023/12/21/db_mssql_teguh
touch db_mssql_teguh_20231221_000000.bak
touch db_mssql_teguh_20231221_003000.trn
touch db_mssql_teguh_20231221_010000.trn
touch db_mssql_teguh_20231221_013000.trn
touch db_mssql_teguh_20231221_020000.trn

cd /media/backup/mssql_teguh/2023/12/21/db_mssql_teguh_dmart
touch db_mssql_teguh_dmart_20231221_000000.bak
touch db_mssql_teguh_dmart_20231221_003000.trn
touch db_mssql_teguh_dmart_20231221_010000.trn
touch db_mssql_teguh_dmart_20231221_013000.trn
touch db_mssql_teguh_dmart_20231221_020000.trn


 


2.2 create automatic testing base on date


mkdir -p /media/backup/mariadb_teguh/$(date +"%Y")/$(date +"%m")/$(date +"%d")/db_mariadb_teguh
mkdir -p /media/backup/mariadb_teguh/$(date +"%Y")/$(date +"%m")/$(date +"%d")/db_mariadb_teguh_dmart
mkdir -p /media/backup/mssql_teguh/$(date +"%Y")/$(date +"%m")/$(date +"%d")/db_mssql_teguh
mkdir -p /media/backup/mssql_teguh/$(date +"%Y")/$(date +"%m")/$(date +"%d")/db_mssql_teguh_dmart

cd /media/backup/mariadb_teguh/$(date +"%Y")/$(date +"%m")/$(date +"%d")/db_mariadb_teguh
touch [FULL]_db_mariadb_teguh_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").sql
touch [LOG]_db_mariadb_teguh_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").sql
touch [LOG]_db_mariadb_teguh_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").sql
touch [LOG]_db_mariadb_teguh_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").sql
touch [LOG]_db_mariadb_teguh_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").sql

cd /media/backup/mariadb_teguh/$(date +"%Y")/$(date +"%m")/$(date +"%d")/db_mariadb_teguh_dmart
touch [FULL]_db_mariadb_teguh_dmart_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").sql
touch [LOG]_db_mariadb_teguh_dmart_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").sql
touch [LOG]_db_mariadb_teguh_dmart_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").sql
touch [LOG]_db_mariadb_teguh_dmart_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").sql
touch [LOG]_db_mariadb_teguh_dmart_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").sql

cd /media/backup/mssql_teguh/$(date +"%Y")/$(date +"%m")/$(date +"%d")/db_mssql_teguh
touch db_mssql_teguh_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").bak
touch db_mssql_teguh_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").trn
touch db_mssql_teguh_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").trn
touch db_mssql_teguh_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").trn
touch db_mssql_teguh_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").trn

cd /media/backup/mssql_teguh/$(date +"%Y")/$(date +"%m")/$(date +"%d")/db_mssql_teguh_dmart
touch db_mssql_teguh_dmart_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").bak
touch db_mssql_teguh_dmart_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").trn
touch db_mssql_teguh_dmart_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").trn
touch db_mssql_teguh_dmart_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").trn
touch db_mssql_teguh_dmart_$(date +"%Y")$(date +"%m")$(date +"%d")_$(date +"%H%M%S").trn


3. check file base on query

cd /media/backup/mariadb_teguh/2023/12/20/db_mariadb_teguh
ls -lh
cd /media/backup/mariadb_teguh/2023/12/20/db_mariadb_teguh_dmart
ls -lh
cd /media/backup/mssql_teguh/2023/12/20/db_mssql_teguh
ls -lh
cd /media/backup/mssql_teguh/2023/12/20/db_mssql_teguh_dmart
ls -lh

4. create shell script check for backup file mariadb check_mariadb_teguh.sh

sh check_mariadb_teguh.sh

[root@teguhth checkback]# cat check_mariadb_teguh.sh

#!/bin/bash
year=`date +%Y`; month=`date +%m`; day=`date +%d`; hour=`date +%H`; min=`date +%M`; sec=`date +%S`; dayname=`date +%a`;

backup_dirdb_mariadb_teguh="/media/backup/mariadb_teguh/${year}/${month}/${day}/db_mariadb_teguh"
db_infodb_mariadb_teguh="db_mariadb_teguh"

backup_dirdb_mariadb_teguh_dmart="/media/backup/mariadb_teguh/${year}/${month}/${day}/db_mariadb_teguh_dmart"
db_infodb_mariadb_teguh_dmart="db_mariadb_teguh_dmart"

echo ""
echo "Client with Account Code: mariadb_teguh"
echo ""
# Check for files with "full" and "log" filters
full_files=$(find "$backup_dirdb_mariadb_teguh" -type f -name "*FULL*")
log_files=$(find "$backup_dirdb_mariadb_teguh" -type f -name "*LOG*")

# Check conditions and display appropriate messages
if [ -n "$full_files" ] && [ -n "$log_files" ]; then
    # Files with both "full" and "log" filters exist
    echo "Backup full success & backup log success in $db_infodb_mariadb_teguh"
elif [ -n "$full_files" ] && [ -z "$log_files" ]; then
    # Files with "full" filter exist, but no files with "log" filter
    echo "Backup full success & backup log failed in $db_infodb_mariadb_teguh"
elif [ -z "$full_files" ] && [ -n "$log_files" ]; then
    # Files with "log" filter exist, but no files with "full" filter
    echo "Backup full failed & backup log success in $db_infodb_mariadb_teguh"
else
    # No files with either "full" or "log" filter
    echo "Backup full & log failed >> Backup Not Found in $db_infodb_mariadb_teguh"
fi

ls -lh $backup_dirdb_mariadb_teguh | grep ".*FULL*" | head -n 1
ls -lh $backup_dirdb_mariadb_teguh | grep ".*LOG*" | head -n 1

echo ""

echo ""
# Check for files with "full" and "log" filters
full_files=$(find "$backup_dirdb_mariadb_teguh_dmart" -type f -name "*FULL*")
log_files=$(find "$backup_dirdb_mariadb_teguh_dmart" -type f -name "*LOG*")

# Check conditions and display appropriate messages
if [ -n "$full_files" ] && [ -n "$log_files" ]; then
    # Files with both "full" and "log" filters exist
    echo "Backup full success & backup log success in $db_infodb_mariadb_teguh_dmart"
elif [ -n "$full_files" ] && [ -z "$log_files" ]; then
    # Files with "full" filter exist, but no files with "log" filter
    echo "Backup full success & backup log failed in $db_infodb_mariadb_teguh_dmart"
elif [ -z "$full_files" ] && [ -n "$log_files" ]; then
    # Files with "log" filter exist, but no files with "full" filter
    echo "Backup full failed & backup log success in $db_infodb_mariadb_teguh_dmart"
else
    # No files with either "full" or "log" filter
    echo "Backup full & log failed >> Backup Not Found in $db_infodb_mariadb_teguh_dmart"
fi

ls -lh $backup_dirdb_mariadb_teguh_dmart | grep ".*FULL*" | head -n 1
ls -lh $backup_dirdb_mariadb_teguh_dmart | grep ".*LOG*" | head -n 1

echo ""

[root@teguhth checkback]#


 


5. create shell script check for backup file sql server check_mssql_teguh.sh

[root@teguhth checkback]# cat check_mssql_teguh.sh

#!/bin/bash
year=`date +%Y`; month=`date +%m`; day=`date +%d`; hour=`date +%H`; min=`date +%M`; sec=`date +%S`; dayname=`date +%a`;

backup_dirdb_mssql_teguh="/media/backup/mssql_teguh/${year}/${month}/${day}/db_mssql_teguh"
db_infodb_mssql_teguh="db_mssql_teguh"

backup_dirdb_mssql_teguh_dmart="/media/backup/mssql_teguh/${year}/${month}/${day}/db_mssql_teguh_dmart"
db_infodb_mssql_teguh_dmart="db_mssql_teguh_dmart"

echo ""
echo "Client with Account Code: mssql_teguh"
echo ""
# Check for files with "full" and "log" filters
full_files=$(find "$backup_dirdb_mssql_teguh" -type f -name "*.bak")
log_files=$(find "$backup_dirdb_mssql_teguh" -type f -name "*.trn")

# Check conditions and display appropriate messages
if [ -n "$full_files" ] && [ -n "$log_files" ]; then
    # Files with both "full" and "log" filters exist
    echo "Backup full success & backup log success in $db_infodb_mssql_teguh"
elif [ -n "$full_files" ] && [ -z "$log_files" ]; then
    # Files with "full" filter exist, but no files with "log" filter
    echo "Backup full success & backup log failed in $db_infodb_mssql_teguh"
elif [ -z "$full_files" ] && [ -n "$log_files" ]; then
    # Files with "log" filter exist, but no files with "full" filter
    echo "Backup full failed & backup log success in $db_infodb_mssql_teguh"
else
    # No files with either "full" or "log" filter
    echo "Backup full & log failed >> Backup Not Found in $db_infodb_mssql_teguh"
fi


ls -lh $backup_dirdb_mssql_teguh | grep ".bak" | head -n 1
ls -lh $backup_dirdb_mssql_teguh | grep ".trn" | head -n 1

echo ""

echo ""
# Check for files with "full" and "log" filters
full_files=$(find "$backup_dirdb_mssql_teguh_dmart" -type f -name "*.bak")
log_files=$(find "$backup_dirdb_mssql_teguh_dmart" -type f -name "*.trn")

# Check conditions and display appropriate messages
if [ -n "$full_files" ] && [ -n "$log_files" ]; then
    # Files with both "full" and "log" filters exist
    echo "Backup full success & backup log success in $db_infodb_mssql_teguh_dmart"
elif [ -n "$full_files" ] && [ -z "$log_files" ]; then
    # Files with "full" filter exist, but no files with "log" filter
    echo "Backup full success & backup log failed in $db_infodb_mssql_teguh_dmart"
elif [ -z "$full_files" ] && [ -n "$log_files" ]; then
    # Files with "log" filter exist, but no files with "full" filter
    echo "Backup full failed & backup log success in $db_infodb_mssql_teguh_dmart"
else
    # No files with either "full" or "log" filter
    echo "Backup full & log failed >> Backup Not Found in $db_infodb_mssql_teguh_dmart"
fi

ls -lh $backup_dirdb_mssql_teguh_dmart | grep ".bak" | head -n 1
ls -lh $backup_dirdb_mssql_teguh_dmart | grep ".trn" | head -n 1

echo ""
[root@teguhth checkback]#


6. create shell script to main_menu check backup  all_check_backup.sh

[root@teguhth checkback]# cat all_check_backup.sh

#!/bin/bash
clear
while true; do
date
    echo "Choose an option:"
    echo "1. Run sh check_mariadb_teguh.sh"
    echo "2. Run sh check_mssql_teguh.sh"
        echo "3. Run sh tetris_game.sh"
    echo "4. Exit"
    read -p "Enter your choice (1, 2, 3 or n): " choice

    case $choice in
        1)
            echo "1. Run sh check_mariadb_teguh.sh"
            sh check_mariadb_teguh.sh
            ;;
        2)
            echo "2. Run sh check_mssql_teguh.sh"
            sh check_mssql_teguh.sh
            ;;
        3)
            echo "3. Run sh tetris_game.sh"
            sh tetris_game.sh
            ;;
        4)
            echo "Exiting."
            exit 0
            ;;
        *)
            echo "Invalid choice. Please enter 1, 2, or 3."
            ;;
    esac
        echo "Copyright by           : Teguh Triharto"
echo "Website                : https://www.linkedin.com/in/teguhth"
echo ""

read -p "Press Enter to return to the main menu..."

clear  # Clear the terminal for the next iteration
done
[root@teguhth checkback]#

 


7. run to check result 



 

No comments:

Post a Comment

Popular Posts