XYBRCODE

RANDOM BITS

Search Results

Database Update Script

This is a script for upgrading schema versions on a database.

Requirements

  • a folder of sql scripts with filenames in order
  • table in database called _system_ with column called schema-version
#!/bin/bash
export MYSQL_PWD=mypassword
DATABASE=mydatabase
SERVER=myhost
UPDATE_FOLDER=myfolder

cat <<EOF
**************************
*   $DATABASE  UPDATER   
**************************
EOF

for f in $(ls -1 $UPDATE_FOLDER | sort)
do
    CURRENT_VERSION=$(mysql -sN $DATABASE -h $SERVER -u root -e "SELECT \`schema-version\` FROM _system_" 2> /dev/null)
    NEXT=$(basename $f)

    if [ "$CURRENT_VERSION" -ge "$NEXT" ] ; then
        continue;
    fi

    echo "[$CURRENT_VERSION] -> [$NEXT]"

    echo "Creating Backup..."

    echo "DROP DATABASE $DATABASE;" > $CURRENT_VERSION.bak.sql
    echo "CREATE DATABASE $DATABASE;" >> $CURRENT_VERSION.bak.sql
    echo "USE $DATABASE;" >> $CURRENT_VERSION.bak.sql
    mysqldump -q --routines --skip-triggers -h $SERVER -u root $DATABASE  >> $CURRENT_VERSION.bak.sql
    EXITCODE=$?

    if [ $EXITCODE -ne 0 ] ; then
        echo "Could not backup database: review backup error and try again";
        exit;
    fi

    echo "Running Update..."
    mysql -sN $DATABASE -h $SERVER -u root -e "source /root/updates/$NEXT"
    EXITCODE=$?

    if [ $EXITCODE -ne 0 ] ; then

        echo "Attempting to restore last backup";

        mysql -sN $DATABASE -h $SERVER -u root -e "source $CURRENT_VERSION.bak.sql"
        EXITCODE=$?

        if [ $EXITCODE -ne 0 ] ; then
            echo "Restore failed: database in possible inconsistent state, review database and restore manually"
            exit;
        else
            echo "Restore Successful: review error and try again"
            exit;
        fi

    else
        echo "Update Complete"
        mysql -sN $DATABASE -h $SERVER -u root -e "UPDATE _system_ SET \`schema-version\` = $NEXT;"
        rm $CURRENT_VERSION.bak.sql
    fi

done

Ubuntu 16.04, Wildfly 10, Mysql Setup Script

UPDATE: Working on a version for Google Compute Engine ->https://goo.gl/BwqkBw

DO NOT USE FOR PRODUCTION : Firewall is disabled along with Wildfly IP filters

Runwget -q -O - https://goo.gl/NGfDVv > install.sh && chmod +x install.sh && ./install.shas root to run it

#!/bin/bash
#wget -q -O - https://goo.gl/NGfDVv > install.sh
#chmod +x install.sh
#./install.sh

echo "
****************************************
* Java Application Server Setup Script *
****************************************"

#check for root
USER=$(id -u)
if [ $USER -ne "0" ]; then
  echo "Must be root" && exit;
fi

#Collect Password
echo "Enter a password to be used throughout this setup:"
read passw

#Disable Firewall
ufw disable

#Port Forwarding
iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 8443
iptables-save > /etc/iptables.rules
cat <<EOT >> /etc/network/if-pre-up.d/iptablesload
#!/bin/sh
iptables-restore < /etc/iptables.rules
exit 0
EOT
chmod +x /etc/network/if-pre-up.d/iptablesload

#Open SSH
sed -i -e 's,without-password,yes,g' /etc/ssh/sshd_config

#Setup Dependencies 
apt-get update && apt-get --assume-yes upgrade
apt-get --assume-yes install openjdk-8-jdk-headless unzip mysql-server

#Cofig MySQL
sed -i -e 's,bind-address,#bind-address,g' /etc/mysql/mysql.conf.d/mysqld.cnf
echo "Enter MySQL root password at prompt"
mysql -D mysql -u root -p -e "UPDATE user SET host='%' WHERE user='root';"
service mysql restart

#Setup WildFly User
adduser --gecos "" --no-create-home --disabled-password --disabled-login wildfly

#Download and Install Wildfly
cd /srv
wget http://download.jboss.org/wildfly/10.0.0.Final/wildfly-10.0.0.Final.tar.gz
tar -xvzf wildfly-10.0.0.Final.tar.gz
ln -s wildfly-10.0.0.Final wildfly
rm wildfly-10.0.0.Final.tar.gz
cp /srv/wildfly/docs/contrib/scripts/init.d/wildfly-init-debian.sh /etc/init.d/wildfly
update-rc.d wildfly defaults
cp /srv/wildfly/docs/contrib/scripts/init.d/wildfly.conf /etc/default/wildfly

