Created
March 15, 2018 18:21
-
-
Save clayadavis/e165363378a3df703d442c6cb081a860 to your computer and use it in GitHub Desktop.
Back up your crontab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -uo pipefail | |
IFS=$'\n\t' | |
if [[ $# -eq 0 ]]; then | |
echo "usage: $0 <target_directory>" | |
exit 1 | |
fi | |
TARGET_DIR=$1 | |
HOST=$(hostname --short) | |
DATE=$(date +%F) | |
FNAME="${HOST}_${DATE}.cron" | |
GLOB="${HOST}_*.cron" | |
LAST_FILE=$(find $TARGET_DIR -maxdepth 1 -name $GLOB | tail -n 1) | |
function output { | |
crontab -l > ${TARGET_DIR}/${FNAME} | |
} | |
if [[ -z "$LAST_FILE" ]]; then | |
# echo "No file found, writing file" | |
output | |
else | |
DIFF=$(crontab -l | diff -q - "$LAST_FILE") | |
if [[ -z "$DIFF" ]]; then | |
# echo "Last file is the same, touching file" | |
touch "$LAST_FILE" | |
else | |
# echo "Last file differs, writing new file" | |
output | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment