Disclaimer: I, like many, am not a programmer, but I can grasp things with help.
I'm trying to develop yet another way to copy files from a server to external media. I want to be able to test whether it's running from cron or the command line. The command line test seems fairly obvious: If the script is run manually with no arguments then show usage. (I'll work out how to restrict this to a date format before I put it in production.)
Below is the first step that creates a directory structure to move the archive to. It's incomplete still but the idea is that it will create a date-based directory name. It seems the cron test needs to happen before the while loop.
Code:
#!/bin/bash
#
vflag=off
# Set DirDate1 from command line
DirDate1=$1
# Set DirDate2 for use from cron
DirDate2=$(date +'%Y-%m-%d')
# cron test here?
# Test whether external device is mounted and capture mount exit code
if /mnt/external/server_backups !exist then
mount /mnt/external
) test exit code to do some other stuff that I can't find my notes on
and email my phone if mount fails.
fi
while [ "$DirDate1" = "" ] # test for arguments
do # echo usage
case "$1" in
"") echo >&2 \
"
Creates backup directory structure with
date input as part of directory name, then
performs selective copy.
usage: $0 [yyyymmdd]
"
exit 1;;
esac
shift
done
# DirDate must be enclosed in double quotes
mkdir /mnt/external/server_backups/week_ending_"$DirDate"
# rsync stuff will go here when I get it figured out and tested
#
exit
So, how would you all suggest I test if the script is being launched from cron? It's probably right in front of my face, which is why I can't see it....
Thanks,
Andrew