#Download and Install MySQL Driver
wget http://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.39.zip
unzip mysql-connector-java-5.1.39.zip
rm mysql-connector-java-5.1.39.zip
mkdir -p /srv/wildfly/modules/system/layers/base/com/mysql/main
mv mysql-connector-java-5.1.39/mysql-connector-java-5.1.39-bin.jar /srv/wildfly/modules/system/layers/base/com/mysql/main/.
ln -s /srv/wildfly/modules/system/layers/base/com/mysql/main/mysql-connector-java-5.1.39-bin.jar /srv/wildfly/modules/system/layers/base/com/mysql/main/mysql-connector-java-bin.jar
rm -r mysql-connector-java-5.1.39
cat <<EOT >> /srv/wildfly/modules/system/layers/base/com/mysql/main/module.xml
<?xml version="1.0" ?>

<module xmlns="urn:jboss:module:1.3" name="com.mysql">
    <resources>
        <resource-root path="mysql-connector-java-bin.jar"/>
    </resources>
    <dependencies>
        <module name="javax.api"/>
        <module name="javax.transaction.api"/>
    </dependencies>
</module>
EOT

#Fix Permissions
chown -R wildfly:wildfly wildfly*

#Generate keystore
keytool -genkeypair \
        -alias serverkey \
        -keyalg RSA \
        -keysize 2048 \
        -validity 3650 \
        -keystore /srv/wildfly/standalone/configuration/server.keystore \
        -keypass $passw \
        -storepass $passw \
        -dname "CN=LocalAdministrator,O=LocalDomain,C=US"
        
#Download Wildfly Password tool
wget https://dl.dropboxusercontent.com/u/356692/xybrcode/software/wpc.jar


#Config Wildfly
perl -i -pe 's/\<drivers\>/\<drivers\>
    \<driver name="mysql" module="com.mysql"\>
        \<driver-class\>com.mysql.jdbc.Driver\<\/driver-class\>
    \<\/driver\>/g' /srv/wildfly/standalone/configuration/standalone.xml
    
        
sed -i -e 's,<http-listener name="default" socket-binding="http" redirect-socket="https"/>,<http-listener name="default" socket-binding="http"/>\n\t\t<https-listener name="https-server" security-realm="ManagementRealm" socket-binding="https"/>,g' /srv/wildfly/standalone/configuration/standalone.xml
sed -i -e "s,<security-realm name=\"ManagementRealm\">,<security-realm name=\"ManagementRealm\">\n\t\t<server-identities>\n\t\t\t<ssl>\n\t\t\t\t<keystore path=\"server.keystore\" relative-to=\"jboss.server.config.dir\" keystore-password=\"$passw\" alias=\"serverkey\" key-password=\"$passw\"/>\n\t\t\t</ssl>\n\t\t</server-identities>,g" /srv/wildfly/standalone/configuration/standalone.xml
    
sed -i -e 's,<inet-address value="${jboss.bind.address.management:127.0.0.1}"/>,<any-address/>,g' /srv/wildfly/standalone/configuration/standalone.xml
sed -i -e 's,<inet-address value="${jboss.bind.address:127.0.0.1}"/>,<any-address/>,g' /srv/wildfly/standalone/configuration/standalone.xml
sed -i -e '/default-bindings/ d' /srv/wildfly/standalone/configuration/standalone.xml

sed -i -e 's,# JBOSS_HOME,JBOSS_HOME,g' /etc/default/wildfly
sed -i -e 's,/opt/wildfly,/srv/wildfly,g' /etc/default/wildfly

echo ""

echo "Adding user admin with password '$passw'"
java -jar wpc admin $passw >> /srv/wildfly/standalone/configuration/mgmt-users.properties

rm wpc.jar

#wait for service
echo -n "Starting Wildfly..."
service wildfly start

while true
do
if [[ -z $(pgrep java) ]]
then
    echo -n "." && sleep 1
else
    echo "PID : $(pgrep java)"
    break
fi
done

echo "Done. Please reboot."

Resize non-lvm disk live without reboot

After increasing the dynamic virtual disk in VMWare/Virtualbox (sdbin this example)

Re-Scan disk
echo 1 > /sys/block/sdb/device/rescan

Get Disk Info
fdisk -u /dev/sdb

pto print the partition table, copy and paste this info into a text file for reference.

Modify Partition

Delete partition withd

Create new partition withn

Create it with same start number and type but with a bigger end number.

Save changes withwthenq. (May throw warning)

Inform the kernel of changes
partx /dev/sda

Inform the OS of changes
partprobe

Resize Filesystem to take advantage of larger partition.
resize2fs /dev/sdb1