Create an archive only if files are found

This commit is contained in:
Jeremy Gardais 2020-01-08 10:51:14 +01:00
parent dea26fe311
commit 525826f0d0
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 18 additions and 11 deletions

View File

@ -32,6 +32,9 @@ xz_compression_lvl="-9"
## Get current directory name ## Get current directory name
current_dir=${PWD##*/} current_dir=${PWD##*/}
## Count the number of files
match_files=$(find . -type f -newermt "${date_start}" -not -newermt "${date_end}" -not -iname "*.tar*" | wc -l)
## Archive name ## Archive name
tar_file_name="${date_year}.${current_dir}${xz_compression_lvl}.tar.xz" tar_file_name="${date_year}.${current_dir}${xz_compression_lvl}.tar.xz"
@ -41,18 +44,22 @@ tar_file_name="${date_year}.${current_dir}${xz_compression_lvl}.tar.xz"
if [ -s "${tar_file_name}" ]; then if [ -s "${tar_file_name}" ]; then
printf "${c_redb}%-6b${c_reset}\n" "ERROR: ${tar_file_name} already exists. Please manage this directory manually or remove the archive and restart." printf "${c_redb}%-6b${c_reset}\n" "ERROR: ${tar_file_name} already exists. Please manage this directory manually or remove the archive and restart."
exit 1 exit 1
else
[ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG: Create an archive for ${current_dir} files between ${date_start} and ${date_end} using XZ's compression level: ${xz_compression_lvl}."
fi fi
## Get the list of files between the 2 dates and ignore tar files # If some files match
find . -type f -newermt "${date_start}" -not -newermt "${date_end}" -not -iname "*.tar*" -print0 | tar cJf "${tar_file_name}" --null -T - 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}."
## Check previous return code and if the archive exists with size > 0 ## Get the list of files between the 2 dates and ignore tar files
if [ "${?}" -eq "0" ] && [ -s "${tar_file_name}" ]; then #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 "${c_magentab}%-6b${c_reset}\n" "DEBUG: ${tar_file_name} successfully created, the files can be deleted." [ "${debug}" -eq "0" ] && printf "${c_magentab}%-6b${c_reset}\n" "DEBUG: ${tar_file_name} successfully created, the files can be deleted."
find . -type f -newermt "${date_start}" -not -newermt "${date_end}" -not -iname "*.tar*" -delete #find . -type f -newermt "${date_start}" -not -newermt "${date_end}" -not -iname "*.tar*" -delete
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 fi
exit 0 exit 0