Join the community today
Register Now

Reload Script

Discussion in 'Feature Requests & Suggestions' started by Jimmy, Mar 5, 2018.

  1. Jimmy

    Jimmy Well-Known Member

    1,778
    388
    83
    Oct 24, 2015
    East Coast USA
    Ratings:
    +987
    Local Time:
    4:17 PM
    I was reading over a couple of @eva2000 posts and I noticed some instances of "on new installs" which means, if I understand correctly, is only available for new installs.

    Maybe this isn't possible or practical, but it would be really nice to have a script which reloads the entire system only keeping the custom config files, any files specified in a separate conf file, while backing up and restoring all website accounts. This would allow people who've been running systems for awhile to get the latest and greatest CMM features.

    If CMM would ever go mainstream, having something where you can basically install a fresh system keeping settings and accounts, would be super helpful since there wouldn't really have to be server troubleshooting because a "reload" would bring everything back to normal while keeping all the users custom settings and accounts.


    Sure, some people have highly customized systems, and this wouldn't be for them. But having the ability to reinstall an entire system, would be really interesting and useful.

    Just an idea.
     
  2. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,108
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,643
    Local Time:
    6:17 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    I understand totally where the request is coming from. However, it's tricky as you won't be able to get to point of 100% replicating a fresh install if there was a reload routine. As depending on the 'new' features some will involve overwriting the Centmin Mod users custom configurations or will involve breaking their web app's configured setups - especially if they go far off the default Nginx vhost configuration syntax/structure and/or do their own thing.

    As an alternative though, there are future plans for a Centmin Mod to Centmin Mod server to server Nginx vhost transfer tool/script similar to WHM/cPanel's site migration too. That would allow Centmin Mod users to setup a test 2nd VPS on a fresh or newer Centmin Mod install and copy/duplicate/migrate their sites and data from older Centmin Mod server so they can privately test the copy of their sites on new Centmin Mod version without affecting their live sites' operations on the older Centmin Mod server. Or use such a migration tool to setup staging copies of their sites for private dev work i.e. give hired devs access to the staging copy separate and isolated from their live server sites. Think of it like a much more automated version of the manual migration guide at Insight Guide - Centmin Mod Site Data Migration Guide ;) :D

    Such a site migration feature will probably start off as a Centmin Mod Premium Membership offered item to start with though :)
     
  3. Jimmy

    Jimmy Well-Known Member

    1,778
    388
    83
    Oct 24, 2015
    East Coast USA
    Ratings:
    +987
    Local Time:
    4:17 PM
    That tool sounds like it would be perfect!

    Is this something you've been working on?
     
  4. pamamolf

    pamamolf Premium Member Premium Member

    4,068
    427
    83
    May 31, 2014
    Ratings:
    +832
    Local Time:
    11:17 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    An automated way to transfer to a new Centminmod server will be great...

    My wishlist for Centminmod is:

    1)automated backup/restore of a specific domain - (files-database)
    2)Automated remote transfer of a domain - (files-database)

    :)
     
  5. Jimmy

    Jimmy Well-Known Member

    1,778
    388
    83
    Oct 24, 2015
    East Coast USA
    Ratings:
    +987
    Local Time:
    4:17 PM
    I needed a script would automate the process of restoring backups (db/dir) to my dev server. Giving me a bunch of options to change the names, etc. I'm almost done with that. It won't do backups though as that is done via scripts run by cron.

    Here is what I have so far. It's very rough and not complete. I'm not an @eva2000 level basher by any stretch. :)

    Code:
    #!/usr/bin/env bash
    ####################################################################
    #####   ACCOUNT REPLACE AND RESTORE
    ###################################################################
    
    # Clear the history in the current shell
    history -c
    
    # Set the text bold variables
    bold=$(tput bold)
    normal=$(tput sgr0)
    
    # Functions
    blankentry () {
        echo -e "\r";
        echo -e "Sorry, blank inputs result in a restart.";
        xdg-open https://www.youtube.com/watch?v=dQw4w9WgXcQ
        sleep 4;
        clear;
        exec bash "$0" "$@";
    }
    errorentry () {
        echo -e "\r";
        echo -e "$a.\n"
        echo -e "Sorry, you need to start over. Redirecting in 4 seconds.";
        sleep 6;
        clear;
        exec bash "$0" "$@";
    }
    founddb () {
        echo -e "\r";
        echo -e "We found your database backup file $DBBACKUP located on the server. All signs point to go.";
    }
    founddir () {
        echo -e "\r";
        echo -e "We found your directory backup file $DIRBACKUP located on the server. Mission control, we are good for launch.";
    }
    
    # Colors
    RED='\033[1;31m'
    YELLOW='\033[0;33m'
    GREEN='\033[0;32m'
    NC='\033[0m' # No color
    
    # Let's have a fresh new screen.
    clear;
    
    echo -e "
    ${YELLOW}######################################################################
    ####
    #### ${bold}DEV SERVER ACCOUNT REPLACE / RESTORE${normal}${NC}${YELLOW}
    ####
    ######################################################################\n${NC}"
    
    echo -e "${bold}${RED}WARNING.  IMPROPER USE OF THIS SCRIPT CAN RESULT IN LOSS OF DATA.
    PLEASE TEST OUT THE BETA SOFTWARE BEFORE USE.  SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY.
    SOFTWARE COVERED UNDER THE GPLv3 LICENSE.${NC}${normal}
    
    ${bold}AUTHOR:${normal} ZERONUG
    ${bold}WEBSITE:${normal} https://github.com/zeronug\n"
    
    echo -e "Welcome to the backup restore tool for Centmin Mod!
    This tool will delete existing backup, directory, and restore.\n"
    
    echo -e "${bold}${RED}PLEASE NOTE:${NC}${normal}  IF YOU ENTER A BLANK FIELD, THE SCRIPT WILL START OVER.  ALL BLANK
    ENTRIES WILL RESULT IN A SCRIPT RESTART."
    
    echo -e "
    ${YELLOW}######################################################################
    ####
    #### ${bold}DOMAIN / PATHS SECTION${normal}${NC}${YELLOW}
    ####
    ######################################################################\n${NC}"
    
    # Get the domain name / cmm account
    read -p "Please enter the ${bold}DOMAIN NAME${normal} of the account to be restored (example.com): " DOMAINNAME
    
    # Check to make sure something was entered for the domain name.
    if [ "$DOMAINNAME" = '' ]; then
        blankentry;
    fi
    
    echo -e "\r"
    read -p "Where are the restore files located on the server (home, root, or custom)? " LOCATIONCHOICE
    
    # If location choice isn't home, root, or custom start the script over.
    if [ "$LOCATIONCHOICE" = '' ]; then
        blankentry;
    elif ! [[ "$LOCATIONCHOICE" =~ ^(home|root|custom)$ ]]; then
        echo -e "\r";
        echo -e "OPPS.  No correct choices were chosen, please try again.\n";
        unset LOCATIONCHOICE;
        read -p "Where are the restore files located on the server (home, root, or custom)? " LOCATIONCHOICE;
        if [ "$LOCATIONCHOICE" = '' ]; then
            blankentry;
        elif ! [[ "$LOCATIONCHOICE" =~ ^(home|root|custom)$ ]]; then
            a="Looks like you didn't enter a valid location (home, root, or custom).";
            errorentry;
        fi
    fi
    
    # Set location if it's not custom.
    FINALLOCATION=$LOCATIONCHOICE
    
    # Set custom location if user decided on location.
    if [ "$LOCATIONCHOICE" = "custom" ]; then
        echo -e "\r";
        echo -e "${bold}CUSTOM RESTORE BACKUP LOCATION${normal}\n";
        read -p "Please enter the location (/path/to/your/backups/) where your restore backup is located: " CUSTOMLOCATION;
        if [ "$CUSTOMLOCATION" = '' ]; then
            blankentry;
        elif [[ ${CUSTOMLOCATION:0:1} = "/" ]] && [[ ${CUSTOMLOCATION: -1} = "/" ]]; then
            echo -e "\r";
            echo -e "${GREEN}Excellent!${NC}  Custom backup location looks perfect.";
        else
            echo -e "\r";
            echo -e "Looks like your custom location didn't ${bold}start${normal} and ${bold}end${normal} with slashes.\n";
            usnet CUSTOMLOCATION;
            read -p "Please enter the custom backup location (/path/to/your/backups/): " CUSTOMLOCATION;
            if [ "$CUSTOMLOCATION" = '' ]; then
                blankentry;
            elif [[ ${CUSTOMLOCATION:0:1} = "/" ]] && [[ ${CUSTOMLOCATION: -1} = "/" ]]; then
                echo -e "\r";
                echo -e "${GREEN}Excellent!${NC}  Custom backup location looks perfect.  Moving on...";
            else
                a="Looks like your custom location didn't start and end with slashes.";
                errorentry;
            fi
        fi
        unset FINALLOCATION;
        FINALLOCATION=$CUSTOMLOCATION;
    fi
    
    echo -e "
    ######################################################################
    ####
    #### ${bold}BACKUP NAMES SECTION${normal}
    ####
    ######################################################################\n"
    
    # Enter the filename of the database backup to be restored.
    read -p "Please enter the ${bold}DATABASE BACKUP${normal} filename which will be used for the restore (.sql): " DBBACKUP
    
    # Get last 4 characters of the database backup filename
    DBLAST4CHAR="${DBBACKUP: -4}"
    
    # Check if database backup filename is blank or is missing sql extension
    if [ "$DBBACKUP" = '' ]; then
        blankentry;
    elif [[ "$DBLAST4CHAR" != ".sql" ]]; then
        echo -e "\r";
        echo -e "The correct extension wasn't provided (.sql).  Backup file must have .sql extension.\n";
        unset DBBACKUP;
        read -p "Please enter a valid ${bold}DATABASE BACKUP FILENAME${normal} which will be used for the restore (.sql): " DBBACKUP;
        unset DBLAST4CHAR;
        DBLAST4CHAR="${DBBACKUP: -4}";
        if [ "$DBBACKUP" = '' ]; then
            blankentry;
        elif [[ "$DBLAST4CHAR" != ".sql" ]]; then
            a="The correct extension (.sql) wasn't provided for the database backup filename.";
            errorentry;
        fi
        founddb;
    elif [ ! -f $FINALLOCATION$DBBACKUP ]; then
        echo -e "\r";
        echo -e "The ${bold}DATABASE BACKUP${normal} file could not be found on the server.\n";
        unset DBBACKUP;
        read -p "Please enter a valid ${bold}DATABASE BACKUP${normal} filename you want to restore (.tar.gz): " DBBACKUP;
        unset DBLAST4CHAR;
        DBLAST4CHAR="${DBBACKUP: -4}";
        if [ "$DBBACKUP" = '' ]; then
            blankentry;
        elif [[ "$DBLAST4CHAR" != ".sql" ]]; then
            a="The correct extension (.sql) or filename wasn't provided for the database backup filename.";
            errorentry;
        elif [ ! -f $FINALLOCATION$DBBACKUP ]; then
            a="The ${bold}DATABASE BACKUP${normal} file could not be found on the server.";
            errorentry;
        fi
        founddb;
    fi
    
    # Enter the name of the directory backup to be restored.
    echo -e "\r"
    read -p "Please enter the ${bold}DIRECTORY BACKUP${normal} filename which will be used for the restore (.tar.gz): " DIRBACKUP
    
    # Get last 4 characters of the directory backup filename
    DIRLAST4CHAR="${DIRBACKUP: -7}"
    
    # Check if directory backup filename is blank,  missing the correct file extension, or missing.
    if [ "$DIRBACKUP" = '' ]; then
        blankentry;
    elif [[ "$DIRLAST4CHAR" != ".tar.gz" ]]; then
        echo -e "\r";
        echo -e "The correct extension wasn't provided (.tar.gz).  Backup file must have (.tar.gz) extension.\n";
        unset DIRBACKUP;
        read -p "Please enter the ${bold}DIRECTORY BACKUP${normal} filename you want to restore (.tar.gz): " DIRBACKUP;
        unset DIRLAST4CHAR;
        DIRLAST4CHAR="${DIRBACKUP: -7}";
        if [ "$DIRBACKUP" = '' ]; then
            blankentry;
        elif [[ "$DIRLAST4CHAR" != ".tar.gz" ]]; then
            a="The correct extension (.tar.gz) wasn't provided for the directory backup filename.";
            errorentry;
        fi
        founddir;
    elif [ ! -f $FINALLOCATION$DIRBACKUP ]; then
        echo -e "\r";
        echo -e "The ${bold}DIRECTORY BACKUP${normal} file could not be found on the server.\n";
        unset DIRBACKUP;
        read -p "Please enter the ${bold}DIRECTORY BACKUP${normal} filename you want to restore (.tar.gz): " DIRBACKUP;
        if [ "$DIRBACKUP" = '' ]; then
            blankentry;
        elif [[ "$DIRLAST4CHAR" != ".tar.gz" ]]; then
            a="The correct extension (.tar.gz) wasn't provided for the directory backup filename.";
            errorentry;
        elif [ ! -f $FINALLOCATION$DIRBACKUP ]; then
            a="The ${bold}DIRECTORY BACKUP${normal} file could not be found on the server.";
            errorentry;
        fi
        founddir;
    fi
    
    ## PASWORD DISPLAY
    echo -e "\r"
    echo -e "
    ######################################################################
    ####
    #### ${bold}ROOT PASSWORD SECTION${normal}
    ####
    ######################################################################\n"
    
    # Ask if user wants to display passwords.
    read -p "Next is the rootpassword section. At the end of this tool a summary page will display all the information you entered.
    In some situations, people don't want passwords displayed on the screen.  If that's an issue, please select 'n' below.
    Do you want to show passwords when displaying the summary (y/n)? " PASSWORDCHOICE
    
    # Enter master password so that database can be dropped.
    echo -e "\r"
    echo -e "${bold}MYSQL ROOT PASSWORD${normal}\n"
    read -p "Please enter the ${bold}MYSQL ROOT PASSWORD${normal} for your server: " ROOTPASSWORD
    
    # Check if the root password is blank.
    if [ "$ROOTPASSWORD" = '' ]; then
        blankentry;
    fi
    
    echo -e "\r"
    echo -e "
    ######################################################################
    ####
    #### ${bold}DATABASE SECTION${normal}
    ####
    ######################################################################\n"
    
    echo -e "In the database section you'll be entering the database information
    about the old and new database.\n"
    
    read -p "How would you like to handle the existing database?
    
    A.  Delete the old database and create a new database with a new name.
    B.  Delete the old database and create a new database using the old database's name.
    C.  Only create a new database, don't delete anything.
    D.  Tell me I'm awesome!
    
    Please enter your choice (A, B, C, or D): " DATABASECHOICE
    
    if [ "$DATABASECHOICE" = '' ]; then
            blankentry;
    elif [ "$DATABASECHOICE" = "A" ]; then
        echo -e "\r";
        echo -e "We're going to delete the old database and create a new database with a new name.\n";
        echo -e "Below is a list of your existing mysql databases on this server:\n";
        mysql -e "show databases;";
        echo -e "\r";
        read -p "Please enter the name of the ${bold}MYSQL DATABASE${normal} which will be deleted: " OLDDB;
    
        # Check to see if the database entered actually exists.
        OLDDBCHECK=`mysql -sNe "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$OLDDB';"`
    
        # Check if the old mysql database is blank.
        if [ "$OLDDB" = '' ]; then
            blankentry;
        elif [ "$OLDDBCHECK" != "$OLDDB" ]; then
            echo -e "\r";
            echo -e "A valid mysql database name was not entered / doesn't exist on the server.\n";
            unset OLDDB;
            read -p "Please enter the name of an existing ${bold}OLD MYSQL DATABASE${normal} which will be deleted / replaced: " OLDDB;
            unset OLDDBCHECK;
            OLDDBCHECK=`mysql -sNe "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$OLDDB';"`;
            if [ "$OLDDB" = '' ]; then
                blankentry;
            elif [ "$OLDDBCHECK" != "$OLDDB" ]; then
                a="A valid mysql database wasn't provided.";
                errorentry;
            fi
        fi
    
        echo -e "\r";
        echo -e "Excellent.  We've found your old database and we're going to delete that database now.\n";
        read -p "We're about to delete the database named ${bold}$OLDDB${normal}, proceed (y/n)? " OLDDBAGREE;
    
        if [ "$OLDDBAGREE" = "y" ]; then
            # mysqladmin -u root -p$ROOTPASSWORD drop $OLDDB;
            echo -e "\r";
            echo -e "The database named ${bold}$OLDDB${normal} has been deleted.";
            sleep 2;
        else
            echo -e "\r";
            echo -e "I guess you decided not to delete the database.";
            echo -e "The database named ${bold}$OLDDB${normal} has NOT been deleted.";
            sleep 2;
        fi
    
        # As user if he wants to use the old database name or create a new one.
        echo -e "\r";
        echo -e "Now we're going to create a new database. \n";
        read -p "Do you want me to generate a random 8 mixed letter / number database name (y/n)? " DBNAMECREATE;
    
        if [ "$DBNAMECREATE" = "y" ]; then
            echo -e "\r";
            # bash generate random 32 character alphanumeric string (lowercase only)
            # https://gist.github.com/earthgecko/3089509
            NEWRANDOMDBNAME=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1);
            echo -e "Whoot!  We've generated ${bold}$NEWRANDOMDBNAME${normal} for you (you might want to write it down).\n";
            sleep 3;
            # mysqladmin -u root -p$ROOTPASSWORD create $NEWRANDOMDBNAME;
            FINALNEWDBNAME=$NEWRANDOMDBNAME;
            echo -e "We've created your new database named ${bold}$FINALNEWDBNAME${normal}.\n";
        else
            echo -e "\r";
            echo -e "${bold}NEW MYSQL DATABASE${normal}\n";
            read -p "Please enter a name for your new ${bold}MYSQL DATABASE${normal}: " NEWDBNAME;
            if [ "$NEWDBNAME" = '' ]; then
                blankentry;
            elif [[ $NEWDBNAME == *['!'@#\$%^\&*()_+]* ]]; then
                echo -e "\r";
                echo -e "Sorry, no special characters allowed in mysql database names.\n";
                unset NEWDBNAME;
                read -p "Please enter the name of the new ${bold}MYSQL DATABASE${normal}: " NEWDBNAME;
                if [ "$NEWDBNAME" = '' ]; then
                    blankentry;
                elif [[ $NEWDBNAME == *['!'@#\$%^\&*()_+]* ]]; then
                    a="Sorry, no special characters allowed in mysql database names.";
                    errorentry;
                fi
            fi
            sleep 2;
            # mysqladmin -u root -p$ROOTPASSWORD create $NEWRANDOMDBNAME;
            FINALNEWDBNAME=$NEWDBNAME;
            echo -e "We've created your new database named ${bold}$FINALNEWDBNAME${normal}.\n";
        fi
    elif [ "$DATABASECHOICE" = "B" ]; then
        echo -e "\r";
        echo -e "We're going to delete the old database and create a new database using the old database's name.\n";
        echo -e "Below is a list of your existing mysql databases on this server:\n";
        mysql -e "show databases;";
        echo -e "\r";
        read -p "Please enter the name of the ${bold}MYSQL DATABASE${normal} which will be deleted: " OLDDB;
    
        # Check to see if the database entered actually exists.
        OLDDBCHECK=`mysql -sNe "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$OLDDB';"`;
    
        # Check if the old mysql database is blank.
        if [ "$OLDDB" = '' ]; then
            blankentry;
        elif [ "$OLDDBCHECK" != "$OLDDB" ]; then
            echo -e "\r";
            echo -e "A valid mysql database name was not entered / doesn't exist on the server.\n";
            unset OLDDB;
            read -p "Please enter the name of an existing ${bold}OLD MYSQL DATABASE${normal} which will be deleted / replaced: " OLDDB;
            unset OLDDBCHECK;
            OLDDBCHECK=`mysql -sNe "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$OLDDB';"`;
            if [ "$OLDDB" = '' ]; then
                blankentry;
            elif [ "$OLDDBCHECK" != "$OLDDB" ]; then
                a="A valid mysql database wasn't provided.";
                errorentry;
            fi
        fi
        echo -e "\r";
        echo -e "Excellent.  We've found your old database and we're going to delete that database now.\n";
        read -p "We're about to delete the database named ${bold}$OLDDB${normal}, proceed (y/n)? " OLDDBAGREE;
    
        if [ "$OLDDBAGREE" = "y" ]; then
            echo -e "\r";
            # mysqladmin -u root -p$ROOTPASSWORD drop $OLDDB;
            echo -e "The database named ${bold}$OLDDB${normal} has been deleted.";
        else
            echo -e "\r";
            echo -e "I guess you decided not to delete the database.";
            echo -e "The database named ${bold}$OLDDB${normal} has NOT been deleted.";
            a="The script can't create a database with the same name as an existing database.";
            errorentry;
        fi
    
        # As user if he wants to use the old database name or create a new one.
        echo -e "\r";
        echo -e "Now we're going to create a new database.\n";
    
        # mysqladmin -u root -p$ROOTPASSWORD create $NEWRANDOMDBNAME;
        FINALNEWDBNAME=$OLDDB;
        echo -e "We've created your new database named ${bold}$FINALNEWDBNAME${normal}.\n";
    
    elif [ "$DATABASECHOICE" = "C" ]; then
        echo -e "\r";
        echo -e "We're going to create a new database. Nothing will be deleted.\n";
        read -p "Do you want me to generate a random 8 mixed letter / number database name (y/n)? " DBNAMECREATE;
    
        if [ "$DBNAMECREATE" = "y" ]; then
            echo -e "\r";
            # bash generate random 32 character alphanumeric string (lowercase only)
            # https://gist.github.com/earthgecko/3089509
            NEWRANDOMDBNAME=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1);
            echo -e "Whoot!  We've generated ${bold}$NEWRANDOMDBNAME${normal} for you (you might want to write it down).\n";
            sleep 3;
            # mysqladmin -u root -p$ROOTPASSWORD create $NEWRANDOMDBNAME;
            FINALNEWDBNAME=$NEWRANDOMDBNAME;
            echo -e "We've created your new database named ${bold}$FINALNEWDBNAME${normal}.\n";
        else
            echo -e "\r";
            echo -e "${bold}NEW MYSQL DATABASE${normal}\n";
            read -p "Please enter a name for your new ${bold}MYSQL DATABASE${normal}: " NEWDBNAME;
            if [ "$NEWDBNAME" = '' ]; then
                blankentry;
            elif [[ $NEWDBNAME == *['!'@#\$%^\&*()_+]* ]]; then
                echo -e "\r";
                echo -e "Sorry, no special characters allowed in mysql database names.\n";
                unset NEWDBNAME;
                read -p "Please enter the name of the new ${bold}MYSQL DATABASE${normal}: " NEWDBNAME;
                if [ "$NEWDBNAME" = '' ]; then
                    blankentry;
                elif [[ $NEWDBNAME == *['!'@#\$%^\&*()_+]* ]]; then
                    a="Sorry, no special characters allowed in mysql database names.";
                    errorentry;
                fi
            fi
            sleep 2;
            # mysqladmin -u root -p$ROOTPASSWORD create $NEWRANDOMDBNAME;
            FINALNEWDBNAME=$NEWDBNAME;
            echo -e "We've created your new database named ${bold}$FINALNEWDBNAME${normal}. Moving on...\n";
        fi
    elif [ "$DATABASECHOICE" = "D" ]; then
        while true
        do
            echo -e "You're awesome!";
            sleep 1;
        done
    else
        a="Please select either A, B, C, or D.  Entering anything else will cause an error.";
        errorentry;
    fi
    
    ## MYSQL USER
    echo -e "\r"
    echo -e "
    ######################################################################
    ####
    #### ${bold}MYSQL USER SECTION${normal}
    ####
    ######################################################################\n"
    
    echo -e "Below is a list of your existing mysql users on this server:\n\n"
    # Generate a list of current mysql users.
    echo -e "${bold}MYSQL USER${normal}\n"
    
    read -p "How would you like to handle the existing MySQL user?
    
    A.  Delete the old user and create a new MySQL user.
    B.  Use the existing MySQL user.
    C.  Create a new user.  Don't delete old user.
    
    Please enter your choice (A, B, or C): " MYSQLUSERCHOICE
    
    if [ "$MYSQLUSERCHOICE" = "A" ]; then
        echo -e "\r";
        read -p "Please enter the MySQL user you want to delete: " MYSQLUSER;
        if [ "$MYSQLUSER" = '' ]; then
            blankentry;
        fi
    
        # Check to see if the username entered actually exists.
        CURRENTUSERCHECK=`mysql -sNe "SELECT USER FROM MYSQL.USER WHERE MYSQL.USER = '$MYSQLUSER';"`
    
        if [ "$CURRENTUSERCHECK" != "$MYSQLUSER" ]; then
            echo -e "\r";
            echo -e "A valid mysql user name was not entered.\n";
            read -p "Please enter the name of the ${bold}MYSQL USER${normal} which will be deleted / replaced: " MYSQLUSER;
            CURRENTUSERCHECK=`mysql -sNe "SELECT USER FROM MYSQL.USER WHERE MYSQL.USER = '$MYSQLUSER';"`
            if [ "$MYSQLUSER" = '' ]; then
                blankentry;
            elif [ "$CURRENTUSERCHECK" != "$MYSQLUSER" ]; then
                a="A valid ${bold}MYSQL USER${normal} was not entered. Please check your spelling.";
                errorentry;
            fi
        fi
    
        # Ask if user really wants to delete the existing user.
        echo -e "\r";
        read -p "Are you sure you want to delete ${bold}$MYSQLUSER${normal} (y/n)? " MYSQLUSERDELETEAPPROVE;
    
        if [ "$MYSQLUSERDELETEAPPROVE" = "y" ]; then
    
            # We're going to drop the mysql user here.
            echo -e "\r";
            # mysql -e "DROP USER 'MYSQLUSER'@'localhost'; FLUSH PRIVILEGES;
            echo -e "MySQL user ${bold}MYSQLUSER${normal}... has been terminated.\n";
            sleep 3;
    
        else
    
            # Since the user selected no, we'll restart the script.
            echo -e "\r";
            echo -e "I guess we'll have to start over since you didn't want to delete the ${bold}MYSQL USER${normal}.\n";
            errorentry;
    
        fi
    
        # Enter the new mysql username.
        echo -e "\n${bold}NEW MYSQL USER / PASSWORD${normal}\n"
        read -p "Please enter the ${bold}NEW MYSQL USER${normal} for your new database: " NEWMYSQLUSER
    
        # Check what the user entered to see if it's blank or using special characters.
        if [ "$NEWMYSQLUSER" = '' ]; then
            blankentry;
        elif [[ $NEWMYSQLUSER == *['!'@#\$%^\&*()_+]* ]]
            echo -e "\r";
            echo -e "Sorry, no special characters allowed in mysql user names.\n";
            read -p "Please enter the name of the ${bold}NEW MYSQL USER${normal}: " NEWMYSQLUSER;
            if [ "$NEWMYSQLUSER" = '' ]; then
                blankentry;
            elif [[ $NEWMYSQLUSER == *['!'@#\$%^\&*()_+]* ]]
                a="Sorry, no special characters allowed in ${bold}MYSQL USER${normal} names. Please check your spelling next time.";
                errorentry;
            fi
        fi
    
        # Enter the new mysql password.
        read -p "Please enter the ${bold}MYSQL PASSWORD${normal} for your ${bold}NEW MYSQL USER${normal}: " NEWMYSQLPASSWORD
        if [ "$NEWMYSQLPASSWORD" = '' ]; then
            blankentry;
        fi
    
    elif [ "$MYSQLUSERCHOICE" = "B" ]; then
    
        # Use an existing user.  Don't delete any old users.
        echo -e "\r";
        read -p "Please enter the existing ${bold}MYSQL USER${normal} you want to re-use: " OLDMYSQLUSER;
        if [ "$OLDMYSQLUSER" = '' ]; then
            blankentry;
        fi
    
        # Check to see if the username entered actually exists.
        CURRENTUSERCHECK=`mysql -sNe "SELECT USER FROM MYSQL.USER WHERE MYSQL.USER = '$OLDMYSQLUSER';"`
    
        if [ "$CURRENTUSERCHECK" != "$OLDMYSQLUSER" ]; then
            echo -e "\r";
            echo -e "A valid ${bold}MYSQL USER${normal} name was not entered.\n";
            read -p "Please enter the name of the ${bold}MYSQL USER${normal} you want to use: " OLDMYSQLUSER;
            CURRENTUSERCHECK=`mysql -sNe "SELECT USER FROM MYSQL.USER WHERE MYSQL.USER = '$OLDMYSQLUSER';"`
            if [ "$OLDMYSQLUSER" = '' ]; then
                blankentry;
            elif [ "$CURRENTUSERCHECK" != "$OLDMYSQLUSER" ]; then
                a="A valid ${bold}MYSQL USER${normal} was not found on this server.";
                errorentry;
            fi
        fi
    
    elif [ "$MYSQLUSERCHOICE" = "C" ]; then
    
        # Create a new user.  Don't delete old user.
        echo -e "\r";
        read -p "Please enter the ${bold}MYSQL USER${normal} you want to create: " NEWMYSQLUSER;
        if [ "$NEWMYSQLUSER" = '' ]; then
                blankentry;
        elif [[ $NEWMYSQLUSER == *['!'@#\$%^\&*()_+]* ]]
            echo -e "\r";
            echo -e "Sorry, no special characters allowed in ${bold}MYSQL USER${normal} names.\n";
            read -p "Please enter the ${bold}MYSQL USER${normal} you want to create: " NEWMYSQLUSER;
            if [ "$NEWMYSQLUSER" = '' ]; then
                blankentry;
            elif [[ $NEWMYSQLUSER == *['!'@#\$%^\&*()_+]* ]]
                a="Sorry, no special characters allowed in ${bold}MYSQL USER${normal} names. Please check your spelling next time.";
                errorentry;
            fi
        fi
    fi
    
    ## FILES AND FOLDERS
    echo -e "\r"
    echo -e "
    ######################################################################
    ####
    #### ${bold}FILES / FOLDERS SECTION${normal}
    ####
    ######################################################################\n"
    
    echo -e "\n${bold}FILES / FOLDERS REMOVAL${normal}\n"
    
    echo -e "Since you're using Centmin Mod, we can use the domain name ($DOMAINNAME) you entered above to delete
    the files and folders at that location./n"
    read -p "Do you want to delete the files / folders for $DOMAINNAME (y/n)? " FILESFOLDERSDELETEAGREE
    
    # Delete the files and folders
    if [ "$FILESFOLDERSDELETEAGREE" = "y" ]; then
    
        # Remove files and folders
        echo -e "\r";
        # rm -rf /home/nginx/domains/$DOMAINNAME/*;
        echo -e "All done!  We deleted all the files and folders for $DOMAINNAME.\n";
        sleep 2;
    
    else
    
        # Return to script if user doesn't want to delete anything.
        echo -e "\r";
        echo -e "Files and folders were NOT removed from $DOMAINNAME. Moving on...\n";
        sleep 2;
    
    fi
    
    echo -e "
    ######################################################################
    #### ${bold}CURRENT BACKUP DATA${normal}
    ######################################################################"
    
    echo -e "\n${bold}DOMAIN / PATHS${normal}\n"
    echo -e "${bold}DOMAIN NAME${normal}: $DOMAINNAME"
    
    if [ "$LOCATIONCHOICE" = "custom" ]; then
        echo -e "${bold}CUSTOM BACKUP LOCATION${normal}: $CUSTOMLOCATION\n";
    fi
    if [ "$LOCATIONCHOICE" = "home" ]; then
        export LOCATIONHOME=/home/;
        echo -e "${bold}BACKUP LOCATION${normal}: $LOCATIONHOME\n";
    fi
    if [ "$LOCATIONCHOICE" = "root" ]; then
        export LOCATIONROOT=/root/;
        echo -e "${bold}BACKUP LOCATION${normal}: $LOCATIONROOT\n";
    fi
    
    echo -e "${bold}BACKUP NAMES${normal}\n"
    echo -e "${bold}DATABASE BACKUP NAME${normal}: $DIRBACKUP"
    echo -e "${bold}DIRECTORY BACKUP NAME${normal}: $DBBACKUP\n"
    
    if [ "$PASSWORDCHOICE" = "y" ]; then
        echo -e "${bold}MYSQL ROOT PASSWORD${normal}";
        echo -e "$ROOTPASSWORD\n";
    
        echo -e "${bold}MYSQL USER / PASSWORD${normal}"
        echo -e "${bold}USERNAME${normal}: $MYSQLUSER";
        echo -e "${bold}PASSWORD${normal}: $MYSQLPASSWORD\n";
    else
        echo -e "${bold}MYSQL ROOT / USER PASSWORDS${normal}";
        echo -e "[Passwords not displayed due to preference]\n"
    fi
    
    read -p "Is all the above data correct (y/n)? " CONT
    
    if [ "$DOMAINNAME" > 0 ] && [ "$LOCATIONROOT" > 0 ]; then
        echo -e "these are both active";
    else
        echo -e "not working \n";
    fi
    
    
    
    if [ "$CONT" = "y" ]; then
    
        read -p "Do you still want to proceed (y/n)? " ABORTORPROCEED
    
        if [ "$ABORTORPROCEED" = "n" ]; then
            echo -e "Mission Aborted.  Mission Aborted.\n"
            echo -e "Thanks for stopping by.  See yaw next time."
            exit 0
        fi
    
    echo -e "\n\n##########################################
    #### BEGIN HARD DISK FORMAT...
    ##########################################\n\n";
    
        sleep 2;
        echo -e "Ha, just kidding.\n\n";
        echo -e "Restore process will begin in 5 seconds.";
        sleep 5;
    else
        echo -e "Electricity isn't free.  Type better next time and we might save the planet.\n"
        echo -e "You can start from the beginning in 5 seconds!";
        sleep 5;
        exec bash "$0" "$@";
    fi
    
    echo -e "\n\n##########################################
    #### BEGIN PROCESS
    ##########################################\n\n";
     
  6. pamamolf

    pamamolf Premium Member Premium Member

    4,068
    427
    83
    May 31, 2014
    Ratings:
    +832
    Local Time:
    11:17 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    It looks huge for a simple files/database restore but thanks for sharing... :)
     
    Last edited: Mar 6, 2018
  7. Jimmy

    Jimmy Well-Known Member

    1,778
    388
    83
    Oct 24, 2015
    East Coast USA
    Ratings:
    +987
    Local Time:
    4:17 PM
    There are a lot of questions. Not a whole lot of actually "action". I was trying to think of every possible option and also make it for the person who might not know what they're doing (there are a lot of checks about the data entered).

    I'll post the finish product, if you want to use it you can.

    I'm also doing one I called restore_quicker. Just enter the info at the top of the script and it does everything.
     
  8. pamamolf

    pamamolf Premium Member Premium Member

    4,068
    427
    83
    May 31, 2014
    Ratings:
    +832
    Local Time:
    11:17 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    Yes i prefer the quicker one ....

    An option to restore only the database or only the files will also be good...
     
  9. Jimmy

    Jimmy Well-Known Member

    1,778
    388
    83
    Oct 24, 2015
    East Coast USA
    Ratings:
    +987
    Local Time:
    4:17 PM
    Didn't think about only the db or files. That's a good idea.
     
  10. pamamolf

    pamamolf Premium Member Premium Member

    4,068
    427
    83
    May 31, 2014
    Ratings:
    +832
    Local Time:
    11:17 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    The issue is not always both :)
     
  11. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,108
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,643
    Local Time:
    6:17 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Yeah in my spare time - lots of advance features are planned for Centmin Mod's future when I have the time and finances to do it ;) :)

    FYI, for backup, restore and transfer scripts simple is nice but without advance error checking at every step of the process, it can be dangerous.

    that's one thing I haven't looked at restoring/transfer with a new domain name/db name though. Something to think about too.
     
  12. pamamolf

    pamamolf Premium Member Premium Member

    4,068
    427
    83
    May 31, 2014
    Ratings:
    +832
    Local Time:
    11:17 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    Ok time for you to laugh :p

    After 20 minutes (NEVER use it at your server) i code this one:

    Code:
    #!/bin/bash
    # Database and files backup script
    # I always backup database as database.sql
    # Edit the 9 variables below
    DBNAME="yourdatabasename"
    DBUSER="root"
    DBPASSWORD="dbrootpassword"
    BACKUPFORFILES="/backup/yourdomain.com/05-03-2018/forumfiles_05-03-2018_Time_19-25.tgz"
    BACKUPFORDB="/backup/yourdomain.com/05-03-2018/forumdb_05-03-2018_Time_19-25.tgz"
    DBPATH="/backup/yourdomain.com/05-03-2018/"
    DBFILE="/backup/yourdomain.com/05-03-2018/database.sql"
    PUBLICFOLDER="/home/nginx/domains/yourdomain.com/"
    DELETEPUBLICFOLDER="/home/nginx/domains/yourdomain.com/public"
    
    # Extract database from backup
    if /usr/bin/tar -pxzf $BACKUPFORDB -C $DBPATH
    then
       echo "${0##*/} extract database backup worked"
    else
       echo "${0##*/} extract database backup failed"
    fi
    
    # Drop database
    if mysql --user="$DBUSER" --password="$DBPASSWORD" --execute="DROP DATABASE $DBNAME;"
    then
       echo "${0##*/} drop database worked"
    else
       echo "${0##*/} drop database backup failed"
    fi
    
    sleep 2
    
    # Create database
    if mysql --user="$DBUSER" --password="$DBPASSWORD" --execute="CREATE DATABASE $DBNAME;"
    then
       echo "${0##*/} create database worked"
    else
       echo "${0##*/} create database failed"
    fi
    
    sleep 2
    
    # Import extracted database
    if mysql --user="$DBUSER" --password="$DBPASSWORD" $DBNAME < $DBFILE
    then
       echo "${0##*/} import database worked"
    else
       echo "${0##*/} import database failed"
    fi
    
    # Delete public folder
    if rm -rf $DELETEPUBLICFOLDER
    then
       echo "${0##*/} delete public folder worked"
    else
       echo "${0##*/} delete public folder failed"
    fi
    
    sleep 2
    
    # Extract backup files
    if /usr/bin/tar -pxzf $BACKUPFORFILES -C $PUBLICFOLDER
    then
       echo "${0##*/} import database worked"
    else
       echo "${0##*/} import database failed"
    fi
    
    sleep 2
    
    # Delete database backup file
    if rm -rf $DBFILE
    then
       echo "${0##*/} delete database backup file worked"
    else
       echo "${0##*/} delete database backup file failed"
    fi
    
    echo "Restore done"
    

    Upload and use:
    WARNING: First time i code something and it make kill your server !!!!!

    So after running it come back and complain that i kill your server :p
     
    Last edited: Mar 6, 2018
  13. pamamolf

    pamamolf Premium Member Premium Member

    4,068
    427
    83
    May 31, 2014
    Ratings:
    +832
    Local Time:
    11:17 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    Ultimate plan will be:

    Backup script to generate:
    So when we generate the above we will have a separate folder for each domain and backups will be dated/timed and databases and files will be separated so we can restore what we want :)

    Then the restore script should run and ask which domain we want to restore (without the www as the way we input it when we generate it) and after that it should output the backup folders with dates.

    The user select which folder date wants and then the script output the databases and files and asks if we need to restore a database.If the user say yes then he prompt to select the database file and the restore should start....

    Then at the end when the database finish a question should arrive if the user wants to restore also the files and prompt for the backup file name and restore of files starts.....

    That's a basic idea :)
     
  14. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,108
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,643
    Local Time:
    6:17 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
  15. pamamolf

    pamamolf Premium Member Premium Member

    4,068
    427
    83
    May 31, 2014
    Ratings:
    +832
    Local Time:
    11:17 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    Will do when i code it properly without so many variables (as i must know how to get dates and some info on script) and with some checks but i didn't try hard for it and i already edit a few times the above post :)

    Do you see anything very bad that i should change?
     
  16. pamamolf

    pamamolf Premium Member Premium Member

    4,068
    427
    83
    May 31, 2014
    Ratings:
    +832
    Local Time:
    11:17 PM
    Nginx-1.25.x
    MariaDB 10.3.x
    Ok i did a few adjustments and now it works :)

    Don't know if it works as it should but it works .....I edit also my post above....

    When i code something better i will create my own thread at admins area....

    I am kindly request George to check if something is very bad and a must to not use so none of the users have issues with the above scripts please....

    Thank you
     
  17. eva2000

    eva2000 Administrator Staff Member

    53,142
    12,108
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,643
    Local Time:
    6:17 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    I won't be checking/proofing folks scripts so will leave testing up to you. Again best to start your own thread and best to use github.com to host your code so you can edit it and have version control :)
     
  18. Jimmy

    Jimmy Well-Known Member

    1,778
    388
    83
    Oct 24, 2015
    East Coast USA
    Ratings:
    +987
    Local Time:
    4:17 PM
    That's why my script above is so long... tons of checks... little actually "action". I just wanted it easy for me and anyone else.