Learn about Centmin Mod LEMP Stack today
Become a Member

Install Simple Mysql All Database Backup Script

Discussion in 'Centmin Mod User Tutorials & Guides' started by brijendrasial, Jun 14, 2023.

  1. brijendrasial

    brijendrasial Active Member

    202
    147
    43
    Mar 21, 2018
    Ratings:
    +229
    Local Time:
    2:07 AM
    1.13.9
    10.0.22-MariaDB
    Code:
    #!/bin/bash
    
    # Mysql Backup Script CentMinMod.
    
    # Scripted by Brijendra Sial @ Bullten Web Hosting Solutions [https://www.bullten.com]
    
    RED='\033[01;31m'
    RESET='\033[0m'
    GREEN='\033[01;32m'
    YELLOW='\e[93m'
    WHITE='\e[97m'
    BLINK='\e[5m'
    
    #set -e
    #set -x
    
    if  rpm -q pv > /dev/null ; then
    echo " "
    else
    yum install pv -y
    fi
    
    BACKUP_PATH_DEFINED=/home/mysql
    TIME=$(date +"%m_%d_%Y-%H.%M.%S")
    mkdir ${BACKUP_PATH_DEFINED}/${TIME}
    
    function BACKUP_ALL_DB
    {
            dbs=$(mysql -u root -B -N -e 'show databases;' | egrep -v '^mysql|_schema$')
                    if [ $? = '0' ]; then
                            for db in $dbs; do
                                            /usr/bin/mysqldump -u root $db | pv | gzip > $BACKUP_PATH_DEFINED/$TIME/$db.sql.gz
                                            x=$((x + 1))
                                            echo -e $GREEN"Database Backup Completed in $BACKUP_PATH_DEFINED/$TIME/$db.sql.gz"$RESET
                                            echo " "
                                    done
                    else
                            echo -e $RED"Database Connection Failed. Please check Your Database Password in /root/.my.cnf File"$RESET
                            echo " "
                            exit
                    fi
    
    }
    
    BACKUP_ALL_DB