Scripting user data backup

pat_best
Contributor III

Hi everyone, I got to a point, now I am stuck. I am trying to compress the user home folder and then transfer that .zip to a file share. When I run the commands separately the compression works and the transfer works. The issue is that when I try and run them together, the compression never finishes or doesn't report that it is done ( I do occasionally get a zip file of the test account, but the script never continues past the point of the zip command. Here is what I have so far:

#!/bin/sh

# fileshare credentials
shareUsername="user"
password="password"

# network information
# Get the ethernet hardware port (ehwport) and ip address (eipadd)
ehwport=`networksetup -listallhardwareports | awk '/.Ethernet/,/Ethernet Address/' | awk 'NR==2' | cut -d " " -f 2`

# Get the ethernet ip address (eipadd)
eipadd=`ipconfig getifaddr $ehwport`

# get the first six of the addy
eipsubnet=$( echo "$eipadd" | cut -d. -f1,2 )

echo "The ethernet IP address is $eipadd"

# Test the ethernet connection, if not connected exit the script with a message
if [[ "$eipsubnet" != "123.456" ]]; then
        /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Wired Network Not Detected" -description "Please connect your computer to the wired network and start the backup program again, thanks!" -button1 "Okay" -defaultButton 1
        exit 0
fi

#bring Finder to the front
open .

# user information
loginusername="$(logname)"
  echo "The logged in user is $loginusername"
userfolder="$(osascript -e '
        tell application "Finder"
        display dialog "Please enter you district user name here (first.last)" default answer ""
            set userfolder to the (text returned of the result)
        end tell')"
    echo "The user backup folder will be named $userfolder"
# put in a message in case they canceled the last window or didn't type anything to be able to exit
baduname="$(/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Continue?" -description "If you made a mistake in typing your name in the last window, here is your chance to exit and start over.  Press Continue to move forward or Close to start over." -button1 "Continue" -button2 "Close" -defaultButton 2)"
    echo "The baduname exit status is $baduname"
if [[ $baduname == 2 ]]; then
            exit 0
    else

# empty the downloads and trash folders
# folder cleanup
cleanup="$(/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "File Clean Up" -description "Is it okay for me to empty your trash and downloads folder now?  If yes, the files in those folders will be deleted and the backup process will begin. The backup may take a few minutes to several hours to complete during which your computer will be unavailable. If no, please empty them at your convenience and run the backup program again." -button1 "No" -button2 "Yes" -defaultButton 2)"
    echo "The cleanup exit status is $cleanup"
if [[ $cleanup == 0 ]]; then
            /Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Clear Your Folders" -description "Please save any files from your Downloads and Trash folders and run the program again, thanks!" -button1 "Okay" -defaultButton 1
            exit 0
    else
        rm -rf Users/"$loginusername"/Downloads/*
        rm -rf Users/"$loginusername"/.Trash/*
fi  

# lock out users from using their computer while this is running
/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType fs -heading "User Backup in Progress" -description "This part of the process may take an extended period of time (from minutes to possibly hours).  We appreciate your patience while your computer is running it's backup.  This message will automatically clear and your computer will reboot when it is done, thanks!" &

# compress the user home folder
zip -r "$loginusername".zip /Users/"$loginusername"

# mount network share
/usr/sbin/jamf mount -server servername -share sharepoint -type smb -username "$shareUsername" -password "$password"

# make a folder using their district username
mkdir /Volumes/backup/"$userfolder"

# copy archive folder to network share
cp "$loginusername".zip /Volumes/backup/"$userfolder"

# dismount network share
/usr/sbin/jamf unmountServer servername -mountPoint sharepoint

# kill the backup fullscreen window
killall jamfhelper

fi

exit 0

any ideas out there? I appreciate any input on the whole thing as well if something strikes you as bad practice or just plain weird. Thanks!!

5 REPLIES 5

pat_best
Contributor III

just an update, I removed the fullscreen jamfhelper window on the compression completes. It looks like the file gets created every time but the next step is held up by the fullscreen window.

pat_best
Contributor III

I put an "&" at the end of the jamfhelper command to allow other code to run after the fullscreen window launches. Am I using this function incorrectly?

pat_best
Contributor III

I ended up taking the full screen window out and adding cocoa barber poles instead.

denmoff
Contributor III

Nice work. I'm working on something similar. I need to get mobile user accounts to backup to the AD share at regular intervals and i need a straight backup to our DeployStudio server for new computer migrations. I've got some of it worked out, but still working out many of the details.

Would you mind explaining the usage case for this? Is this for data migration purposes or simply backup? Have you considered using rsync and have it run in the background instead of interrupting the user? The zipping of the home folder and then storing it in the home folder seems like a bad way to do this. If the user's home directory is very large, you may run into disk space issues. You could probably use tar to compress and transfer on the fly, but i'm not sure how that works. I prefer rsync.

pat_best
Contributor III

This is for a one shot program where we are swapping a limited number of staff computers over a few month period (100 of them to be exact) All I needed was a way to capture the user data into a shared location for temporary storage. I don't have capacity issues for this particular project...yet :) A major change we will be dealing with is in a lot of cases our users will be moving from 10.5 to 10.10 and moving from a local account to an AD login. I had considered rsync but in this case this will be the last thing the staff do with their computer before they leave for the summer and will not be ongoing so cp and rsync are very similar in this instance. We do have users with 200+ GB home folders and we will deal with them separately. Why do you feel zipping then storing the home folder is a bad way to do this? I would appreciate any thoughts you have, thanks!!