scripts/xymon/tar.client.logfiles.sh

47 lines
1.4 KiB
Bash
Raw Normal View History

#!/bin/sh
# Purpose:
# Create an XZ archive of all files between 2 dates.
# Then remove these files
# Call this script from an /var/lib/xymon/hostdata subdirectory or
# /var/lib/xymon/histlogs subdirectory.
# Vars {{{
## Enable (0) or disable (1) debug
debug=0
## Manage files of year
date_year="2019"
## Compress files between these dates
date_start="${date_year}-01-01 00:00:01"
date_end="${date_year}-12-31 23:59:59"
2020-01-07 10:49:27 +01:00
## Best XZ compression level
xz_compression_lvl="-9"
## Fatest XZ compression level
#xz_compression_lvl="-0"
## Get current directory name
current_dir=${PWD##*/}
## Archive name
2020-01-07 10:50:17 +01:00
tar_file_name="${date_year}.${current_dir}${xz_compression_lvl}.tar.xz"
# }}}
2020-01-07 10:49:27 +01:00
[ "${debug}" -eq "0" ] && printf '%b\n' "Create an archive for ${current_dir} files between ${date_start} and ${date_end} using XZ's compression level: ${xz_compression_lvl}."
## Get the list of files between the 2 dates and ignore tar files
find . -type f -newermt "${date_start}" -not -newermt "${date_end}" -not -iname "*.tar*" -print0 | tar cJf "${tar_file_name}" --null -T -
## Check previous return code and if the archive exists with size > 0
if [ "${?}" -eq "0" ] && [ -s "${tar_file_name}" ]; then
[ "${debug}" -eq "0" ] && printf '%b\n' "${tar_file_name} successfully created, the files can be deleted."
find . -type f -newermt "${date_start}" -not -newermt "${date_end}" -not -iname "*.tar*" -delete
fi
exit 0