Oneota
April 22, 2004, 10:10pm
#1
Hello! I’m writing a script that my workers can run to backup any important information on the hard drive of a machine before they NetBoot and NetRestore it. Basically, I want it to back up any apps that aren’t on our image, anything the user might have in a Media Storage folder we’ve set up, and any local users (we use mostly network users, but have some local accounts for special purposes). My script thus far is this:
[Problem in question fixed, but see the post below for my current question]
Oneota
April 28, 2004, 6:36pm
#2
Hello again! I’ve figured out my problem that prompted the original post. I thought I’d post my current version (which is pretty close to done, but not quite) and invite any feedback on programming style, etc. Anything you gurus would improve upon?
Thanks!
(* Pre-Restore Script Design
1) Check Current Computer Name – if it starts with 00, then the machine was reimaged without it being in the DB. Notify user to ensure it’s in the DB now.
2) Check for pre-existing backups on TechComm/User Backups.
a. Has this script already been run?
b. Can the old backup be deleted?
3) Check for mobile or home/home accounts.
a. Need to find some way to back these up automatically
b. Also back-up the appropriate NetInfo record(s)
c. Post-Restore needs to be able to restore:
i. files w/intact permissions
ii. NetInfo records
4) Check for stuff in Media Storage
a. Post-Restore should copy back with 777 permissions
5) Check for non-standard apps
6) Check for non-standard stuff at the root of the HD
7) Check for stuff on Mac OS 9’s Desktop
8) Look up in replacement table to see if this unit is getting swapped out
a. If so, rename backup folder to Tag ID or ethernet address of new unit
b. Post-Restore script will need to check for Ethernet address, and, if no matching backup is found, prompt user for Tag ID (or lookup ethernet address).
*)
set currentUser to (do shell script “whoami”) as string
if ((currentUser is not “techcomm”) and (currentUser is not “”)) then
display dialog “This script must be run as techcomm.” buttons {“Cancel”}
end if
global testOnly
global ethernetAddress
set ethernetAddress to (do shell script “/sbin/ifconfig en0 | /usr/bin/awk '/ether/ { gsub(”:“, “”); print $2 }'”)
global computerName
set computerName to (do shell script “hostname | cut -d ‘.’ -f 1”) as string
if ((computerName begins with “00”) or (computerName is equal to ethernetAddress )) then
display dialog “This machine was last reimaged without being in the Database. Ensure it’s in there now, rename the machine in the Sharing pane of System Preferences, and re-run this script.” buttons {“Cancel”}
end if
if (computerName is “<Development Station’s Name>”) then
set testOnly to true
end if
set the clipboard to ethernetAddress as string
set standardApps to {" <>"}
set standardUsers to {“Shared”, “techcomm”, “ARD”}
set pathToBackup to “User Backups:” & ethernetAddress as string
set pathToApps to pathToBackup & “:Apps”
set pathToUsers to pathToBackup & “:Users”
set pathToMedia to pathToBackup & “:Media”
set mediaStorageContents to {}
set nonStandardUsers to {}
set nonStandardApps to {}
tell application “Finder”
set mediaStorageContents to (get (every item of folder “Media Storage” of folder “Shared” of folder “Users” of startup disk ))
if (count of mediaStorageContents ) is 0 then – No Media to Backup
set mediaToBackup to false
else
set mediaToBackup to true
end if
set nonStandardUsers to (get (every item of folder “Users” of startup disk whose name is not in standardUsers ))
if (count of nonStandardUsers ) is 0 then – No local accounts
set nonStandardUsersExist to false
else – local accounts exist
set nonStandardUsersExist to true
end if
set nonStandardApps to (get (every item of folder “Applications” of startup disk whose name is not in standardApps ))
if (count of nonStandardApps ) is 0 then – Nothing Found
set nonStandardAppsFound to false
else – Something found
set nonStandardAppsFound to true
end if
if ((mediaToBackup is true ) or (nonStandardAppsFound is true ) or (nonStandardUsersExist is true )) then
mount volume “afp://user:pass@172.16.2.15/User Backups”
–#####UNCOMMENT ME!!!#####
–if (folder pathToBackup exists) then
–display dialog “A folder with the name " & ethernetAddress & " already exists. Please verify that the script hasn’t already been run and rename/delete the old backup as appropriate.” buttons {“OK-Change me to Cancel”}
–end if
–#####UNCOMMENT ME!!!#####
try
make new folder at disk “User Backups” with properties {name :ethernetAddress as string }
–#####UNCOMMENT ME!!!#####
–on error
– display dialog “Somehow, there’s still a duplicate folder! How’d that happen?”
–#####UNCOMMENT ME!!!#####
end try
else
display dialog “Go ahead and blitz this machine. Nothing needs to be done to it.”
end if
end tell
if (nonStandardAppsFound is true ) then
tell application “Finder”
try
make new folder at folder pathToBackup with properties {name :“Apps”}
end try
end tell
backupStuff (nonStandardApps , pathToApps as string )
end if
if (nonStandardUsersExist is true ) then
tell application “Finder”
try
make new folder at folder pathToBackup with properties {name :“Users”}
end try
repeat with i in nonStandardUsers
try
make new folder at folder “Users” of folder pathToBackup with properties {name do shell script “id " & (name of i as string ) & " | cut -d ‘=’ -f 2 | cut -d ‘(’ -f 1”)}
[color=blue]end[/color] [color=blue]try[/color]
[color=blue]end[/color] [color=blue]repeat[/color]
[color=blue]end[/color] [color=blue]tell[/color]
[color=green]backupUserNetInfoRecords[/color]([color=green]nonStandardUsers[/color])
[color=green]backupStuff[/color]([color=green]nonStandardUsers[/color], [color=green]pathToUsers[/color] [color=blue]as[/color] [color=blue]string[/color])
end if
if (mediaToBackup is true ) then
tell application “Finder”
try
make new folder at folder pathToBackup with properties {name :“Media”}
end try
end tell
backupStuff (mediaStorageContents , pathToMedia as string )
end if
on backupStuff (L , destination )
tell application “Finder”
repeat with i in L
set theVariable to i as alias
set posixPath to POSIX path of theVariable
if testOnly then
display dialog "We’d be backing up " & posixPath & " to " & destination
else
try
set unlockFiles to “echo ‘password’ | sudo -S chflags -R nouchg '” & posixPath & “'”
do shell script unlockFiles
set changeOwnership to “echo ‘password’ | sudo -S chown -R techcomm:admin '” & posixPath & “'”
do shell script changeOwnership
set changePermissions to “echo ‘password’ | sudo -S chmod -R u+rwx '” & posixPath & “'”
do shell script changePermissions
duplicate i to folder destination
end try
end if
end repeat
end tell
end backupStuff
on [color=green]backupUserNetInfoRecords/color
if testOnly then
display dialog “NetInfo Dump of Local User would go here if we weren’t testing”
else
repeat with i in L
set uidOfUser to (do shell script “id " & (name of i as string ) & " | cut -d ‘=’ -f 2 | cut -d ‘(’ -f 1”)
do shell script “nidump -r /users/” & (name of i as string ) & " / > " & “'/Volumes/User Backups/” & ethernetAddress & “/Users/” & uidOfUser & “/” & uidOfUser & “.nidump’”
end repeat
end if
end backupUserNetInfoRecords
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code ]
rdagel
December 31, 2005, 12:21am
#3
are you run this as a pre action for net restore or this runs before netrestore even launches?