The post Reinstall All Homebrew Packages appeared first on Justin Silver.
]]>After upgrading to OS X Sierra I was unable to start MariaDB (or MySQL). Long story short, I ended up rebuilding openssl
which fixed the signal 4 error, likely due to changes in the compiler. I decided that recompiling all the packages would be a good idea to prevent any future issues, and after some digging found this script which I modifying slightly.
#!/bin/bash # Reinstall all brew packages and dependencies in the correct order # - list all installed packages # - print the package followed by its dependencies # - print the package and a single depenency on each line # - perform a topographical sort # - use tail to reverse the order # - print out each package in the correct order on a single line # - pass to brew reinstall brew list \ | while read package; do echo -n "$package "; echo $(brew deps $package); done \ | awk 'NF == 1 {print $1, $1} NF > 1 {for (dep=1; dep<=NF; dep++) print $1, $dep}' \ | tsort \ | tail -r \ | REINSTALL_LIST=$(while read package; do echo -n "$package "; done) \ | brew reinstall $REINSTALL_LIST
The post Reinstall All Homebrew Packages appeared first on Justin Silver.
]]>The post MySQL / MariaDB Backup & Archive Script appeared first on Justin Silver.
]]>I am using MariaDB – the open source version of MySQL – and wanted an easy way to backup my databases on a regular schedule without having to do anything when I created or dropped a database. MariaDB is a drop in replacement for MySQL, so this script should work as is. I put together this base script that will use the show databases
command to iterate over each database so that it can be passed to mysqldump
. The nice
command is used to lower the impact to your regular server work.
Once exported the resulting SQL files are compressed into an archive using tar
with bzip2
compression, which saves quite a bit of space over gzip
from my tests with this kind of data. After the archive is created the source SQL files are deleted (again using nice
) and finally anything in the directory older than the specified archive days value will be deleted.
You will need to create a file called /etc/sysconfig/dbbackup
that stores the USERNAME
and PASSWORD
parameters for the script.
#/bin/bash # # MySQL/MariaDB backup script # Justin Silver # http://www.justinsilver.com # # Use cron to schedule this script to run as frequently as you want. ################################################################################### # Set properties in this file SYSCONFIG="/etc/sysconfig/dbbackup" # User with SELECT, SHOW VIEW, EVENT, and TRIGGER, or... root #USERNAME="USERNAME" #PASSWORD="PASSWORD" # Archive path ARCHIVE_PATH="/var/backups" # Archive filename ARCHIVE_FILE="databases_`date +%F_%H-%M-%S`.tbz2" # Archives older than this will be deleted ARCHIVE_DAYS="15" # Set or override config variables here if [ -f $SYSCONFIG ]; then source $SYSCONFIG fi if [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then echo "You must set USERNAME and PASSWORD in $SYSCONFIG"; exit fi # Change working directory cd $ARCHIVE_PATH # Get all of the databases for database in `mysql -u $USERNAME -p"$PASSWORD" -Bse 'show databases'`; do # Skip ones we don't want to back up if [ "performance_schema" == "$database" ]; then continue; fi if [ "information_schema" == "$database" ]; then continue; fi # Use Nice to dump the database nice mysqldump -u $USERNAME -p"$PASSWORD" --events $database > $database.sql done # Use Nice to create a tar compressed with bzip2 nice tar -cjf $ARCHIVE_FILE *.sql # Remove the SQL files nice rm -rf *.sql # Remove old archive files nice find . -mtime +$ARCHIVE_DAYS -exec rm {} \;
The post MySQL / MariaDB Backup & Archive Script appeared first on Justin Silver.
]]>The post Monitor IPSec VPN Tunnel appeared first on Justin Silver.
]]>I have an IPSec Tunnel built from one of my servers to an integration partner which is used to secure our web service calls. It uses a IPSec, OpenSwan, and Pluto to maintain a private network. Unfortunately I was seeing that this tunnel would for some reason collapse, requiring me to manually restart IPSec to rebuild the tunnel and re-enable our web services. This usually seemed to happen around 1am so despite many, many (MANY), emails, I wouldn’t actually fix it for several hours.
To aid in the process of stopping and then restarting the services, I wrote a bash script to handle all the comments. I only have one IPSec interface of ipsec0
which is used in my script. Make sure to chmod +x /usr/local/bin/ipsec-restart.sh
.
#!/bin/bash # get the -i or --interface argument value while [[ $# > 1 ]] do key="$1" case $key in -i|--interface) INTERFACE="$2" shift # past argument ;; esac shift # past argument or value done # show an error if the interface isn't specified if [ -z "$INTERFACE" ] then echo "You must provide an interface argument with -i or --interface" exit fi # restart ipsec, then bring up the IPSec tunnel /sbin/service ipsec restart /usr/sbin/ipsec whack --shutdown /usr/sbin/ipsec setup --restart /usr/sbin/ipsec auto --add $INTERFACE sleep 5 /usr/sbin/ipsec auto --up $INTERFACE
Next step is to have the system automatically run the script when the tunnel goes down. Using NetCat (nc
) is a good option for this – it can actually do a crazy number of things I won’t go into here. Basically we want to test the hostname of our service to see if we can open port 80, and if not, run the restart script. Passing in -w 10
tells it to wait 10 seconds to time out. By redirecting the output we can have this show nothing if it connects successfully, but email the address specified in the MAILTO
with the ipsec-restart.sh
output. Run this script every 5 minutes (and as root) by adding it to crontab while logged in as root, or using sudo crontab -e
to edit.
# Monitor VPN MAILTO="[email protected]" */5 * * * * ( nc -w 10 -z hostname.webservice.com 80 ) >& /dev/null || /usr/local/bin/ipsec-restart.sh -i ipsec0
The post Monitor IPSec VPN Tunnel appeared first on Justin Silver.
]]>The post Install Jenkins on CentOS as a Service appeared first on Justin Silver.
]]>Updated post: Yum Install Jenkins as a Service on CentOS 7. You probably want to do it this way, it’s much easier. Just saying.
This guide describes how to install Jenkins on your CentOS server as an init.d
service. Jenkins is a great continuous integration server that can do a lot of cool automation, but I won’t go into all of those details here. If you don’t have Java or ANT
, you should install them before getting started – I am usingĀ java-1.7.0-openjdk.x86_64
but choose the appropriate JVM
for your server, either the OpenJDK
or Oracle JVM
is required.
yum -y install java-1.7.0-openjdk.x86_64 ant
Now on to the actual setup. All of the uppercase variables in the text below can be set in the /etc/sysconfig/jenkins
file. There are defaults for all values, so only create this file if you want to override them for your own environment. First let’s create the JENKINS_USER
.
groupadd jenkins useradd -g jenkins jenkins
Next create your JENKINS_HOME
and install Jenkins by downloading the latest version of the jenkins.war
.
sudo mkdir -p /usr/local/jenkins cd /usr/local/jenkins wget http://mirrors.jenkins-ci.org/war/latest/jenkins.war
Now create the scripts to start and stop the process, called /usr/local/start-jenkins.sh
and /usr/local/stop-jenkins.sh
respectively. Both of these files should be in the JENKINS_HOME
.
/usr/local/jenkins/start-jenkins.sh
#!/bin/bash # import sysconfig settings and set defaults [ -f /etc/sysconfig/jenkins ] && . /etc/sysconfig/jenkins [ "${JENKINS_HOME}" == "" ] && JENKINS_HOME=/usr/local/jenkins [ "${JENKINS_LOG}" == "" ] && JENKINS_LOG=/home/jenkins/jenkins.log [ "${JENKINS_JAVA}" == "" ] && JENKINS_JAVA=/usr/bin/java [ "${JENKINS_JAVAOPTS}" == "" ] && JENKINS_JAVAOPTS="" [ "${JENKINS_IP}" == "" ] && JENKINS_IP=0.0.0.0 [ "${JENKINS_PORT}" == "" ] && JENKINS_PORT=8080 [ "${JENKINS_ARGS}" == "" ] && JENKINS_ARGS="" JENKINS_WAR=${JENKINS_HOME}/jenkins.war # check for config errors JENKINS_ERRORS=() [ ! -f ${JENKINS_WAR} ] && JENKINS_ERRORS[${#JENKINS_ERRORS[*]}]="JENKINS_HOME : The jenkins.war could not be found at ${JENKINS_HOME}/jenkins.war" [ ! -f $JENKINS_JAVA ] && JENKINS_ERRORS[${#JENKINS_ERRORS[*]}]="JENKINS_JAVA : The java executable could not be found at $JENKINS_JAVA" # display errors if there are any, otherwise start the process if [ ${#JENKINS_ERRORS[*]} != '0' ] then echo "CONFIGURATION ERROR:" echo " The following errors occurred when starting Jenkins." echo " Please set the appropriate values at /etc/sysconfig/jenkins" echo "" for (( i=0; i<${#JENKINS_ERRORS[*]}; i++ )) do echo "${JENKINS_ERRORS[${i}]}" done echo "" exit 1 else echo "starting service" echo "nohup nice $JENKINS_JAVA $JENKINS_JAVAOPTS -jar $JENKINS_WAR --httpListenAddress=$JENKINS_IP --httpPort=$JENKINS_PORT $> $JENKINS_LOG 2>&1 &" nohup nice $JENKINS_JAVA $JENKINS_JAVAOPTS -jar $JENKINS_WAR --httpListenAddress=$JENKINS_IP --httpPort=$JENKINS_PORT $> $JENKINS_LOG 2>&1 & fi
/usr/local/jenkins/stop-jenkins.sh
#!/bin/bash kill `ps -ef | grep [j]enkins.war | awk '{ print $2 }'`
Now create a script called /etc/init.d/jenkins
to allow the server to be started and stopped as a service.
/etc/init.d/jenkins
#! /bin/bash # chkconfig: 2345 90 10 # description: Jenkins Continuous Integration server # processname: /usr/local/jenkins/jenkins.war # Source function library. . /etc/rc.d/init.d/functions # Get network sysconfig. . /etc/sysconfig/network # Check that networking is up, otherwise we can't start [ "${NETWORKING}" = "no" ] && exit 0 # Get the Jenkins sysconfig [ -f /etc/sysconfig/jenkins ] && . /etc/sysconfig/jenkins [ "${JENKINS_HOME}" = "" ] && JENKINS_HOME=/usr/local/jenkins [ "${JENKINS_USER}" == "" ] && JENKINS_USER=jenkins startup=${JENKINS_HOME}/start-jenkins.sh shutdown=${JENKINS_HOME}/stop-jenkins.sh export JAVA_HOME=/usr/local/java/ start(){ echo -n $"Starting Jenkins service: " pid=`ps -ef | grep [j]enkins.war | wc -l` if [ $pid -gt 0 ]; then echo "Jenkins is already running" exit 1 fi su - $JENKINS_USER -c $startup RETVAL=$? [ $RETVAL == 0 ] && echo "Jenkins was started successfully." || echo "There was an error starting Jenkins." } stop(){ action $"Stopping Jenkins service: " pid=`ps -ef | grep [j]enkins.war | wc -l` if [ ! $pid -gt 0 ]; then echo "Jenkins is not running" exit 1 fi su - $JENKINS_USER -c $shutdown RETVAL=$? [ $RETVAL == 0 ] && echo "Jenkins was stopped successfully." || echo "There was an error stopping Jenkins." } status(){ pid=`ps -ef | grep [j]enkins.war | wc -l` if [ $pid -gt 0 ]; then echo "Jenkins is running..." else echo "Jenkins is stopped..." fi } restart(){ stop sleep 5 start } # Call functions as determined by args. case "$1" in start) start;; stop) stop;; status) status;; restart) restart;; *) echo $"Usage: $0 {start|stop|status|restart}" exit 1 esac exit 0
Make sure that the JENKINS_USER
is the owner of the JENKINS_HOME
directory and scripts, and that the scripts have executable flags set.
chown -R jenkins. /usr/local/jenkins chmod a+x /usr/local/jenkins/start-jenkins.sh chmod a+x /usr/local/jenkins/stop-jenkins.sh chmod a+x /etc/init.d/jenkins
You can then start, stop, restart, and check the status of the service via the following commands, or control if it is launched on boot using chkconfig
:
service jenkins status service jenkins start service jenkins restart service jenkins stop chkconfig jenkins on
The additional variables that can be set at /etc/sysconfig/jenkins
and their defaults are as follows:
# Jenkins system configuration JENKINS_HOME=/usr/local/jenkins JENKINS_USER=jenkins JENKINS_LOG=/home/jenkins/jenkins.log JENKINS_JAVA=/usr/bin/java JENKINS_JAVAOPTS="" JENKINS_IP=0.0.0.0 JENKINS_PORT=8080 JENKINS_ARGS=""
Since my development server already has Apache running on it, the easiest way to expose the application to the world was via a reverse proxy after I installed Jenkins. The VirtualHost
for this subdomain ends up looking like the following:
<VirtualHost *:80> ServerName jenkins.doublesharp.com DocumentRoot /var/www/html ProxyPass / http://localhost:8080/ nocanon ProxyPassReverse / http://localhost:8080/ ProxyRequests Off ProxyPreserveHost On <Proxy http://localhost:8080/*> Order deny,allow Allow from all </Proxy> # If using reverse proxy from SSL #Header edit Location ^http: https: </VirtualHost>
Complete the setup by restarting Apache.
service httpd restart
The post Install Jenkins on CentOS as a Service appeared first on Justin Silver.
]]>