Skip to content

Instantly share code, notes, and snippets.

@makadev
Created July 6, 2017 12:57
Show Gist options
  • Save makadev/b2c01ca6dc344abcca234be4a196ba55 to your computer and use it in GitHub Desktop.
Save makadev/b2c01ca6dc344abcca234be4a196ba55 to your computer and use it in GitHub Desktop.
Bash script for searching (and creating) a list of files that where modified since the last run.
#!/bin/bash
############
## CONFIG ##
############
# SET SN_NAMESPACE - will be used to create filenames and reduce possible collusions
# SET SN_DIR - directory to scan
# (OPTIONAL) SET SN_DB_FILE - will be used to create the db file which holds the last updated files
# (OPTIONAL) SET SN_GUARD_FILE - will be used to create the run guard/lock file to stop multiple instances of this script
# (OPTIONAL) SET SN_PTR_FILE - will be used to create the pointer file which has the date reference for other files
SN_FNNAME="${SN_NAMESPACE:-scan_newest}"
SN_SCANDIR="${SN_DIR:-/}"
SN_DEFAULT_GUARD_FILE="/var/lock/${SN_FNNAME}.lock"
SN_DEFAULT_DB_FILE="/var/lib/${SN_FNNAME}.lst"
SN_DEFAULT_PTR_FILE="/var/lib/${SN_FNNAME}.ptr"
SN_GUARD_FILE="${SN_GUARD_FILE:-$SN_DEFAULT_GUARD_FILE}"
SN_DATABASE_FILE="${SN_DB_FILE:-$SN_DEFAULT_DB_FILE}"
SN_PTR_FILE="${SN_PTR_FILE:-$SN_DEFAULT_PTR_FILE}"
###########
## STUFF ##
###########
# from https://stackoverflow.com/a/25515370/3828957
sn_yell() { echo "$0: $*" >&2; }
sn_die() { sn_yell "$*"; exit 1; }
sn_try() { "$@" || sn_die "cannot $*"; }
## check if already running
[[ -f "$SN_GUARD_FILE" ]] && sn_die "Lockfile $SN_GUARD_FILE exists, you may need to clean up a failed run or check instance collusions."
## create guard file and check again for (unlikely) collusions
SN_MODERATELY_RANDOM_NUMBER=$(($RANDOM + ($RANDOM % 2) * 32768))
echo $SN_MODERATELY_RANDOM_NUMBER > "$SN_GUARD_FILE"
grep -q "$SN_MODERATELY_RANDOM_NUMBER" "$SN_GUARD_FILE";
[ $? -eq 0 ] && echo "starting" || sn_die "Lockfile $SN_GUARD_FILE already exists, there may be another instance running."
## check if pointer file exists, if not create it
## in any case set the current pointer file for the next scan
if [ ! -f "$SN_PTR_FILE" ]; then
touch "$SN_PTR_FILE"
SN_PTR_CREATED=1
else
SN_PTR_CREATED=0
fi
## create a list of current files OR if pointer file exists, a list
## of files newer than the pointer
if [ $SN_PTR_CREATED -gt 0 ]; then
find "$SN_SCANDIR" -type f -fprint "$SN_DATABASE_FILE"
else
find "$SN_SCANDIR" -type f -newer "$SN_PTR_FILE" -fprint "$SN_DATABASE_FILE"
fi
## cleanup
echo "stopping"
grep -q "$SN_MODERATELY_RANDOM_NUMBER" "$SN_GUARD_FILE";
[ $? -eq 0 ] && rm $SN_GUARD_FILE || sn_yell "Lockfile $SN_GUARD_FILE not deleted!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment