Engine23
blogBannerImage
By Engine23

Shell script to create multiple versions of current project

I was asked to take a Magento codebase, split it off into 2 different exact copies.

They also wanted each to have updated local.xml with updated database credentials.

Furthermore, they wanted the databases replicated into 2 new ones, that the local.xml would be able to connect to.

#!/bin/bash

ok=0;

while ((ok==0))
    do
    echo "Choose your installation Type:"
    echo -e "\t(p) Probes"
    echo -e "\t(e) Education"
    echo -e "\t(b) Both"
    echo -e "\t(x) Exit"
    echo -n "Please enter your choice:"
    read choice
    case $choice in
        "p"|"P")
        ok=1
            ;;
        "e"|"E")
        ok=1
            ;;
        "b"|"B")
        ok=1
            ;;
        "x"|"X")
            exit
        ;;
            *)
            echo "That is not a valid answer, p for Probes, e for Education, b for Both or x for exit"
            ;;

    esac
done


ok=0;

while ((ok==0))
    do
    echo "Do you want rysnc progress displayed:"
    echo -e "\t(y) Yes"
    echo -e "\t(n) No"
    echo -n "Please enter your choice:"
    read rsyncprogress
    case $rsyncprogress in
        "y"|"Y")
        ok=1
            ;;
        "n"|"N")
        ok=1
            ;;

            *)
            echo "y for yes n for no if you want to view the rsync progress"
            ;;

    esac
done
if [ "$rsyncprogress" == "y" ] || [ "$rsyncprogress" == "Y" ]; then
SHOW_PROGRESS='--progress'
elif [ "$rsyncprogress" == "n" ] || [ "$rsyncprogress" == "n" ]; then
SHOW_PROGRESS=''
fi
NEW_FOLDER_PATH="/var/www/vhosts"

if [ -d "$NEW_FOLDER_PATH" ]; then
    printf '%s\n' "Removing folder ($NEW_FOLDER_PATH)"
    rm -rf "$NEW_FOLDER_PATH"
fi

mkdir $NEW_FOLDER_PATH
cd $NEW_FOLDER_PATH

NEW_MYSQL_EDUCATION_USER="ca_education"
NEW_MYSQL_EDUCATION_PASS="ca_education"
NEW_MYSQL_EDUCATION_DB="ca_magento_education"
NEW_MYSQL_PROBES_USER="ca_probes"
NEW_MYSQL_PROBES_PASS="ca_probes"
NEW_MYSQL_PROBES_DB="ca_magento_probes"
ROOT_MYSQL_USER="root"
ROOT_MYSQL_PASS="caadmin"

if [ "$choice" == "b" ] || [ "$choice" == "B" ]; then
    echo "Creating both probes and education folders..."
    mkdir education
    mkdir probes
    echo "Syncing the education and probes folders using rsync"
    rsync -vru $SHOW_PROGRESS /var/www/html/ $NEW_FOLDER_PATH/education/
    rsync -vru $SHOW_PROGRESS /var/www/html/ $NEW_FOLDER_PATH/probes/
    echo "Creating a copy of the original local.xml and making an education and probes backup"
    cp $NEW_FOLDER_PATH/education/app/etc/local.xml $NEW_FOLDER_PATH/education/app/etc/local.xml-education
    cp $NEW_FOLDER_PATH/education/app/etc/local.xml $NEW_FOLDER_PATH/probes/app/etc/local.xml-probes
    echo "Swapping out the new education mysql user, password and database names in our backup of local.xml"
    sed -i "s/maguser/$NEW_MYSQL_EDUCATION_USER/g" $NEW_FOLDER_PATH/education/app/etc/local.xml-education
    sed -i "s/magpass/$NEW_MYSQL_EDUCATION_PASS/g" $NEW_FOLDER_PATH/education/app/etc/local.xml-education
    sed -i "s/magento/$NEW_MYSQL_EDUCATION_DB/g" $NEW_FOLDER_PATH/education/app/etc/local.xml-education
    echo "Swapping out the new probes mysql user, password and database names in our backup of local.xml"
    sed -i "s/maguser/$NEW_MYSQL_PROBES_USER/g" $NEW_FOLDER_PATH/probes/app/etc/local.xml-probes
    sed -i "s/magpass/$NEW_MYSQL_PROBES_PASS/g" $NEW_FOLDER_PATH/probes/app/etc/local.xml-probes
    sed -i "s/magento/$NEW_MYSQL_PROBES_DB/g" $NEW_FOLDER_PATH/probes/app/etc/local.xml-probes
    echo "Removing the original local.xml"
    rm -rf $NEW_FOLDER_PATH/education/app/etc/local.xml
    rm -rf $NEW_FOLDER_PATH/probes/app/etc/local.xml
    echo "Using our new version with updated mysql user, password and database name for both probes and education"
    cp $NEW_FOLDER_PATH/education/app/etc/local.xml-education $NEW_FOLDER_PATH/education/app/etc/local.xml
    cp $NEW_FOLDER_PATH/probes/app/etc/local.xml-probes $NEW_FOLDER_PATH/probes/app/etc/local.xml
    echo "Taking the original database for magento, and creating new databases, as well as moving the content over to both of our new databases"
    echo "New database names are $NEW_MYSQL_EDUCATION_DB and $NEW_MYSQL_PROBES_DB"
    mysql -u $ROOT_MYSQL_USER -p"$ROOT_MYSQL_PASS" magento < /var/www/html/setup_education.sql
    mysql -u $ROOT_MYSQL_USER -p"$ROOT_MYSQL_PASS" magento < /var/www/html/setup_probes.sql
