2020-01-06 16:07:54 +01:00
#!/bin/sh
# Purpose :
# Create an XZ archive of all files between 2 dates.
# Then remove these files
2020-01-07 14:29:55 +01:00
# Call this script from an /var/lib/xymon/hostdata subdirectory or
# /var/lib/xymon/histlogs subdirectory.
2020-01-06 16:07:54 +01:00
# Vars {{{
## Enable (0) or disable (1) debug
debug = 0
2020-01-07 16:10:01 +01:00
## Colors {{{
c_redb = '\033[1;31m'
c_magentab = '\033[1;35m'
c_reset = '\033[0m'
## }}}
2020-01-06 16:07:54 +01:00
## 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"
2020-01-06 16:07:54 +01:00
## Get current directory name
current_dir = ${ PWD ##*/ }
2020-01-08 10:51:14 +01:00
## Count the number of files
match_files = $( find . -type f -newermt " ${ date_start } " -not -newermt " ${ date_end } " -not -iname "*.tar*" | wc -l)
2020-01-06 16:07:54 +01:00
## Archive name
2020-01-07 10:50:17 +01:00
tar_file_name = " ${ date_year } . ${ current_dir } ${ xz_compression_lvl } .tar.xz "
2020-01-06 16:07:54 +01:00
# }}}
2020-01-07 16:10:29 +01:00
# If archive already exists
if [ -s " ${ tar_file_name } " ] ; then
2020-01-08 10:53:05 +01:00
printf " ${ c_redb } %-6b ${ c_reset } \n " " ERROR : ${ tar_file_name } already exists (also ${ match_files } files match the expected pattern). Please manage this directory manually or remove the archive or files then restart. "
2020-01-07 16:10:29 +01:00
exit 1
fi
2020-01-07 10:49:27 +01:00
2020-01-08 10:51:14 +01:00
# If some files match
if [ ! ${ match_files } -eq "0" ] ; then
[ " ${ debug } " -eq "0" ] && printf " ${ c_magentab } %-6b ${ c_reset } \n " " DEBUG : Create an archive for ${ current_dir } files between ${ date_start } and ${ date_end } ( ${ match_files } files) using XZ's compression level : ${ xz_compression_lvl } . "
2020-01-07 10:49:27 +01:00
2020-01-08 10:51:14 +01:00
## Get the list of files between the 2 dates and ignore tar files
2020-01-08 10:55:33 +01:00
find . -type f -newermt " ${ date_start } " -not -newermt " ${ date_end } " -not -iname "*.tar*" -print0 | tar cJf " ${ tar_file_name } " --null -T -
2020-01-07 10:49:27 +01:00
2020-01-08 10:51:14 +01:00
## Check previous return code and if the archive exists with size > 0
if [ " ${ ? } " -eq "0" ] && [ -s " ${ tar_file_name } " ] ; then
[ " ${ debug } " -eq "0" ] && printf " ${ c_magentab } %-6b ${ c_reset } \n " " DEBUG : ${ tar_file_name } successfully created, the files can be deleted. "
2020-01-08 10:55:33 +01:00
find . -type f -newermt " ${ date_start } " -not -newermt " ${ date_end } " -not -iname "*.tar*" -delete
2020-01-08 10:51:14 +01:00
fi
else
[ " ${ debug } " -eq "0" ] && printf " ${ c_magentab } %-6b ${ c_reset } \n " " DEBUG : Skip ${ current_dir } , no files found between ${ date_start } and ${ date_end } . "
fi
2020-01-06 16:07:54 +01:00
exit 0