Saturday, September 22, 2018

Automated XLog backup - Revised version

Back in the days when I worked for a living, one of the computer guys at the plant corrected me while I was talking about disc drives in our computers - I was saying "if it fails..."  He corrected me with "Not if it fails.  When..."

Though no longer required, lots of old-school Radio Amateurs keep a log.  Some use pencil and paper, but most do it digitally (and some, like me, do both).  Belt and suspenders.


With that in mind, I have written a Linux script that will help me avoid manual re-creation of my digital Amateur Radio log from the pencil and paper copy - which dates back to 1970 - when the computer finally fails.  I use XLog, but this should work for other Linux logging programs.

You will need some basic familiarity with Linux scripting and permissions, have crontab up and running and Dropbox installed.

The general scheme is to copy the XLog log file to a safe place at least once a day.  When my computer dies (and they all will), I could re-install XLog on a replacement and retrieve the latest of my daily log file copies kept remotely by Dropbox.

The scheme has three parts:
     (1)  Linux 'crontab'
     (2)  Dropbox
     (3)  The 'xlog_backup.cmd' script


'crontab'

Linux has a very handy program that allows users to schedule events at intervals.  I will not do a tutorial on cron or crontab, but will just show you the appropriate crontab file entry, below:

#       min     hour    day     month   day of  command
#                                       week            
#    
30 * * * *                        /home/MYHOME/bin/xlog_backup.cmd


Crontab reads this file and is directed to run the command xlog_backup.cmd (in the directory given) on the half-hour of every hour of every day of every month on every day of the week.  For you old timers, that's every time Mickey's long arm points down.


Dropbox

Dropbox is a commercial file sharing and storage program.  There is a fee if you want to store lots of data, but since XLog files are relatively tiny, we can just use the free ('Basic') version.  Check out:

          https://www.dropbox.com

You will have to create an account.  The script below could possibly work with another file-sharing program that works in a similar fashion.  Your decision.  I have no commercial interest in Dropbox, but it is quite handy for things other than saving Amateur Radio log files.

One you have Dropbox running and understand how it works, make a subdirectory, perhaps named "XLog", to match the script location below.


'xlog_backup.cmd'

Here is the backup script.  This is a revised version (23 February 2019) that is slightly more logical and creates a new backup if you change your log.  You will have to edit the various lines that are unique to your application - one example is the name of XLog's log file.   XLog typically keeps this file in the hidden directory '.xlog'.  Mine is named wb5bkl.xlog.

Make a copy of the script below and edit it:

#!/bin/bash
#  /home/urhomedir/bin/xlog_backup.cmd
#
#
###  Edit each of the following for your situation:
###    urhomedir - your home directory
###    YOURCALL  - your callsign in CAPS
###    yourcall  - your callsign in lowercase
###  Note the asumptions about directory locations
###  including the Dropbox directory structure.
###  All the 'echo' statements can be commented out once
###  you've got it working to your satisfaction.
#
#
#  Define paths to xlog data file and Dropbox backup
YOURCALL_XLOG=/home/urhomedir/.xlog/yourcall.xlog
DROPBOX_XLOG=/home/urhomedir/Dropbox/Xlog/yourcall.xlog.`date +%j`
#

#  Housekeeping  -  comment this out if you want to keep them all
#  Get rid of all but the 10 most recent log backups
rm -f  $(ls -1t /home/urhomedir/Dropbox/Xlog/yourcall.xlog* | tail -n +11)

#
#
#
  First, check to see if the backup for today exists
#  If not, create it and exit
#
#
if [ ! -f $DROPBOX_XLOG ]
then
    # create it
    cp $YOURCALL_XLOG $DROPBOX_XLOG
    echo -e "\n Creating today's backup file:   yourcall.xlog.`date +%j` \n"
    #  place a note in .xlog containing the latest backup time
    date > ~/.xlog/Last_Cloud_Backup
    exit
    else
    :
fi
#
#  Or see if the logfile in Dropbox is newer than your logfile
#  If so, do nothing and quit
#
if [ $DROPBOX_XLOG -nt $YOURCALL_XLOG ]
    then
    echo -e "\n yourcall.xlog.`date +%j` exists and is newer."
    echo -e " Quitting xlog_backup.cmd...\n"
    exit
    else
    #  if the Dropbox version is older than the logfile, overwite it
    #  with the newer version
    cp $YOURCALL_XLOG $DROPBOX_XLOG
    echo -e "\n Overwriting today's backup file:   yourcall.xlog.`date +%j` \n"
    #  place a note in .xlog containing the latest backup time
    date > ~/.xlog/Last_Cloud_Backup
fi
#
exit
#


Save the edited copy in your 'bin' directory (or wherever - but match your crontab entry) and make it executable.  The script only makes one Dropbox copy per day - but overwrites that copy if your log changes within the last hour.  Safe enough for me - but you can modify the if/then routine above and the crontab entry to suit your needs.

The backup file will have the name yourcall.xlog.Julian_date, for example:  wb5bkl.xlog.147

And the the latest backup time will be noted in your .xlog directory.  Backups from at least the last 10 days will be saved.

I hope this works for you - and keeps your log(s) nice and safe.

cln - Nick
WB5BKL
Lake Buchanan