vEffort

Physical efforts in virtualization and mobility

Tag Archives: Script

Backing Up and Restoring User Profiles

Hot on the heels of my previous post, I was in a situation where I wanted a safety-net for backing up user profiles. “To the web! >>”  I found this which got me on the right path:

http://community.spiceworks.com/scripts/show/1575-user-profile-backup-and-restore

I didn’t want to use PowerShell (well I did but we had XP to deal with) so was pretty limited to batch. In the end I took the contents of the script in this article and spiced it up a bit:

  • Took the backup part and used it as a logon script which pushed the necessary content to a folder on the users home drive
  • Copied the restore batch file to the same users home drive folder
  • Updated the hard paths to use built in system variables
  • Updated locations to account for different Office versions from 2003 to 2013
  • Commented each section to describe what each line is doing
  • Used a check to see if Robocopy is present locally as it is not by default on XP.
    • If it is, assume the machine is Windows 7 or Vista and skip to that section that uses the %localappdata% variable
    • If it isn’t, assume the machine is XP which will temporarily map a drive to use robocopy on a remote location. Also uses the %userprofile%\Local Settings\Application Data path in the absence of %localappdata%
  • Added a logging option which gives a date and time of user logins and what machines they are logging onto.

The backup script is below:

:: For XP Machines that will not have Robocopy available locally and also no %localappdata% variable
if not exist %systemroot%\system32\robocopy.exe (
 net use v: \\domain.local\NETLOGON
 ::Backup Outlook signature files
 v:\robocopy.exe %appdata%\Microsoft\Signatures %homeshare%\ProfileBackup\Signatures *.* /e
 ::Backup NK2 cache for Outlook 2003/2007
 v:\robocopy.exe %appdata%\Microsoft\Outlook %homeshare%\ProfileBackup\NK2 *.nk2
 ::Backup NK2 cache for Outlook 2010/2013
 v:\robocopy.exe "%userprofile%\Local Settings\Application Data\Microsoft\Outlook\RoamCache" %homeshare%\ProfileBackup\Local *.dat
 ::Backup Internet Explorer Favorites
 v:\robocopy.exe %userprofile%\Favorites %homeshare%\ProfileBackup\Favorites *.* /e
 ::Backup all template files in AppData including normal.dot
 v:\robocopy.exe %appdata%\Microsoft\Templates %homeshare%\ProfileBackup\Templates\*.dot
 ::Backup customised Quick Access Toolbars in Office
 v:\robocopy.exe "%userprofile%\Local Settings\Application Data\Microsoft\Office" %homeshare%\ProfileBackup\Local *.Officeui
 ::Backup Office auto correct files
 v:\robocopy.exe %appdata%\Microsoft\Office %homeshare%\ProfileBackup\Roaming *.acl
 ::Backup custom dictionaries for Office
 v:\robocopy.exe %appdata%\Microsoft\Uproof %homeshare%\ProfileBackup\Roaming *.dic
 net use v: /delete
)

:: carrying on it Vista or Win7 backup the profile data to the users home drive
if exist %systemroot%\system32\robocopy.exe (
 ::Backup Outlook signature files
 Robocopy %appdata%\Microsoft\Signatures %homeshare%\ProfileBackup\Signatures *.* /e
 ::Backup NK2 cache for Outlook 2003/2007
 Robocopy %appdata%\Microsoft\Outlook %homeshare%\ProfileBackup\NK2 *.nk2
 ::Backup NK2 cache for Outlook 2010/2013
 Robocopy %localappdata%\Microsoft\Outlook\RoamCache %homeshare%\ProfileBackup\Local *.dat
 ::Backup Internet Explorer favorites
 Robocopy %userprofile%\Favorites %homeshare%\ProfileBackup\Favorites *.* /e
 ::Backup all template files in AppData including normal.dot
 Robocopy %appdata%\Microsoft\Templates %homeshare%\ProfileBackup\Templates\ *.dot
 ::Backup customised Quick Access Toolbars in Office
 Robocopy %localappdata%\Microsoft\Office %homeshare%\ProfileBackup\Local *.Officeui
 ::Backup Office autocorrect files
 Robocopy %appdata%\Microsoft\Office %homeshare%\ProfileBackup\Roaming *.acl
 ::Backup custom dictionaries for Office
 Robocopy %appdata%\Microsoft\Uproof %homeshare%\ProfileBackup\Roaming *.dic
)

:: copy the restore script to the users home drive so they can run a restore if required
copy "\\domain.local\NETLOGON\RestoreProf.bat" %homeshare%\ProfileBackup\ /y

:: output the logging on users to text file for reference to see what desktops they are logging on
echo %username%,%computername%,%date%,%time% >> \\domain.local\dfs\profiles\LiveLogonInfo.txt
Next create a RestoreProf.bat like this
@Echo off

