vendredi 4 août 2017

Weird output with bash script for backing up directories

I wrote a Bash script for backing up a directory's content into a .tar file. I intent on updating the script into something more organize, portable, and shareable. However, I cannot for the life of me understand a bug in my code, where the output in a occasion shouldn't change the $OUTDIR to the same path as $INDIR.

It happens whenever -d is appended to the file command's parameters, which should display the strings used in the actual gist of the script, the tar command, to define it's own parameters. I've rewritten that part of my code twice and I can't understand why the output differs everytime.

#!/bin/bash
# Script will only back up an accessible directories
# Any directories that require 'sudo' will not work.

if [[ -z $1 ]]; then                    # This specifically
    INDIR=$(pwd); debug=false;          # is the part where 
elif [[ $1 == '-d' ]]; then             # the 'debug' variable 
    debug=true;                        # is tampering with 
    if [[ -z $2 ]]; then INDIR=$(pwd); # the output somehow.
    else INDIR=$2; fi
else 
    INDIR=$1; debug=false; fi

if [[ -z $2 ]]; then
    OUTDIR=$INDIR
elif [[ '$2' = '$INDIR' ]]; then
    if [[ -z $3 ]]; then OUTDIR=$INDIR;
    else OUTDIR=$3; fi
else
    OUTDIR=$2; fi

FILENAME=bak_$(date +%Y%m%d_%H%M).tar
FILEPATH=$OUTDIR'/'$FILENAME
LOGPATH=$OUTDIR'/bak.log'

if [[ "$debug" = true ]]; then
    echo "Input directory:  "$INDIR
    echo "Output directory: "$OUTDIR
    echo "Output file path: "$FILEPATH
    echo "Log file path:        "$LOGPATH
else
    tar --exclude=$FILEPATH --exclude=$LOGPATH \
    -zcvf $FILEPATH $INDIR > $LOGPATH
fi

This is the output

gnomop@GnomoPC:~$ bakdir -d
Input directory:    /home/gnomop
Output directory:   /home/gnomop
Output file path:   /home/gnomop/bak_20170804_2123.tar
Log file path:      /home/gnomop/bak.log
gnomop@GnomoPC:~$ bakdir -d /home/other
Input directory:    /home/other
Output directory:   /home/other
Output file path:   /home/other/bak_20170804_2124.tar
Log file path:      /home/other/bak.log
gnomop@GnomoPC:~$ bakdir -d /home/other /home/other/bak
Input directory:    /home/other
Output directory:   /home/other  
Output file path:   /home/other/bak_20170804_2124.tar
Log file path:      /home/other/bak.log

Aucun commentaire:

Enregistrer un commentaire