?
Solved

OSX 10.6.7 - Copy home directories within local user folder

Posted on 2011-04-29
10
Medium Priority
?
767 Views
Last Modified: 2012-05-11
I'm asking this question again because I didn't make myself clear the last time I posted. The home directories are a mixture of local and 'network', which means most of the folders wont have a local user account on the mac. The systems I inherited were using the 'force local home' option within the AD plugin which I am planning to remove but I want to make sure I have backed up all local home folders before doing so. I only need to copy the documents, desktop and music folders of each user.

The script I was given before worked a treat if the local folder had a local user account on the machine. Again, i didn't specify that most of the folders were network accounts so this is my fault.


So just to recap, there are around 60 user folders within /users/
3 are local user accounts and the rest are network accounts

I guess I need somebody to modify the script so it recursively moves through each folder and copies the contents of docs, desktop and muisc and stores it in say/userback

It would be useful if it didn't copy the local admin account and if the user back-up folder is accessible to everybody.

Hope this makes sense.




 
(* BACK UP LOCAL USERS *)
-- ©2011 Bryan Vines

-- PURPOSE:
-- This script will back up the specified subfolders of all local users; the backups are stored in /Users/Shared. Save this script as an application, and save it as "Run only." Deploy it via ARD, then you can use ARD's "run application" feature to run it whenever you like.

(* THE SCRIPT *)
-- SETUP: 
--Define the login credentials of the administrator on the computer. Generally speaking, this is a bad idea, but if you save this script as an application and deny editing, it should be reasonably safe from most users' prying eyes.
-- Define a designated reviewer. This is the user responsible for examining the backups to find files.

set the adminUserName to "sysadmin"
set the adminPassword to "admin123"
set the designatedReviewer to "sysadmin"

-- If you want to exclude your admin user from the user list, set its short name here.
-- Leave blank to return all local users.
set administratorShortNameToOmit to ""

-- Set the full path to your backup destination here. Make sure to include a trailing slash.
set the backupDestination to "/Users/Shared/Back Up/"

-- Get the user list.
set the localUserList to getLocalUserList(administratorShortNameToOmit)

-- Back up these users to the backup destination folder.
repeat with currentUser in the localUserList
	-- As an example, we will only back up the Sites folder -- It has few files, but enough to prove the concept.
	-- Uncomment each line below to back up the folder it refers to; comment lines to omit folders.
	-- In my work environment I would typically only back up Desktop and Documents.
	
	--backupUserData(currentUser, "Desktop", backupDestination, adminUserName, adminPassword, designatedReviewer)
	--backupUserData(currentUser, "Documents", backupDestination, adminUserName, adminPassword, designatedReviewer)
	--backupUserData(currentUser, "Library", backupDestination, adminUserName, adminPassword, designatedReviewer)
	--backupUserData(currentUser, "Movies", backupDestination, adminUserName, adminPassword, designatedReviewer)
	--backupUserData(currentUser, "Music", backupDestination, adminUserName, adminPassword, designatedReviewer)
	--backupUserData(currentUser, "Pictures", backupDestination, adminUserName, adminPassword, designatedReviewer)
	--backupUserData(currentUser, "Public", backupDestination, adminUserName, adminPassword, designatedReviewer)
	backupUserData(currentUser, "Sites", backupDestination, adminUserName, adminPassword, designatedReviewer)
end repeat

(* SUBROUTINES *)
on backupUserData(theUsertoBackUp, theSubfolderToBackUp, theBackupDestination, theAdminUser, theAdminPass, theDesignatedUser)
	set the homeFolderToBackUp to getHomePath(theUsertoBackUp)
	
	-- Use ditto to copy the specified folder to our backup destination.
	do shell script "ditto " & quoted form of (the homeFolderToBackUp & "/" & theSubfolderToBackUp) & " " & quoted form of (theBackupDestination & theUsertoBackUp & "/" & theSubfolderToBackUp) user name theAdminUser password theAdminPass with administrator privileges
	
	-- Set ownership to the admin user.
	do shell script "chown -R " & theAdminUser & " " & quoted form of (theBackupDestination & theUsertoBackUp) user name theAdminUser password theAdminPass with administrator privileges
	
	-- Set the POSIX permissions to allow access only by the owner.
	do shell script "chmod -R 700 " & quoted form of (theBackupDestination & theUsertoBackUp) user name theAdminUser password theAdminPass with administrator privileges
	
	-- Remove any existing Access Control Entries on the backed up data.
	do shell script "chmod -RN " & quoted form of (theBackupDestination & theUsertoBackUp) user name theAdminUser password theAdminPass with administrator privileges
	
	-- Add an Access Control Entry to every backed up item, allowing read access by another designated user.
	do shell script "chmod -R +a# 0 " & quoted form of ("user:" & theDesignatedUser & " allow list,search,read") & " " & quoted form of (theBackupDestination & theUsertoBackUp) user name theAdminUser password theAdminPass with administrator privileges
