1

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) 
Stefan M
  • 868
  • 6
  • 17
Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
  • Oh my god... 15k user. Can you check the edit suggestion? I think it makes sense. – iBug Feb 19 '18 at 13:17
  • 1
    @iBug, you'll be surprised at what havoc impending deadlines and staring at a screen for 12 hours straight can do to you too - don't judge :p – Homunculus Reticulli Feb 19 '18 at 13:25
  • I use Vim as my editor so these kinds of bugs can be easily spotted with syntax highlighting. – iBug Feb 19 '18 at 13:27

2 Answers2

1

This is where it went wrong:

if [ ! -f backup_datafile ]; then

I guess it should have been

if [ ! -f $backup_datafile ]; then
          ^

Or better yet, put that in quotes:

if [ ! -f "$backup_datafile" ]; then
iBug
  • 35,554
  • 7
  • 89
  • 134
0

if [ ! -f backup_datafile ]; then

i think you might be missing a "$" there