borg backup age check with fish for monit
#!/usr/bin/fish | |
set -gx BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK yes | |
set -gx BORG_RELOCATED_REPO_ACCESS_IS_OK yes | |
# variables for the borg repo and the corressponding passphrase need to be parsed from argv | |
if set -q argv[1] | |
set -gx BORG_REPO $argv[1] | |
else | |
exit 1 | |
end | |
if set -q argv[2] | |
set -gx BORG_PASSPHRASE $argv[2] | |
else | |
exit 1 | |
end | |
# parse backup age | |
if set -q argv[3] | |
set -gx backup_age $argv[3] days ago | |
else | |
exit 1 | |
end | |
# function to make the last archive known in the repo available as json | |
function borg_get_last_archive | |
set -gx borg_last_archive (borg list $BORG_REPO --json --last 1) | |
end | |
# execute the function to actually get the last archive | |
borg_get_last_archive | |
# function to extract the time of the last archive as plain text | |
function borg_get_last_archive_time | |
set -gx borg_last_archive_time (echo $borg_last_archive | jq -r '.archives[0].time') | |
end | |
# execute the function to get the time of the last archive | |
borg_get_last_archive_time | |
# function to save the last archive time as unix epoch timestamp | |
function borg_get_last_archive_time_unix | |
set -gx borg_last_archive_time_unix (date --date=$borg_last_archive_time +%s) | |
end | |
# execute function to save last archive time as unix timestamp | |
borg_get_last_archive_time_unix | |
# define variable for max backup age | |
set -gx max_age_date_unix (date --date="$backup_age" +%s) | |
# actually compare the archive date and the max backup age. send a message accordingly | |
if [ (echo $borg_last_archive_time_unix) -gt (echo $max_age_date_unix) ] | |
echo "Backup is up-to-date." | |
else | |
echo -e "Backup is old!\nCheck backup source for borg repo $BORG_REPO" | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment