From 20e39439e466708617532babd70d652832189908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Tue, 7 Jan 2020 10:49:27 +0100 Subject: [PATCH] Compress then remove files --- xymon/tar.client.logfiles.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/xymon/tar.client.logfiles.sh b/xymon/tar.client.logfiles.sh index f037e05..a95cbbf 100755 --- a/xymon/tar.client.logfiles.sh +++ b/xymon/tar.client.logfiles.sh @@ -17,10 +17,10 @@ date_year="2019" date_start="${date_year}-01-01 00:00:01" date_end="${date_year}-12-31 23:59:59" -## Best XZ compression level (= -9) -xz_compression_lvl="--best" -## Fatest XZ compression level (= -0) -#xz_compression_lvl="--fast" +## Best XZ compression level +xz_compression_lvl="-9" +## Fatest XZ compression level +#xz_compression_lvl="-0" ## Get current directory name current_dir=${PWD##*/} @@ -30,6 +30,16 @@ tar_file_name="${date_year}.${current_dir}.tar.xz" # }}} -[ "${debug}" -eq "0" ] && printf '%b' "Create an archive for ${current_dir} files between ${date_start} and ${date_end} using XZ's compression level : ${xz_compression_lvl}." +[ "${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