Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

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

Saturday, October 24, 2015

Linux: Finding Hams from a particular state in a list of Callsigns


Recently I wanted to extract the Texas hams from a long list of callsigns. As there were over several hundred calls in the list (and I am lazy), some automation was needed. I did it with a little Bash script and the Callsign Server at the University of Arkansas:






The callsign server at UALR can report the address the FCC has for hams in its database.



[Note: This script assumes you are familiar with Linux at the command line level and are running the Bash shell or similar]



First place the list of callsigns you need to search into a file with each call on a single line. Here is an example (add a few calls if you like):



W1AW
WB5BKL
K5HLA
W5CT



The list can be quite long. Save this as a file called:



call-list



Here is the Bash call-list.cmd script:



#!/bin/bash
# call-list.cmd by cln – WB5BKL 10/2015
#
for CALL in `cat ./call-list`
do
echo -e "\n"
echo "\$CALL="$CALL
echo -e "\n"
sleep 1
wget -O $CALL.htm http://callsign.ualr.edu/cdetail.php?call=$CALL
done
#



Copy and save this script as call-list.cmd. Make the script executable:



chmod +x call-list.cmd



Then place it in the same directory as the call-list file. I would strongly suggest an directory empty except for these two files.



Then execute the call-list.cmd:



./call-list.cmd





You should find a series of files have been created in the directory with the suffix: .htm  For example:

W1AW.htm



Then execute the following command (assuming you want only the calls that were in Texas:



grep -l "TX" *.htm



and you will see a list of the calls that have TX as part of their address field.

Or you could do this:



grep -l "TX" *.htm > TXcalls.txt



and the file TXcalls.txt will contain a list of the files that had TX as a part of the address. This list could then easily be edited to remover the “.htm” extension – and you have your list of hams in Texas.



Of course, you could use this to look for other states, or even calls that had “Extra” or “CT” or “John” as a part of their listing. Lots of possibilities here.

cln - Nick
WB5BKL