end backupUserData

on getHomePath(userShortName)
	-- This subroutine returns the home directory path for the specified user.
	
	-- Get path to home folder for this user.
	set the homeDirectory to text 19 thru end of (do shell script "dscl . -read /Users/" & userShortName & "|grep NFSHomeDirectory")
	
	-- Return the home directory path.
	return the homeDirectory
end getHomePath

on getLocalUserList(userNameToIgnore)
	-- Get a list of the short names of local user accounts.
	
	--Create an empty list to hold the user names which are discovered.
	set the listOfUserNames to {}
	
	-- Get a list of UniqueIDs of local users on this computer.
	set the listOfUniqueIDs to getUserAccountUniqueIDs()
	
	-- Look at each user record.
	repeat with currentUniqueID in the listOfUniqueIDs
		
		-- Get the short name of the user associated with this UniqueID.
		set the currentUserName to the first word of (do shell script "dscl . -list /Users UniqueID|grep " & the currentUniqueID)
		
		-- If the short name of this user is a user we don't want to list, ignore it.
		if the currentUserName is not the userNameToIgnore then
			-- Add this short name to the list of local user names.
			set the listOfUserNames to the listOfUserNames & the currentUserName
		end if
	end repeat
	
	--Return with the list of local user names.
	return listOfUserNames
end getLocalUserList


on getUserAccountUniqueIDs()
	-- Local user account UniqueIDs begin at 500.
	-- This routine returns a list of local user account UniqueIDs.
	
	-- Create an empty list to hold the UniqueIDs which are discovered.
	set the listOfUniqueIDsGreaterThan500 to {}
	
	-- Get a list of all user records on the computer. This includes system daemons.
	set the listOfUserRecords to paragraphs of (do shell script "dscl . -list /Users UniqueID")
	
	-- Look at each user record.
	repeat with currentUserRecord in listOfUserRecords
		
		-- Get the UniqueID for the record.
		set the currentUserID to the (last word of the currentUserRecord as integer)
		
		-- If the Unique ID > 500, add it to the list we created earlier.
		if the currentUserID is greater than 500 then set the listOfUniqueIDsGreaterThan500 to listOfUniqueIDsGreaterThan500 & the currentUserID
	end repeat
	
	-- Return with the list we discovered.
	return listOfUniqueIDsGreaterThan500
end getUserAccountUniqueIDs

Open in new window

0
Comment
Question by:osxman
  • 5
  • 4
10 Comments
 
LVL 4

Expert Comment

by:Bryan_Vines
ID: 35494123
Hi OSXman,

This looks awfully familiar. :)  OK, so on any given workstation you've got a random assortment of user homes in /Users, is that correct?

Do you still want the resulting backups to end up in /Users/Shared/Backups?

--
Bryan Vines
0
 

Author Comment

by:osxman
ID: 35494312

"This looks awfully familiar. :)"

 Hi Bryan, I was hoping you would see this :O)

" OK, so on any given workstation you've got a random assortment of user homes in /Users, is that correct?"

Correct.

"Do you still want the resulting backups to end up in /Users/Shared/Backups?"

Ideally I would like the folder to appear in the root of the hard drive. /UserBack-ups

TIA



0
 
LVL 4

Expert Comment

by:Bryan_Vines
ID: 35494867
OK, I'll put something together over the weekend. Shouldn't be too difficult.

--
Bryan Vines
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 

Author Comment

by:osxman
ID: 35495017
brilliant!

0
 
LVL 12

Expert Comment

by:nxnw
ID: 35498019
This is on a tangent, but I question this strategy.
- What if a user made additional subfolders at the root of his home directory, and kept important things there?
- No email, address books or calendars (etc) that would be stored in the user's Library folder?
0
 

