LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-10-2017, 12:04 AM   #1
THawk254
LQ Newbie
 
Registered: Dec 2016
Location: Manila, Philippines
Distribution: Ubuntu
Posts: 15

Rep: Reputation: Disabled
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

Last edited by THawk254; 01-10-2017 at 07:06 AM.
 
Old 01-10-2017, 02:33 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,211

Rep: Reputation: 1389Reputation: 1389Reputation: 1389Reputation: 1389Reputation: 1389Reputation: 1389Reputation: 1389Reputation: 1389Reputation: 1389Reputation: 1389
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
 
Old 01-10-2017, 07:07 AM   #3
THawk254
LQ Newbie
 
Registered: Dec 2016
Location: Manila, Philippines
Distribution: Ubuntu
Posts: 15

Original Poster
Rep: Reputation: Disabled
Hmm. I'll try that tomorrow.
 
Old 01-15-2017, 01:46 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,768

Rep: Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797Reputation: 797
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange behavior with simple backup routine jbruyet Linux - Software 2 02-15-2013 01:22 PM
[SOLVED] Gnome-terminal exhibits strange behavior on exit SharpyWarpy Linux - Software 7 08-28-2012 09:52 PM
Unexpected results: Difference in Perl Script Behavior between manual and cron runs EnderX Programming 3 12-10-2010 11:42 PM
Arkeia Network Backup - Strange Behavior - Can anybody help? bdb4269 Linux - Software 4 03-03-2006 06:43 PM
Strange Script Behavior Stranger Linux - General 0 02-18-2004 03:30 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration