Link to home
Start Free TrialLog in
Avatar of pcgym
pcgymFlag for United States of America

asked on

Backup User Profile in BAT file

I would like to add the "My Documents" folder to the list of files that are backed up in this script.

I would also like to back it up to a UNC path or mapped drive.

Can anyone help with this?
:: ========================================
:: BKUPUSER.BAT
::
:: Copy user data to C_BACKUP\%ComputerName% folder
:: ========================================
@echo off

:: ----------------------------------------
:: Initialise Variables
:: ----------------------------------------
set PstFolder=%userprofile%\Local Settings\Application Data\Microsoft\Outlook
set Nk2Folder=%userprofile%\Application Data\Microsoft\Outlook
set FavoritesFolder=%userprofile%\Favorites
set SignaturesFolder=%userprofile%\Application Data\Microsoft\Signatures
set BackupFolder=%userprofile%\My Documents\c_backup\%computername%

:: ----------------------------------------
:: Prepare Folders
:: ----------------------------------------
md "%BackupFolder%\pst" 2>nul
md "%BackupFolder%\nk2" 2>nul
md "%BackupFolder%\signatures" 2>nul
md "%BackupFolder%\favorites" 2>nul
 
:: ----------------------------------------
:: Copy PST Files
:: ----------------------------------------
echo Copying PST files....
xcopy "%PstFolder%\*.pst" "%BackupFolder%\pst\" /y /q /r >nul 2>&1
if %errorlevel%==4 (
   echo.
   echo You must close Outlook before backing up it's PST files....
   echo.
   pause
   exit /b 4
)
 
:: ----------------------------------------
:: Copy NK2 Files
:: ----------------------------------------
echo Copying NK2 files....
xcopy "%Nk2Folder%\*.nk2" "%BackupFolder%\nk2\" /y /q /r >nul
 
:: ----------------------------------------
:: Copy Favorites
:: ----------------------------------------
echo Copying Favourites....
xcopy "%FavoritesFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /s /h 1>nul 2>errors.txt
 
:: ----------------------------------------
:: Copy Signatures
:: ----------------------------------------
echo Copying Outlook's signatures....
xcopy "%SignaturesFolder%\*.*" "%BackupFolder%\signatures\" /y /q /r >nul

Open in new window

Avatar of Indivara
Indivara
Flag of Japan image

You can use
xcopy sourcedir \\servername\sharename\folder\



Something like this?

Ps on line 18; if you set netbackup=0 it won't backup to network


:: ========================================
:: BKUPUSER.BAT
::
:: Copy user data to C_BACKUP\%ComputerName% folder
:: ========================================
@echo off

:: ----------------------------------------
:: Initialise Variables
:: ----------------------------------------
set PstFolder=%userprofile%\Local Settings\Application Data\Microsoft\Outlook
set Nk2Folder=%userprofile%\Application Data\Microsoft\Outlook
set FavoritesFolder=%userprofile%\Favorites
set SignaturesFolder=%userprofile%\Application Data\Microsoft\Signatures
set BackupFolder=%userprofile%\My Documents\c_backup\%computername%
set DocumentsFolder=%userprofile%\My Documents
set NetBackupFolder=\\fileserver\share\%username%\c_backup\%computername%
set netbackup=1

:Loop
:: ----------------------------------------
:: Prepare Folders
:: ----------------------------------------
md "%BackupFolder%\pst" 2>nul
md "%BackupFolder%\nk2" 2>nul
md "%BackupFolder%\signatures" 2>nul
md "%BackupFolder%\favorites" 2>nul
md "%BackupFolder%\My Documents" 2>nul
 
:: ----------------------------------------
:: Copy PST Files
:: ----------------------------------------
echo Copying PST files....
xcopy "%PstFolder%\*.pst" "%BackupFolder%\pst\" /y /q /r >nul 2>&1
if %errorlevel%==4 (
   echo.
   echo You must close Outlook before backing up it's PST files....
   echo.
   pause
   exit /b 4
)
 
