Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Batch file to copy to server with users logged off

Status
Not open for further replies.
Oct 7, 2007
6,597
US
Here's what I'm trying to do. Copy the Outlook files off workstations to the 2003 server for backup purposes (no Exchange involved, just stand-alone Outlook).

So, I want to copy the contents of the outlook folder to the server in the following location:
\\server\share\mailbackup\username

I want it to be able to run as a scheduled task AND regardless of whether the user is logged ON or OFF. So I guess I need to map a drive IN CASE the user is logged off. That's where I'm getting confused - whether you can map a network drive with the user logged off or if I should be using the \\ to address the server. If the user were to remain logged on, there would already be a mapped drive, but I don't want users remaining logged on, so it must work whether or not they are logged on or off.

Assuming that the user is logged off - am I on the right track here??



@echo on

net use x: "\\server1\shared\" /persistent:yes

rem copy the backup to the server
xcopy "C:\Documents and Settings\user\Local Settings\Application Data\Microsoft\Outlook\*.*" "x:\mailbackup\user1" /y

rem release mapped drive
net use x: /d /y

rem exit the batch file
exit
 
You can map drive when user is logged off. You should run scheduled task as this user and in fact this user will log on when task starts.

There is one problem with this scenario, if user changes password, task will not run. I am running backup tasks under specially for this purpose made domain account, member of Domain Admins group, with 'Password never expires' set.

===
Karlis
ECDL; MCSA
 
But can you check the syntax of the batch file to verify that it looks correct? I've been struggling with how to make it work.
 
Why persistent mapping if you release it in the end. If your script crashes, mapping should not stay, I think

[tt]net use x: \\server1\shared /persistent:no[/tt]

Don't know much about xcopy syntax, I am using 'copy' and it works good for me.

[tt]copy /z /y /v "C:\Documents and Settings\user\Local Settings\Application Data\Microsoft\Outlook\*.*" "x:\mailbackup\user1\*.*"[/tt]

Don't need /y switch here

[tt]net use x: /d[/tt]

===
Karlis
ECDL; MCSA
 
That utility only backs up the data if I understand it, NOT copy it to the server for backup, which is my goal.
 
If you have a network mapped drive "H" this might help. If not try changing the destination. Depending on your version of Outlook you may need to adjust the path. This has worked with Outlook 2003.

rem Please don't remove either the text file or the bat file in this folder
rem Batch File to Backup Selected Folders and Files to mapped drive
set temp=
set tempDr=
set temp=%username%
if exist H:\ (set tempDR=H)
if exist H:\ (mkdir H:\OutlookFolder)
if exist "C:\Documents and Settings\%temp%\Local Settings\Application Data\Microsoft\Outlook" (xcopy "C:\Documents and Settings\%temp%\Local Settings\Application Data\Microsoft\Outlook" "%tempDr%:\OutlookFolder")
 
That utility only backs up the data if I understand it, NOT copy it to the server for backup, which is my goal.

I'm not sure I'm seeing the logic here. The Outlook Backup utility, which I've used in a few different situations, myself, can be used to back the personal folder file up to a network location, just the same as a local drive.

When you say whether the user is logged on or off, what do you mean? If the user is not logged on, then no changes, so why would you need a new backup?
 
Why do you need to map a drive at all? Wouldn't the below work?

xcopy "C:\Documents and Settings\user\Local Settings\Application Data\Microsoft\Outlook\*.*" "\\server1\shared\mailbackup\user1" /y
 
The other way around would be a script running on the server that will connect to each machine to get the files

Cheers,
Dian
 
Yeah, that reminds me... another excellent Sync/backup tool - SyncBack. I can't think there is any better.. well, maybe... but I know this one works very well across XP, Vista, and 7, both with network and local drives...
 
I didn't understand your comment KJV, but forget about that utility enirely for a moment as I want to try to do it with it a batch file that runs on the client workstations and will copy to the server whether they are logged in or not.

So, maybe this is the winner per guitarzan:
I have to wait to try it.

@echo on

rem copy the backup to the server
xcopy "C:\Documents and Settings\user\Local Settings\Application Data\Microsoft\Outlook\*.*" "\\server1\shared\mailbackup\user1" /y

rem exit the batch file
exit


