Posted on 06-03-2018 07:35 AM
As we get new computers for users, to transfer the data from old to new, we use a script in self service that runs rysnc between the computers connected via target disk mode, works great. I'd love to improve this by having the rysnc progress in an active dialog verbose dialog box so our Team can track the progress. Can any script gurus help me get that implemented into our script below?
# Get username
username=$(/usr/bin/osascript<<END
tell application "System Events"
activate
set the answer to text returned of (display dialog "Enter the username you want to rsync.
Click Continue and grab some coffee!" default answer "" buttons {"Continue"})
end tell
END)
echo "User paramater passed..."
sleep 1
echo "Working..."
sleep 1
echo "Remove user home folder cache on Macintosh HD 1"
sleep 1
rm -rf /Volumes/Macintosh HD 1/Users/"$username"/Library/Caches/*
sleep 2
echo "Emptying user Trash..."
rm -rf /Volumes/Macintosh HD 1/Users/"$username"/.Trash/*
sleep 2
sleep 1
echo "Working..."
sleep 1
echo "Initializing Migration Assistant..."
sleep 2
echo "Transferring User Data....."
sleep 1
/usr/bin/rsync -av /Volumes/Macintosh HD 1/Users/"$username"/* /Volumes/Macintosh HD/Users/"$username"/
sleep 2
echo "Running Transferring User Data......"
sleep 10
echo "Adjusting File Permissions"
/usr/bin/chmod -R 775 /Volumes/Macintosh HD/Users/"$username"
sleep 1
echo "Adjusting File Ownership..."
/usr/sbin/chown -R "$username":staff /Volumes/Macintosh HD/Users/"$username"
echo "Adjusting File Permissions"
/usr/libexec/repair_packages --repair --standard-pkgs --volume /
echo "Copy complete..."
sleep 1
echo "Transfer Completed Successfully..."
#Prompt Team that transfer was complete
USER=`who | grep console | awk '{print $1}'`
/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Data Migration Utility " -heading "Data Migration Status" -alignHeading center -description "Data transfer has completed, check and compare the sizes of both HDDs while I make this user an admin of this laptop." -icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarInfo.icns -button1 "OK" -button2 "Cancel" -defaultButton 1 -cancelButton 2
# If the user clicks OK
if [ "$?" == "2" ]; then
echo "Thank you!";
exit 1
# if the user clicks cancel
elif [ "$?" == "2" ]; then
echo "Thank You.";
exit 1
fi
sleep 2
#Add a non-admin user to the admin group
if groups $username | grep -q -w admin;
then
echo "User '$username' already is in the admin group";
else
dscl . -append /Groups/admin GroupMembership $username;
echo "User '$username' has been added to the admin group!";
fi
exit 0
########################################################################################
Posted on 06-04-2018 12:21 AM
@TomDay This is a nice interactive way of migrating users, I'd like to copy your script if you dont mind. We are doing this method too but we just do it manually, per user, by request, using the network or target disk mode. We see progress by adding "-P" same as --partial --progress
rsync -avP /Volumes/Macintosh HD 1/Users/"$username"/* /Volumes/Macintosh HD/Users/"$username"/
edit: You can also call the terminal to open to see progress and just kill it with another command on your script.
osascript <<END
tell application "Terminal"
activate
do script "rsync -avP /Volumes/Macintosh HD 1/Users/username/* /Volumes/Macintosh HD/Users/username/"
end tell
END
Posted on 06-04-2018 05:38 AM
I use the same command that @Eigger suggest. On top of that, I have a tmp log file created/opened at the start of the process and output the verbose text to that log file so that the tech running the migration can see the current status.
Posted on 06-04-2018 06:15 AM
@Eigger Please use this as you wish! Definitely needs some TLC though :-)
Working on your additions now, TY.
Posted on 06-04-2018 06:47 AM
Still working on this, running into some issues, can't get your piece @Eigger to work, doesn't execute the rsync at all:
#!/bin/sh
# Get username
username=$(/usr/bin/osascript<<END
tell application "System Events"
activate
set the answer to text returned of (display dialog "Enter the username you want to rsync.
Click Continue and grab some coffee!" default answer "" buttons {"Continue"})
end tell
END)
echo "User paramater passed..."
sleep 1
echo "Working..."
sleep 1
echo "Remove user home folder cache on Macintosh HD 1"
sleep 1
rm -rf /Volumes/Macintosh HD 1/Users/"$username"/Library/Caches/*
sleep 2
echo "Emptying user Trash..."
rm -rf /Volumes/Macintosh HD 1/Users/"$username"/.Trash/*
sleep 2
echo "Initializing Migration Assistant..."
sleep 2
echo "Transferring User Data....."
sleep 1
osascript <<END
tell application "Terminal"
activate
do script "rsync -avP /Volumes/Macintosh HD 1/Users/$username/* /Volumes/Macintosh HD/Users/$username/"
end tell
END
echo "Adjusting File Permissions"
/usr/bin/chmod -R 775 /Volumes/Macintosh HD/Users/"$username"
sleep 1
echo "Adjusting File Ownership..."
/usr/sbin/chown -R "$username":staff /Volumes/Macintosh HD/Users/"$username"
echo "Adjusting File Permissions"
/usr/libexec/repair_packages --repair --standard-pkgs --volume /
echo "Copy complete..."
sleep 1
echo "Transfer Completed Successfully..."
#Prompt Team that transfer was complete
USER=`who | grep console | awk '{print $1}'`
/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Data Migration Utility " -heading "Data Migration Status" -alignHeading center -description "Data transfer has completed, check and compare the sizes of both HDDs while I make this user an admin of this laptop." -icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarInfo.icns -button1 "OK" -button2 "Cancel" -defaultButton 1 -cancelButton 2
# If the user clicks OK
if [ "$?" == "2" ]; then
echo "Thank you!";
exit 1
# if the user clicks cancel
elif [ "$?" == "2" ]; then
echo "Thank You.";
exit 1
fi
sleep 2
#Add a non-admin user to the admin group
if groups $username | grep -q -w admin;
then
echo "User '$username' already is in the admin group";
else
dscl . -append /Groups/admin GroupMembership $username;
echo "User '$username' has been added to the admin group!";
fi
exit 0
########################################################################################
@ddcdennisb Mind if I see some of your code on that?
Posted on 06-04-2018 07:13 AM
Here is my script:
#!/bin/sh
touch /tmp/sync_log.log
status="/tmp/sync_log.log"
systemsetup -setcomputersleep Never
sleep 2
#Ask for username
user="$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter username to move:" default answer "" with title "Username" with text buttons {"Ok"} default button 1' -e 'text returned of result')"
sleep 2
#open Console with syncLog
open /tmp/sync_log.log
sleep 2
#Remove Office 2011 Data
rm -fRv /Volumes/Macintosh HD 1/Users/$user/Documents/Microsoft User Data/Office 2011 Identities/ | tee -a $status
sleep 2
#Remove Office 2016 Data
rm -fRv /Volumes/Macintosh HD 1/Users/$user/Library/Group Containers/UBF8T346G9.* | tee -a $status
sleep 2
#empty Trash
rm -fRv /Volumes/Macintosh HD 1/Users/$user/.Trash | tee -a $status
sleep 2
#Sync Data
rsync -aEv --progress --stats /Volumes/Macintosh HD 1/Users/$user/ /Users/$user/ | tee -a $status
sleep 2
#Repair Permissions
chown -R $user /Users/$user/ | tee -a $status
sleep 2
systemsetup -setcomputersleep 120 | tee -a $status
sleep 2
killall Console
exit 0
Posted on 06-04-2018 08:13 AM
Several years back I wrote an "app" which was a bash script wrapped up in a Platypus application that had an embedded copy of cocoaDialog in it. The app was for backing up user folders to another destination. What was cool about it was it prompted the tech to select a home folder with a standard "open" dialog first, then for a destination volume, and then it would ask them to select which folders from the home directory they wanted to back up. It built the list dynamically from the chosen home folder and used the cocoaDialog 'checkbox' option. They also could select "All" and it would check all items on for backup. It did a few other background operations as well, but it used rsync for the actual copying. While it didn't show actual progress, it did show which top level folder (like "Desktop" or "Documents") it was working on in the cocoadialog indeterminate progress bar window.
All this is to say that I never could figure out an exact way of showing true progress with a percentage. It's not easy to do with rsync, though I understand a newer version of rsync (which of course macOS doesn't include) can do it with a special flag now. It's relatively new though and I think might even still be in beta.
Posted on 06-04-2018 12:46 PM
@ddcdennisb This works nicely, just tweaking for our environment a little TYVM. Have you had any bugginess with computers going to sleep, not sure if the "systemsetup -setcomputersleep Never" command is working for me. Troubleshooting tomorrow, thanks again!
Posted on 06-04-2018 12:47 PM
@TomDay try the caffeinate command.
Posted on 06-04-2018 06:04 PM
@TomDay , we see the screen saver go on but not not sleep. we had issues with the system going to sleep. That is why i added the setcomputersleep command.
Posted on 06-06-2018 08:18 AM
ahhh caffeinate, how could I forget that! Great call @Eigger . I use that in one of my other scripts, will check that out and try to figure out best placement for that command in this script.
@ddcdennisb Interesting, I don't see the screensaver at all. Wonder if I have a profile or policy eluding me that is setting this on my computers and not allowing this to work in this script.
Posted on 06-06-2018 12:17 PM
Not sure WTH I am doing wrong here! I wanted to verify "systemsetup -setcomputersleep Never" would work when directly entered into Terminal on this test computer and it did. Testing with systemsetup -getcomputersleep returns "Never". Played with the script a little using caffeinate and the script stalls so shot in the dark added "sudo" to the script and it works. I know you shouldn't need sudo but it worked, not sure why?
Posted on 06-06-2018 10:37 PM
@TomDay and @ddcdennisb Thank you both for your scripts. Here is what I concocted using both of your scripts and added my own sauce. Take note of the && I added at the end of the rysnc command. I added it instead of sleep because the pause might not be enough if the rsync takes longer than expected. In cmd1 && cmd2, cmd2 will only be run if cmd1 exits with a successful return code. I am working on another version of this that use the network instead of target disk mode. I'm studying the combination of ssh and rsh to query the remote machine, and also applescripts for the visual prompts. Also, please note that I havent fully tested this script yet, so I'm not sure if the --exclude command really works, Ill test again tomorrow on those users that have Office 2016.
EDIT: removed --exclude "Library", LOL! bad! The --exclude "Microsoft User Data" --exclude "UBF8T346G9.*" works. The only thing I couldn't figure out is how to exit the script if the user hits "Cancel" on the selection of the Volume and User.
#!/bin/sh
touch /tmp/sync_log.log
status="/tmp/sync_log.log"
echo "$(date): Caffeinating..." | tee -a $status
caffeinate -d -i -m -u &
caffeinatepid=$!
#Choose Volume
vol="$(/usr/bin/osascript<<END
tell application "System Events"
activate
do shell script "/bin/ls /Volumes"
get paragraphs of result
choose from list (result) with prompt "Select the source volume" with title "CHOOSE VOLUMES"
end tell
END)"
#Choose User
user="$(/usr/bin/osascript<<END
tell application "System Events"
activate
do shell script "/bin/ls '/Volumes/$vol/Users' | egrep -i -v Shared"
get paragraphs of result
choose from list (result) with prompt "Select the User Account you would like to migrate" with title "CHOOSE USER"
end tell
END)"
#open Console with syncLog
open /tmp/sync_log.log
sleep 2
echo "Remove user home folder cache on $vol" | tee -a $status
sleep 2
rm -rf /Volumes/"$vol"/Users/"$user"/Library/Caches/*
sleep 2
echo "Emptying User Trash..." | tee -a $status
sleep 2
rm -rf /Volumes/"$vol"/Users/"$user"/.Trash/*
sleep 2
#Sync Data
rsync -avP --exclude "Microsoft User Data" --exclude "UBF8T346G9.*" /Volumes/"$vol"/Users/$user/* /Users/$user/ | tee -a $status &&
echo "Adjusting File Permissions" | tee -a $status
/usr/bin/chmod -R 775 /Volumes/$1/Users/$user
sleep 2
echo "Adjusting File Ownership..." | tee -a $status
/usr/sbin/chown -R $user:staff /Volumes/$1/Users/$user
sleep 2
echo "$(date): Decaffeinating!" | tee -a $status
pkill caffeinate
sleep 2
#Prompt Team that transfer was complete
USER=`who | grep console | awk '{print $1}'`
/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType utility -title "Data Migration Utility " -heading "Data Migration Status" -alignHeading center -description "Data transfer has completed, check the logs for errors." -icon /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ToolbarInfo.icns -button1 "OK" -defaultButton 1
# If the user clicks OK
if [ "$?" != "1" ]; then
killall Console
exit 0
fi
Posted on 06-07-2018 10:55 AM
Looking forward to the results, thx @Eigger !
Posted on 06-08-2018 02:33 PM
I edited the script and remove --exclude "Library". Other --exclude worked as expected. I'm still trying to figure out how to exit the script if the user hits "Cancel" on the selection of the Volume and User.