:: ----------------------------------------
:: Copy NK2 Files
:: ----------------------------------------
echo Copying NK2 files....
xcopy "%Nk2Folder%\*.nk2" "%BackupFolder%\nk2\" /y /q /r >nul
 
:: ----------------------------------------
:: Copy Favorites
:: ----------------------------------------
echo Copying Favourites....
xcopy "%FavoritesFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /s /h 1>nul 2>errors.txt
 
:: ----------------------------------------
:: Copy Signatures
:: ----------------------------------------
echo Copying Outlook's signatures....
xcopy "%SignaturesFolder%\*.*" "%BackupFolder%\signatures\" /y /q /r >nul

:: ----------------------------------------
:: Copy My Documents
:: ----------------------------------------
echo Copying Favourites....
xcopy "%DocumentsFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /h /EXCLUDE:\c_backup\


:: ----------------------------------------
:: Loop for backup 2 Network
:: ----------------------------------------
if %netbackup%==1 goto Netbackup
goto End

:Netbackup
set BackupFolder=%NetBackupFolder%
set netbackup=0
goto loop

:End
Exit

Open in new window

Yes, it should work like a local folder.
(I assume you are creating the computername folder)

Avatar of pcgym

ASKER

I modified the UNC path for the NetBackupFolder, ran the script and I get the result in the attached screenshot.

Outlook is closed.

Any ideas?
:: ======================================== 
:: BKUPUSER.BAT 
:: 
:: Copy user data to C_BACKUP\%ComputerName% folder 
:: ======================================== 
@echo off 
 
:: ---------------------------------------- 
:: Initialise Variables 
:: ---------------------------------------- 
set PstFolder=%userprofile%\Local Settings\Application Data\Microsoft\Outlook 
set Nk2Folder=%userprofile%\Application Data\Microsoft\Outlook 
set FavoritesFolder=%userprofile%\Favorites 
set SignaturesFolder=%userprofile%\Application Data\Microsoft\Signatures 
set BackupFolder=%userprofile%\My Documents\c_backup\%computername% 
set DocumentsFolder=%userprofile%\My Documents 
set NetBackupFolder=\\it1\it\%username%\c_backup\%computername% 
set netbackup=1 
 
:Loop 
:: ---------------------------------------- 
:: Prepare Folders 
:: ---------------------------------------- 
md "%BackupFolder%\pst" 2>nul 
md "%BackupFolder%\nk2" 2>nul 
md "%BackupFolder%\signatures" 2>nul 
md "%BackupFolder%\favorites" 2>nul 
md "%BackupFolder%\My Documents" 2>nul 
  
:: ---------------------------------------- 
:: Copy PST Files 
:: ---------------------------------------- 
echo Copying PST files.... 
xcopy "%PstFolder%\*.pst" "%BackupFolder%\pst\" /y /q /r >nul 2>&1 
if %errorlevel%==4 ( 
   echo. 
   echo You must close Outlook before backing up it's PST files.... 
   echo. 
   pause 
   exit /b 4 
) 
  
:: ---------------------------------------- 
:: Copy NK2 Files 
:: ---------------------------------------- 
echo Copying NK2 files.... 
xcopy "%Nk2Folder%\*.nk2" "%BackupFolder%\nk2\" /y /q /r >nul 
  
:: ---------------------------------------- 
:: Copy Favorites 
:: ---------------------------------------- 
echo Copying Favourites.... 
xcopy "%FavoritesFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /s /h 1>nul 2>errors.txt 
  
:: ---------------------------------------- 
:: Copy Signatures 
:: ---------------------------------------- 
echo Copying Outlook's signatures.... 
xcopy "%SignaturesFolder%\*.*" "%BackupFolder%\signatures\" /y /q /r >nul 
 
:: ---------------------------------------- 
:: Copy My Documents 
:: ---------------------------------------- 
echo Copying Favourites.... 
xcopy "%DocumentsFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /h /EXCLUDE:\c_backup\ 
 
 
:: ---------------------------------------- 
:: Loop for backup 2 Network 
:: ---------------------------------------- 
if %netbackup%==1 goto Netbackup 
goto End 
 
