Backup script help

tls2t
New Contributor II

Hey all!

One of my coworkers came up with a script to automate backing up a user's home directory (to one of our servers) with no intervention. As far as I can tell, the script looks fine, and runs just fine from a terminal window. The problem I see is when the script runs, I get this in the log on the JSS:

Script Result: 2 Backup Location not Mounted. Mounting ... 10.7 mount_smbfs: URL parsing failed, please correct the URL and try again: Invalid argument Failed: Could not mount backup locations. Exiting...

Here is the script:

#!/bin/bash

instances=exec ps aux | grep -c "backup.sh" | awk -F  '{print $1}'

if [ $instance+0 -gt 1 ]
  then echo "Another copy of backup is running. Exiting..."
  exit
fi

user=$1
if [ ! $user ]
  then user=$USER
  if [ ! $user ]
    then echo "Invalid user ID: $user"
    exit
  fi
fi

root="/Volumes"
backup="/SparseBackup"
netloc="/SARC_Backup"
Server="<SERVER>/Backup"
path="/USERS"
file="$HOSTNAME.sparseimage"


####################
## Setup
####################

# Test to see if Sparse file is mounted
if [ ! -d "$root$backup" ]

# Sparse Backup not mounted
then
  echo "Backup Location not Mounted. Mounting ..."

  #Test to see if the backup location is mounted
  if [ ! -d "$root$netloc" ]

  # Backup Share not Mounted
  then

    # Create Mount Point
    if mkdir $root$netloc
    then
      ver=exec sw_vers | grep 'ProductVersion:' | grep -o '[0-9][0-9].[0-9]'
      if [ $ver > "10.6" ]
        then url="//ARCH;$user@$Server"
      else
        url="//$user@$Server"
      fi
      if ! mount -w -t smbfs $url $root$netloc
      then
        echo "Failed: Could not mount backup locations. Exiting..."
        rmdir $root$netloc
        exit
      fi
    else
      echo "Failed: Could not create mount point. Exiting..."
      exit
    fi
  fi

  # Test to see if user folder exists
  if [ ! -d "$root$netloc$path/$user" ]
  then
    if ! mkdir $root$netloc$path/$user
    then
      echo "Failed: Could not create user directory '$root$netloc$path/$user'. Exiting..."
      rmdir $root$netloc
      exit
    fi
  fi

  # Test to see if sparse file exists
  if [ ! -e "$root$netloc$path/$user/$file" ]
  then
    echo "Sparse File '$root$netloc$path/$user/$file' does not exist. Creating one..."
    if ! hdiutil create -size 100g -type SPARSE -fs HFS+J -volname SparseBackup $root$netloc$path/$user/$file
    then
      echo "Failed: Could not create sparse file '$root$netloc$path/$user/$file'. Exiting..."
      diskutil unmount $root$netloc
      exit
    fi
  fi

  # Mount Sparse File
  if ! hdiutil attach $root$netloc$path/$user/$file
  then
    echo "Failed: Could not mount sparse file '$root$netloc$path/$user/$file'. Exiting..."
    diskutil unmount $root$netloc
    exit
  fi
fi



####################
## Backup
####################

rsync -xaq --delete --log-file=$root$netloc$path/$user/backup.log ~/ /Volumes/SparseBackup/



####################
## Cleanup
####################

hdiutil detach $root$backup
diskutil unmount $root$netloc

Any suggestions?

1 REPLY 1

acidprime
New Contributor III

try quoting the url

if ! mount -w -t smbfs "$url" "$root$netloc"

might be the semi colon at first glance.

also if you add set -x to line two it will echo the commands as they run and would give you a better idea of whats not expanding.

also the $USER may not be set in this context.