LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Backup script runs but exhibits strange behavior - Ubuntu Server 16.04 LTS (https://www.linuxquestions.org/questions/linux-software-2/backup-script-runs-but-exhibits-strange-behavior-ubuntu-server-16-04-lts-4175597130/)

THawk254 01-10-2017 12:04 AM

Backup script runs but exhibits strange behavior - Ubuntu Server 16.04 LTS
 
Hi all,

I'm writing a backup script to backup the folders on a windows server.

Basically, the script reads a text file for the windows folders, mounts them via mount.cifs then tars up the folders and as a backup report, it does a listing (tar -tf).

The strange part that I can't understand is this:

I can run the script from the command line as root. It works fine. I've tested the script with one line in the folderList.txt file (the list of folders) and with 3 lines. However, when I run the script as a cron job at 1:30am, although the script runs, it doesn't backup anything. As in, the script fires and the tarfiles are created, but they're all empty. I've also tested the cron job to fire in the middle of the day (e.g. 3pm) and it runs fine.

Any ideas? Also where would the logs be (if there are any)?

N.B. Before anyone comments, yes, I'm a Doctor Who fan (as evidenced by my choice of server name!) ;)

Script is below:


Code:

#!/bin/bash

# Get the date
THEDATE=`date +%d%m%y_%a`
TODAY=`echo $THEDATE | cut -d '_' -f 1`

# Get the day of week
DOW=`echo $THEDATE | cut -d '_' -f 2`

MNT="/remoteDir"                                # this is where the remote FS is mounted
BACKUPDEST="/backupFiles"                        # this is where the backups get dropped
BACKUPCONFIG="/root/backups/backupConfig"        # this is where the config files are located
EXCEPTIONS="$BACKUPCONFIG/Exceptions.txt"        # location of the exclusions file
LISTINGS="$BACKUPDEST/Listings"                        # location of file listings

#Prepare for a full backup by removing the snapshot files
function prepFullBackup {
  FILE=$1
  if [ -f $FILE ]; then
      rm $BACKUPCONFIG/$FILE.snar
  fi
}

# Mount the remote server
function mountFolder {
  echo "Mounting //claraoswald/$VAR $MNT" >> outputfile.txt
  mount.cifs -o ro,credentials=/root/.smbcredentials,iocharset=utf8,sec=ntlm //claraoswald/$VAR $MNT
  sleep 10s # wait 10 seconds
}

# Create the tar file and file listing
function createBackup {
  echo "Starting backup: tar -cJf $BACKUPDEST/$1 -g $BACKUPCONFIG/$2.snar -X $EXCEPTIONS $MNT" >> outputfile.txt 
  tar -cJf $BACKUPDEST/$1 -g $BACKUPCONFIG/$2.snar -X $EXCEPTIONS $MNT
  echo "Starting listing: tar -tf $BACKUPDEST/$1 > $LISTINGS/$2'_'$TODAY'_'list.txt" >> outputfile.txt
  tar -tf $BACKUPDEST/$1 > $LISTINGS/$2'_'$TODAY'_'list.txt
}

for VAR in $( cat folderList.txt )
do
  FOLDER=`echo $VAR | cut -d '_' -f 2`

  if [ "$DOW" == "Mon" ]; then
      BTYPE='Full'
      prepFullBackup $FOLDER
  else
      BTYPE='Inc'
  fi

  FILENAME=$FOLDER'_'$TODAY'_'$BTYPE.tar.xz
  mountFolder $VAR
  createBackup $FILENAME $FOLDER
  umount $MNT
done


TenTenths 01-10-2017 02:33 AM

Next time put your script in [code] tags to make it readable.

This is covered time and time again.

For any executable command you should specify /the/full/path/to/the/command

THawk254 01-10-2017 07:07 AM

Hmm. I'll try that tomorrow.

MadeInGermany 01-15-2017 01:46 AM

Set PATH at the beginning so every simple command is from there! (And your script remains short and readable).
Further, every $VAR in command arguments should be in quotes: "$VAR"! It avoids unwanted behavior if $VAR contains special characters.
Examples
Code:

FOLDER=`echo "$VAR" | cut -d '_' -f 2`

prepFullBackup "$FOLDER"

mountFolder "$VAR"

createBackup "$FILENAME" "$FOLDER"

if [ -f "$FILE" ]; then
      rm "$BACKUPCONFIG/$FILE.snar"
fi



All times are GMT -5. The time now is 06:31 PM.