Author Comment

by:osxman
ID: 35498491
Thanks for your input nxnw

Students use our share point portal which includes, mail, calendar, etc. students don't actually create folders in their "local" user home folder as they use their AD network home directory. Default apps save path is either to their LOCAL my docs or LOCAL music folder. I am not interested in any settings they may have in local library because students roam from one mac to another and we have over 80! Macs are also locked down heavily so they don't get to customise their presence.

In my new configuration default save paths will be to their AD network share so this problem wont arise in future.

cheers






0
 
LVL 4

Accepted Solution

by:
Bryan_Vines earned 2000 total points
ID: 35502536
osxman,

Edit the exclusionList variable to omit folders from /Users.
Edit the listOfUserSubFoldersToBackUp variable to list the subfolders of a user's home folder you want to back up.
Edit the backupDestination variable to change where the backups are stored.


 
(* BACK UP USER HOMES *)
-- ©2011 Bryan Vines

-- PURPOSE:
-- This script will recursively move through each folder within /Users and copy the contents of 
-- Documents, Desktop and Music, storing them in /User Backups

(* THE SCRIPT *)
-- SETUP: 
--Define the login credentials of the administrator on the computer. 
-- Generally speaking, this is a bad idea, but if you save this script as an application and deny editing, it should be reasonably safe from most users' prying eyes.
set the adminUserName to "sysadmin"
set the adminPassword to "admin123"

-- The exclusion list is a list of home folder names to ignore.
-- Generally, you will want to exclude the Shared folder, and the folder belonging to your local admin account.
-- If there are others, add them to this list.
set the exclusionList to {"Shared", "sysadmin"}

-- Set the list of folders within a user's home to back up.
set the listOfUserSubFoldersToBackUp to {"Desktop", "Documents", "Music"}

-- Set the full path to your backup destination here. Make sure to include a trailing slash.
set the backupDestination to "/User Backups"

-- Set the path where the user home folders exist.
set the homePath to "/Users"

set the userHomeFolderList to getUserHomes(homePath, exclusionList)

repeat with currentUserHomeFolder in userHomeFolderList
	repeat with currentFolder in the listOfUserSubFoldersToBackUp
		
		-- Use ditto to copy the specified folder to our backup destination.
		do shell script "ditto " & quoted form of (the homePath & "/" & currentUserHomeFolder & "/" & currentFolder) & " " & quoted form of (the backupDestination & "/" & currentUserHomeFolder & "/" & currentFolder) user name adminUserName password adminPassword with administrator privileges
	end repeat
	-- Set the POSIX permissions to allow access by all users.
	do shell script "chmod  777 " & quoted form of (the backupDestination) user name adminUserName password adminPassword with administrator privileges
	do shell script "chmod -R 777 " & quoted form of (the backupDestination) user name adminUserName password adminPassword with administrator privileges
	
	-- Remove any existing Access Control Entries on the backed up data.
	do shell script "chmod -RN " & quoted form of (the backupDestination) user name adminUserName password adminPassword with administrator privileges
end repeat

(* SUBROUTINES *)
on getUserHomes(pathToHomes, exclusions)
	-- INPUT:
	--    POSIX path to directory containing user home folders.
	--    AppleScript list of home folder names to exclude.
	--
	-- OUTPUT:
	--    AppleScript list containing home folder names.
	
	set the listOfUserHomes to paragraphs of (do shell script "ls " & the pathToHomes)
	
	set the filteredListOfUserHomes to {}
	repeat with currentUserHome in listOfUserHomes
		if the currentUserHome is not in exclusions then set the filteredListOfUserHomes to the filteredListOfUserHomes & the currentUserHome
	end repeat
	
	return the filteredListOfUserHomes
end getUserHomes

Open in new window

0
 

Author Comment

by:osxman
ID: 35502899
Thanks i'll try it out and get back to you.
0
 

Author Closing Comment

by:osxman
ID: 35510677
As usual, first class solution. works exactly how I want it to.

Thanks!
0
 
LVL 4

Expert Comment

by:Bryan_Vines
ID: 35511913
I'm glad I could help.
--
Bryan Vines
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploystudio is a system which can be used to deploy OSX clients and servers within the small/medium or large business environments. The system is built onto of the OSX Server NetBoot system and uses images & workflows as its core assets. While work…
This is a tech scam I recently helped my parents through.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question