But my question is whether an UNlogged in workstation will understand "\\server1\shared\mailbackup\user1" /y
without being logged in.

 
You know, on mentioning SyncBack, I didn't think about this being a business situation. It's probably only "free for personal use", meaning you'd have to pay for it... so it wouldn't be the best option if cost is a concern.

goomb,

I still don't understand.. I mean, that doesn't matter - my understanding, but why does this part matter?
goombawaho said:
whether they are logged in or not
I mean if it is run while they are not logged in, then what changes will it pick up? It's not like copying a typical .pst file is going to cause some serious networking or system resource issues while the PC is in use.

I may be totally off, but I just can't see a reason to bother with that part..
 
But my question is whether an UNlogged in workstation will understand "\\server1\shared\mailbackup\user1" /y
without being logged in.

Yes, it will work.

You may also be better off using Diancecht's idea of running the script on the server, and having it pull the files from each workstation. Depending on security settings, you may be able to do something like:
xcopy "\\workstation1\c$\Documents and Settings\username\blahblah\outlook.pst" "C:\Backups\Workstation1" /y

If you want even more control, a vbscript can do fancier things like ping the workstation before attempting the copy, write to log files, send the log via email, etc.
 
kjv1611 - let me assplain myself to you more better.

I wanted something that would work whether or not the user was logged in. I have asked the users to always log off (but not shut down). But you know users - some follow instructions and some do not.

So, I needed a script that wouldn't be "broken" and fail due to either being logged in or out, but one script that worked in either case. I don't really care if they follow instructions or not, but I want that script to run once a week to backup their PST and save my rear end because I promised that it would get backed up.

Are you following me??? You're looking at the trees, while I'm looking at the forest of users. Unless we're totally still not on the same page.

The utility that GrimR gave the link for didn't say anything about copying - only that it backs up your PST file. Great, but I don't care if it only gets put on the same hard drive which is a recipe for disaster. So, NO - I didn't try that utility and maybe somebody can tell me if it actually will back up the file to another location other than the local hard drive.

I'll report back when I get something implemented.
 
If you specify some user account and password for your scheduled task, it will run like if user started this by himself, even if this user currently isn't logged in. In fact this user logs in silently. This works even if another user is currently working on this computer.

===
Karlis
ECDL; MCSA
 
Thanks, but that last long explanation was mostly for kjv1611
 
The utility that GrimR gave the link for didn't say anything about copying - only that it backs up your PST file. Great, but I don't care if it only gets put on the same hard drive which is a recipe for disaster. So, NO - I didn't try that utility and maybe somebody can tell me if it actually will back up the file to another location other than the local hard drive.
Yes it does backup to UNC path etc. I deployed it to everyone via GPO.

One thing I have noticed is that no one has yet mentioned the fact you need to close Outlook before you can copy a pst file. So although you can make a script to backup, you would also need to create one to close outlook and kill the process.

What you would need to do scripting way.
[untested]
Create scheduled task
Code:
Dim strLocalfile, newfolder, newfolderpath, strCurrentFolder
Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim WshShell:Set WshShell = WScript.CreateObject("WScript.Shell")
Dim WshEnv:Set WshEnv = WshShell.Environment("Process")
SysDrive = WshEnv("%SYSTEMDRIVE%")
SysRoot = WshEnv("SYSTEMROOT")
LogonSvr = WshEnv("LOGONSERVER")
Const OverwriteExisting = True
'-------------------------------------------------------------------------------------------------------------------------
'Do not run on these machines (list of servers)
Dim ObjNetwork:Set objNetwork = CreateObject("WScript.Network")
	thisComputer = objNetwork.ComputerName
	arrComputers = Array("servername","servername","servername")
For Each arrayElement in arrComputers
	if arrayElement = thisComputer then
		wscript.Quit
	end if 
next
'----------------------------------------------------------------------------------------------------------------------------
'CreateNewFolder
newfolderpath = SysRoot & "\MyProgs"

If Not objFSO.FolderExists(newfolderpath) Then
 Set newfolder = objFSO.CreateFolder(newfolderpath)
End If
'----------------------------------------------------------------------------------------------------------------------------
'Now copy the encoded script file to users machine
strLocalfile = SysRoot & "\MyProgs\" 

