I have written a small bash file to backup a repository. If the archive file doesn't exists, it should be created. Thereafter, files found should be appended to the existing file.
For some reason, the archive keeps getting (re)created/overwritten. This is my script below. Can anyone see where the logic error is coming from?
#!/bin/bash
REPOSITORY_DIR=$PWD/../repository
STORAGE_DAYS=30
#https://stackoverflow.com/questions/48585148/reading-names-of-imediate-child-folders-into-an-array-and-iterating-over-it?noredirect=1#comment84167181_48585148
while IFS= read -rd '' file;
do fbname=$(basename "$file");
# Find all persisted (.CSV) files that are older than $STORAGE_DAYS
files_to_process=($(find $REPOSITORY_DIR/$fbname -type f -name '*.csv' -mtime +$STORAGE_DAYS))
backup_datafile=$REPOSITORY_DIR/$fbname/$fbname.data.tar.gz
#echo $backup_datafile
# If the tar.gz file does not exist, we want to create it
# else (i.e. file exists), we want to add files to it
# Solution from: https://stackoverflow.com/questions/28185012/how-to-create-tar-for-files-older-than-7-days-using-linux-shell-scripting
NUM_FILES_FOUND=${#files_to_process[@]}
if [ $NUM_FILES_FOUND -gt 0 ]; then
echo "Found ${#files_to_process[@]} files to process ..."
if [ ! -f backup_datafile ]; then
# Creating a new tar.gz file, since file was not found
echo "Creating new backup file: $backup_datafile"
tar cvfz $backup_datafile "${files_to_process[@]}"
else
echo "Adding files to existing backup file: $backup_datafile"
# https://unix.stackexchange.com/questions/13093/add-update-a-file-to-an-existing-tar-gz-archive
gzip -dc $backup_datafile | tar -r "${files_to_process[@]}" | gzip >$backup_datafile.new
mv $backup_datafile.new $backup_datafile
fi
# remove processed files
for filename in "${files_to_process[@]}"; do
rm -f "$filename"
done
else
echo "Skipping directory: $REPOSITORY_DIR/$fbname/. No files found"
fi
done < <(find "$REPOSITORY_DIR" -maxdepth 1 -mindepth 1 -type d -print0)