elif [ "$choice" == "p" ] || [ "$choice" == "P" ]; then
    echo "Creating Probes folder"
    mkdir probes
    cd probes
    echo "Syncing the probes folders using rsync"
     rsync -vru $SHOW_PROGRESS /var/www/html/ .
     echo "Creating a copy of the original local.xml"
    cp $NEW_FOLDER_PATH/probes/app/etc/local.xml $NEW_FOLDER_PATH/probes/app/etc/local.xml-probes
    echo "Swapping out the new probes mysql user, password and database names in our backup of local.xml"
        sed -i "s/maguser/$NEW_MYSQL_PROBES_USER/g" $NEW_FOLDER_PATH/probes/app/etc/local.xml-probes
        sed -i "s/magpass/$NEW_MYSQL_PROBES_PASS/g" $NEW_FOLDER_PATH/probes/app/etc/local.xml-probes
        sed -i "s/magento/$NEW_MYSQL_PROBES_DB/g" $NEW_FOLDER_PATH/probes/app/etc/local.xml-probes
        echo "Removing the original local.xml"
    rm -rf $NEW_FOLDER_PATH/probes/app/etc/local.xml
    echo "Using our new version with updated mysql user, password and database name"
    cp $NEW_FOLDER_PATH/probes/app/etc/local.xml-probes $NEW_FOLDER_PATH/probes/app/etc/local.xml
    echo "Taking the original database for magento, and creating new databases, as well as moving the content over to our new probes database"
     echo "New database user name is $NEW_MYSQL_PROBES_USER"
    echo "New database password is $NEW_MYSQL_PROBES_PASS"
    echo "New database name is $NEW_MYSQL_PROBES_DB"
     mysql -u $ROOT_MYSQL_USER -p"$ROOT_MYSQL_PASS" magento < /var/www/html/setup_probes.sql

elif [ "$choice" == "e" ] || [ "$choice" == "E" ]; then
    echo "Creating Education Folder"
    mkdir education
    cd education
    echo "Syncing the education folders using rsync"
    rsync -vru $SHOW_PROGRESS /var/www/html/ .
    cp $NEW_FOLDER_PATH/education/app/etc/local.xml $NEW_FOLDER_PATH/education/app/etc/local.xml-education
        echo "Swapping out the new education mysql user, password and database names in our backup of local.xml"

        sed -i "s/maguser/$NEW_MYSQL_EDUCATION_USER/g" $NEW_FOLDER_PATH/education/app/etc/local.xml-education
        sed -i "s/magpass/$NEW_MYSQL_EDUCATION_PASS/g" $NEW_FOLDER_PATH/education/app/etc/local.xml-education
        sed -i "s/magento/$NEW_MYSQL_EDUCATION_DB/g" $NEW_FOLDER_PATH/education/app/etc/local.xml-education
        echo "Removing the original local.xml"
     rm -rf $NEW_FOLDER_PATH/education/app/etc/local.xml
     echo "Using our new version with updated mysql user, password and database name"
    cp $NEW_FOLDER_PATH/education/app/etc/local.xml-education $NEW_FOLDER_PATH/education/app/etc/local.xml
    echo "Taking the original database for magento, and creating new databases, as well as moving the content over to our new education database"
    echo "New database name is $NEW_MYSQL_EDUCATION_DB"
    mysql -u $ROOT_MYSQL_USER -p"$ROOT_MYSQL_PASS" magento < /var/www/html/setup_education.sql

fi