:: Set log files variable, each line appends to the respective log file
SET LOGW7VISTA=%homeshare%\ProfileBackup\ProfileRestoreW7Vista.log
SET LOGXP=%homeshare%\ProfileBackup\ProfileRestoreXP.log

:: For XP Machines that will not have Robocopy available locally and also no %localappdata% variable
if not exist %systemroot%\system32\robocopy.exe (
 net use v: \\domain.local\NETLOGON >NUL
 ::Restore Outlook signature files
 v:\robocopy.exe %homeshare%\ProfileBackup\signatures %appdata%\Microsoft\Signatures *.* /e /NC /NS /NP >> %LOGXP%
 ::Restore NK2 cache for Outlook 2003/2007
 v:\robocopy.exe %homeshare%\ProfileBackup\NK2 %appdata%\Microsoft\Outlook *.* /e /NC /NS /NP >> %LOGXP%
 ::Restore NK2 cache for Outlook 2010/2013
 v:\robocopy.exe %homeshare%\ProfileBackup\Local "%userprofile%\Local Settings\Application Data\Microsoft\Outlook\RoamCache" *.*
 ::Restore Internet Explorer Favorites
 v:\robocopy.exe %homeshare%\ProfileBackup\Favorites %userprofile%\Favorites *.* /e
 ::Restore all Template files in AppData including normal.dot
 v:\robocopy.exe %homeshare%\ProfileBackup\Templates %appdata%\Microsoft\Templates *.* /e /NC /NS /NP >> %LOGXP%
 ::Restore customised Quick Access Toolbars in Office
 v:\robocopy.exe %homeshare%\ProfileBackup\Local "%userprofile%\Local Settings\Application Data\Microsoft\Office" *.Officeui /e /NC /NS /NP >> %LOGXP%
 ::Restore Office autocorrect files
 v:\robocopy.exe %homeshare%\ProfileBackup\Roaming %appdata%\Microsoft\Office *.acl /e /NC /NS /NP >> %LOGXP%
 ::Restore custom dictionaries for Office
 v:\robocopy.exe %homeshare%\ProfileBackup\Roaming %appdata%\Microsoft\Uproof *.dic /e /NC /NS /NP >> %LOGXP%
 net use v: /delete >NUL
)

:: carrying on if Vista or Win7 restore the profile data to the users local profile for upload at logoff
if exist %systemroot%\system32\robocopy.exe (
 ::Restore Outlook signature files
 Robocopy %homeshare%\ProfileBackup\Signatures %appdata%\Microsoft\Signatures *.* /e /NC /NS /NP >> %LOGW7VISTA%
 ::Restore NK2 cache for Outlook 2003/2007
 Robocopy %homeshare%\ProfileBackup\NK2 %appdata%\Microsoft\Outlook *.* /e /NC /NS /NP >> %LOGW7VISTA%
 ::Restore NK2 cache for Outlook 2010/2013
 Robocopy %homeshare%\ProfileBackup\Local %localappdata%\Microsoft\Outlook\RoamCache *.* /e /NC /NS /NP >> %LOGW7VISTA%
 ::Restore Internet Explorer Favorites
 Robocopy %homeshare%\ProfileBackup\Favorites %userprofile%\Favorites *.* /e
 ::Restore all Template files in AppData including normal.dot
 Robocopy %homeshare%\ProfileBackup\Templates %appdata%\Microsoft\Templates *.* /e /NC /NS /NP >> %LOGW7VISTA%
 ::Restore customised Quick Access Toolbars in Office
 Robocopy %homeshare%\ProfileBackup\Local %localappdata%\Microsoft\Office *.Officeui /e /NC /NS /NP >> %LOGW7VISTA%
 ::Restore Office autocorrect files
 Robocopy %homeshare%\ProfileBackup\Roaming %appdata%\Microsoft\Office *.acl /e /NC /NS /NP >> %LOGW7VISTA%
 ::Restore custom dictionaries for Office
 Robocopy %homeshare%\ProfileBackup\Roaming %appdata%\Microsoft\Uproof *.dic /e /NC /NS /NP >> %LOGW7VISTA%
)

@Echo profile settings restored
Pause

This will be copied to the users home drives from the logon script (so long as you put it in the right place) and will create a folder structure like this

Folderlook

Restoring files

So the idea here is that if there is an issue, the user can manually run the batch file, which may be a problem if you restrict the running of these file types. I see this as a useful tool though to be used as a matter of course. If everyone’s profiles are backup in such a way and you have a service desk with a penchant for flushing user profiles this will make the process easier because if the login script runs at every logon, the folder will always be up-to-date. After a profile flush the service desk can either insruct the user to run the file or they can run it as admin to drop all of the files in the correct place.

I have only found one slight issue with the restore method so far in that the signature file is somewhat orphaned. It’s there in Outlook, it just needs assigning as per screenshot below:

Sig

%d bloggers like this: