Jul 092016
 

Since moving Docker containers is easy, and Amazon keeps on patching and rebooting my little AWS server, time to put the theory into practice and move to some other place.

Moving was 4 parts:

  • Copy the data volumes over
  • Change DNS
  • Configure the load balancer
  • Start everything in the new place

The first one needed some scripting since moving data containers in Docker is not a built-in function. And that needs a corresponding restore function of course. Here the scripts, first backup:

#!/bin/bash
# Does a backup from one or many Docker volumes
# Harald Kubota 2016-07-09

if [[ $# -eq 0 ]] ; then
 echo "Usage: $0 docker_volume_1 [docker_volume_2 ...]"
 echo "will create tar.bz2 files of the docker volumes listed"
 exit 10
fi

today=`date +%Y-%m-%d`

for vol in "$@" ; do

 backup_file=$vol-${today}.tar.bz2
 echo "Backup up docker volume $i into $backup_file"
 docker run --rm -v $vol:/voldata -v $(pwd):/backup debian:8 \
 tar cf /backup/$vol.tar.tmp /voldata
 bzip2 <$vol.tar.tmp >$backup_file && rm -f $vol.tar.tmp
done

and here restore:

#!/bin/bash
# Restore a tar dump back into a Docker data volume
# tar file can be .tar or .tar.bz2
# Name is always VOLUME-NAME-20YY-MM-DD.tar[.bz2]
# Harald Kubota 2016-07-09

if [[ $# -ne 1 ]] ; then
 echo "Usage: $0 tarfile"
 echo "will create a new volume derived from the tarfile name and restore the tarfile data into it"
 exit 10
fi

today=`date +%Y-%m-%d`

for i in "$@" ; do

 volumename=$(echo $i | sed 's/\(.*\)-20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]\.tar.*/\1/')
 docker volume create --name $volumename

 # if .bz2 then decompress first
 length=${#i}
 last4=${i:length-4}
 tar_name=$i
 delete_tar=0

 echo "Restoring tar file $i into volume $volumename"

 if [[ ${last4} == ".bz2" ]] ; then
 tar_name=$(basename $i .bz2)
 bunzip2 <$i >$tar_name
 delete_tar=1
 fi
 #echo "tar_name=$tar_name, delete_tar=$delete_tar"

 docker run --rm -v $volumename:/voldata -v $(pwd):/backup debian:8 \
 tar xfv /backup/$tar_name
 if [[ $delete_tar -eq 1 ]] ; then
 rm $tar_name
 fi
done

With this, moving Docker volumes is a piece of cake, and it doubles as a universal backup mechanism too.

Updating DNS is trivial, as is the Load Balancer part which maps the popular port 80 to the correct Docker instance. Starting is a simple “docker-compose up”.

Jul 022016
 
LED Clock + Arduino

4 years ago (time flies by!) I built a 6 digit 16 segment LED display. It’s using SPI and thus can be extended to many digits. It does not refresh all signals, so it’s limited in length, but it’s working for 6 digits. Which is enough for a clock.

Using Arduino as a quick check works well. Synchronizing the clock from a PC works too. There’s a problem though I did not expect: sending data to the Arduino via output redirection to /dev/ttyUSB0 fails to work. Reason is that the interface gets closes when done which breaks communication.

2 choices for the fix:

tail -f /dev/ttyUSB0 &

or

stty -F /dev/ttyUSB0 cs8 38400 ignbrk -brkint -icrnl -imaxbel \
-opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke \
noflsh -ixon -ixoff -crtscts -hupcl

The latter sets baurate too, but the important part is the -hupcl which does not close the interface when the shell is done.

So now I can synchronize the time from the PC to the Arduino via:

TZ_adjust=9;d=$(date +%s);t=$(echo "60*60*$TZ_adjust/1" | bc);echo T$(echo $d+$t | bc) >
/dev/ttyUSB0

Adjust TZ_adjust for your time zone. Also the Arduino clock is very inaccurate, so for a real clock, get a proper RTC with a good and more accurate crystal. The one I have is off by 900ppm (about 3s too fast per hour).