If objFSO.FileExists(strLocalfile)=false Then
	objFSO.CopyFile  "\\server\share\backup.vbs" , SysRoot & "\MyProgs\" , OverwriteExisting = true
end if
'----------------------------------------------------------------------------------------------------------------------------
'Time to create a new Scheduled Task
StartTime = "1300" 
StartString = StartTxt(StartTime)

'Configure the days to run the script. Only change Yes/No values.
Rundays=vbEmpty
Rundays = Rundays & RunTxt(Monday, "No", "1")
Rundays = Rundays & RunTxt(Tuesday, "No","2")
Rundays = Rundays & RunTxt(Wednesday, "No","4")
Rundays = Rundays & RunTxt(Thursday, "No","8")
Rundays = Rundays & RunTxt(Friday, "Yes","16")
Rundays = Rundays & RunTxt(Saturday, "No","32")
Rundays = Rundays & RunTxt(Sunday, "No","64")


If Left(Rundays,4) = " OR " Then
  Rundays = Right(Rundays,(Len(Rundays)-4))
End If

BuildTask StartString, eval(Rundays)

Sub BuildTask(StartString, Rundays)
      
      Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      
      Set objNewJob = objWMIService.Get("Win32_ScheduledJob")
             errJobCreated = objNewJob.Create (SysRoot & "\MyProgs\BackupPST.vbs", StartString, False , Rundays, , ,"BackupPST")
End Sub

Function StartTxt(StartTime)
If StartTime = "1301" Then StartTime = "1300"
	'First get the systems time bias
	wmiQuery = "Select * from Win32_TimeZone"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery(wmiQuery)
	For Each objItem in colItems
	  TimeBias = objItem.Bias
	Next
	If TimeBias>=0 Then
	  TimeBias="+" & right("000" & TimeBias,3)
	else
	  TimeBias="-" & right("000" & abs(TimeBias),3)
	end If
'Now build the time string
StartTxt = "********" & StartTime & "00.000000" & TimeBias
End Function


Function RunTxt(dayUnused,RunYesNo,DayVal)
  If Ucase(RunYesNo) = "YES" Then
             RunTxt = " OR " & DayVal & " "
  Else
      RunTxt = ""
  End If
End Function
'----------------------------------------------------------------------------------------------------------------------------
'Now that WMI created the task as AT1 lets Rename  the Scheduled Task to something more meaningful
strCurFol = SysRoot & "\Tasks"
objFSO.MoveFile strCurFol & "\At1.job" , strCurFol & "\BackupPst.job"
'----------------------------------------------------------------------------------------------------------------------------
Create backup of PST
Code:
Dim objNetwork:Set objNetwork = CreateObject("WScript.Network")
SysDrive = WshEnv("%SYSTEMDRIVE%")
strName = objNetwork.UserName

On Error Resume Next
'Close Outlook
Dim oOutLook:Set oOutLook = GetObject(, "Outlook.Application")
If oOutLook Is Nothing Then
		'Outlook is not running
	Else
		oOutLook.Session.Logoff
		oOutLook.Quit
End If
On Error Goto 0

If CStr(strDay) = CStr(Weekday(Now())) Then
'Reads key from registry to see if we have already backed up today
	strTemp = WshShell.RegRead("HKLM\Software\MYCOMPANY\PSTCopyFlag")
	
  If CStr(strTemp) <> CStr(Date) Then
   
strDest = "\\server\Backups\PST\" & strName & "\"
strSource = SysDrive & "\Documents and Settings\" & strName & "\Local Settings\Application Data\Microsoft\Outlook\*.pst"

If Not objFSO.FolderExists(strDest) Then
  objFSO.CreateFolder strDest
End If
objFSO.CopyFile  strSource, strDest, True

    Set objFSO = Nothing
    'Set the FLAG
    WshShell.RegWrite "HKLM\Software\MYCOMPANY\PSTCopyFlag", Date
  End If
End If


MCITP:EA/SA, MCSE, MCSA, MCDBA, MCTS, MCP+I, MCP
 
> One thing I have noticed is that no one has yet mentioned the fact you need to close Outlook before you can copy a pst file

Absolutely correct, I meant to mention that and forgot. Although, goombawaho may be looking for a more "quick and dirty" solution, in which case forcing a restart of the computers and waiting x minutes before backing up might be sufficient.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top