:Netbackup 
set BackupFolder=%NetBackupFolder% 
set netbackup=0 
goto loop 
 
:End 
Exit

Open in new window

error1.bmp
Is it running in the background? In the taskbar notification area?
If not, open task manager (Ctrl+Shift+Esc) and see whether the process is still running there (outlook.exe)


Try removing the 2>&1 part and see what error is displayed.
I get "File creation error - The process cannot access the file because another process has locked a portion of the file." when Outlook is running

outlook-error.png
Avatar of pcgym

ASKER

Outlook.exe is not running.
Fixed:
- Errorlevel has a reset at the bottom
- Exclusion switch was not properly used..

:: ========================================
:: BKUPUSER.BAT
::
:: Copy user data to C_BACKUP\%ComputerName% folder
:: ========================================
@echo off

:: ----------------------------------------
:: Initialise Variables
:: ----------------------------------------
set PstFolder=%userprofile%\Local Settings\Application Data\Microsoft\Outlook
set Nk2Folder=%userprofile%\Application Data\Microsoft\Outlook
set FavoritesFolder=%userprofile%\Favorites
set SignaturesFolder=%userprofile%\Application Data\Microsoft\Signatures
set BackupFolder=%userprofile%\My Documents\c_backup\%computername%
set DocumentsFolder=%userprofile%\My Documents
set NetBackupFolder=\\fileserver\share\%username%\c_backup\%computername%
set netbackup=1
echo \c_backup\ > excludelist.txt

:Loop
:: ----------------------------------------
:: Prepare Folders
:: ----------------------------------------
md "%BackupFolder%\pst" 2>nul
md "%BackupFolder%\nk2" 2>nul
md "%BackupFolder%\signatures" 2>nul
md "%BackupFolder%\favorites" 2>nul
md "%BackupFolder%\My Documents" 2>nul
 
:: ----------------------------------------
:: Copy PST Files
:: ----------------------------------------
echo Copying PST files....
xcopy "%PstFolder%\*.pst" "%BackupFolder%\pst\" /y /q /r >nul 2>&1
if %errorlevel%==4 (
   echo.
   echo You must close Outlook before backing up it's PST files....
   echo.
   pause
   exit /b 4
)
 
:: ----------------------------------------
:: Copy NK2 Files
:: ----------------------------------------
echo Copying NK2 files....
xcopy "%Nk2Folder%\*.nk2" "%BackupFolder%\nk2\" /y /q /r >nul
 
:: ----------------------------------------
:: Copy Favorites
:: ----------------------------------------
echo Copying Favourites....
xcopy "%FavoritesFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /s /h 1>nul 2>errors.txt
 
:: ----------------------------------------
:: Copy Signatures
:: ----------------------------------------
echo Copying Outlook's signatures....
xcopy "%SignaturesFolder%\*.*" "%BackupFolder%\signatures\" /y /q /r >nul

:: ----------------------------------------
:: Copy My Documents
:: ----------------------------------------
echo Copying Favourites....
xcopy "%DocumentsFolder%\*.*" "%BackupFolder%\Favorites\" /y /r /q /h /EXCLUDE:


:: ----------------------------------------
:: Loop for backup 2 Network
:: ----------------------------------------
if %netbackup%==1 goto Netbackup
goto End

:Netbackup
set BackupFolder=%NetBackupFolder%
set netbackup=0
set errorlevel=0
goto loop

:End
del /Q excludelist.txt
Exit

Open in new window

Avatar of pcgym

ASKER

After removing "2>&1" I get the same error.
ASKER CERTIFIED SOLUTION
Avatar of Rory de Leur
Rory de Leur
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pcgym

ASKER

CRIPL, we are very close now.

Everything worked perfectly except the MyDocs folder was not backed up.  The folder was created but there were no files in it.
Indeed, line 35 remove "&1" at the end..