Solved

VBScript - Custom BackUp

Posted on 2007-03-22
47
420 Views
Last Modified: 2010-05-18
Hi I am a vbscript beginner but need a script that will do the following.
Backup files to a network share from 4 different locations
1) My Documents
2) Favourites
3) Desktop
4) C:\Email
5  c:|BackUp
to a network  drive - lets say \\servername\foldername (and then My Document/Favourites/Desktop etc etc)
I need it to show progress (which can be minimised) and report any failures or success at the end of back-up.
I also need it to prompt the user to exit outlook at the start .
This is probobly a relatively easy one but scripting is my achilles heal.

thanks to anyone who helps.
0
Comment
Question by:William_Conway
  • 24
  • 23
47 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 18771503
Personally, I think you'd be better off using a product like robocopy.
It has the capability to mirror the source and target.  To do so with vbscript would require a fair amount of code...
0
 

Author Comment

by:William_Conway
ID: 18771528
Providing it does what it says on the tin (as above) I'm not too bothered.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18771660
Robocopy is located here...http://www.microsoft.com/downloads/details.aspx?FamilyID=9d467a69-57ff-4ae7-96ee-b18c4790cffd&DisplayLang=en

Kind of a combination of vbscript and the robocopy tool, set up your script like this:

'Backup.vbs
strResp=Msgbox("Outlook should be closed.  Are you ready to continue?", vbYesNo, "Backup")
If strResp=No Then wscript.quit

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")

With objShell
  UserName=.ExpandEnvironmentStrings("%Username%")
  MyDocuments=.ExpandEnvironmentStrings("%UserProfile%") & "\My Documents\"
  Desktop=.ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\"
  Favorites=.ExpandEnvironmentStrings("%UserProfile%") & "\Favorites\"
End With
Email="C:\Email\"
Backup="C:\backup\"

arrFolders=Array(MyDocuments, Favorites, Desktop, Email, Backup)
strTarget="\\Server\Backups\" & username
If not objFSO.FolderExists(strTarget) Then objFSO.CreateFolder(strTarget)

For Each Item in arrFolders
  objShell.Run "robocopy " & Item & " " & strTarget & " /tee /mir /r:3 /zb /log+" & strTarget & ".log"
Next

Set objFSO=Nothing
Set objShell=Nothing
'End code


The arguments do this:
 /tee = display onscreen status (as well as log to file)
/mir = mirror the source to the directory (if a source file is deleted it will be deleted from the backup - if you don't want this turn it off and use /e instead
 /r:3 = retry open files 3 times, then give up and move on...
 /zb = restartable backup mode - will retry a file if a network hiccup occurs.  Will attempt a copy in 'backup' mode - if the file is in use
 /log+" & strTarget & ".log" = records the log file at the root of the target in the format of username.log
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:William_Conway
ID: 18771976
Its giving the outlook prompt - creating the backup folder but then it doesn't copy anything - no error
A dos screen pops up momentarily after the outlook prompt but nothing else.
any ideas?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18772009
Did it create the log file?
0
 

Author Comment

by:William_Conway
ID: 18772087
No log file
modified script as below

'Backup.vbs
strResp=Msgbox("Outlook should be closed.  Are you ready to continue?", vbYesNo, "Backup")
If strResp=No Then wscript.quit

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")

With objShell
  UserName=.ExpandEnvironmentStrings("%Username%")
  MyDocuments=.ExpandEnvironmentStrings("%UserProfile%") & "\My Documents\"
  Desktop=.ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\"
  Favorites=.ExpandEnvironmentStrings("%UserProfile%") & "\Favorites\"
End With
Email="C:\Email\"
Backup="C:\backup\"

arrFolders=Array(MyDocuments, Favorites, Desktop, Email, Backup)
strTarget="\\Server\Share\BackUp\" & username
If not objFSO.FolderExists(strTarget) Then objFSO.CreateFolder(strTarget)

For Each Item in arrFolders
  objShell.Run "robocopy " & Item & " " & strTarget & " /tee /mir /r:3 /zb /log+" & strTarget & ".log"
Next

Set objFSO=Nothing
Set objShell=Nothing
'End code

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18772100
use this for/next and let me know what the output is...

For Each Item in arrFolders
  wscript.echo "robocopy " & Item & " " & strTarget & " /tee /mir /r:3 /zb /log+" & strTarget & ".log"
  objShell.Run "robocopy " & Item & " " & strTarget & " /tee /mir /r:3 /zb /log+" & strTarget & ".log"
Next
0
 

Author Comment

by:William_Conway
ID: 18772183
robocopy c:\Documents and Settings\(My Username)\My Documents\ \\server\share\backup\(My Username) /tee /mir /r:3 /zb /log+\\server\share\backup\(My Username).log

followed by a head scratch and a d'oh
0
 

Author Comment

by:William_Conway
ID: 18772203
Then the same message for favourites and desktop with keyword replaced i.e my documents = favorites - but not for c:\Backup or c:\email
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18772209
Bingo - spaces in the path... :^)

Replace with this:

For Each Item in arrFolders
  objShell.Run "robocopy " & chr(34) & Item & chr(34) & " " & chr(34) & strTarget & chr(34) & " /tee /mir /r:3 /zb /log+" & chr(34) & strTarget & ".log" & chr(34)
Next
0
 

Author Comment

by:William_Conway
ID: 18772221
Apologies c:\backup and c:\wemail does actually show up also
0
 

Author Comment

by:William_Conway
ID: 18772394
Still no joy - no log no copies.
Will
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18772441
replace this block again...post the output (just one instance is fine)

For Each Item in arrFolders
  wscript.echo "robocopy " & chr(34) & Item & chr(34) & " " & chr(34) & strTarget & chr(34) & " /tee /mir /r:3 /zb /log+" & chr(34) & strTarget & ".log" & chr(34)
'  objShell.Run "robocopy " & chr(34) & Item & chr(34) & " " & chr(34) & strTarget & chr(34) & " /tee /mir /r:3 /zb /log+" & chr(34) & strTarget & ".log" & chr(34)
Next

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18772452

It should read:

robocopy "c:\Documents and Settings\(My Username)\My Documents\" "\\server\share\backup\(My Username)" /tee /mir /r:3 /zb /log+"\\server\share\backup\(My Username).log"
0
 

Author Comment

by:William_Conway
ID: 18772557
It reads as it should  (As Above)- so the next thing i tried was manually typing it in
I get error : No Destination Directory Specified
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18772602
Typing it manually, try this...it may just be that it needs that last trailing \

robocopy "c:\Documents and Settings\(My Username)\My Documents\" "\\server\share\backup\(My Username)\" /tee /mir /r:3 /zb /log+"\\server\share\backup\(My Username).log"

^ added the \ just before the /tee
0
 

Author Comment

by:William_Conway
ID: 18772697
From the below it looks to me like its not setting the source and destination correctly - not sure how to correct this though.
Rgds,
Will

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

P:\>c:

C:\>robocopy "c:\Documents and Settings\kw3199\My Documents\" "\\server\share\backup\username\" /tee /mir /r:3 /zb /log+"\\server\share\backup\username.log"

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows     ::     Version XP010
-------------------------------------------------------------------------------

  Started : Thu Mar 22 16:38:41 2007

   Source : c:\Documents and Settings\username\My Documents" \server\sharebackup\username"\
     Dest : C:\log+\server\share\backup\username.log\

    Files : *.*

  Options : *.* /TEE /S /E /COPY:DAT /PURGE /MIR /ZB /R:3 /W:30

------------------------------------------------------------------------------

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18772758
Try this one...

For Each Item in arrFolders
  wscript.echo  "robocopy """ & Item & """ """ & strTarget & """ /tee /mir /r:3 /zb /log+"""" & strTarget & ".log"""
  objShell.Run "robocopy """ & Item & """ """ & strTarget & """ /tee /mir /r:3 /zb /log+"""" & strTarget & ".log"""
Next
0
 

Author Comment

by:William_Conway
ID: 18772798
line 22 char 117 unterminated string constant
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18772825
Thought Id checked that...try this..

wscript.echo "robocopy """ & Item & """ """ & strTarget & """ /tee /mir /r:3 /zb /log+""" & strTarget & ".log"""
0
 

Author Comment

by:William_Conway
ID: 18772923
No destination provided and the source seems to be the source plus destination.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18778829
It really shouldn't be this difficult.
I just did something similar a couple of weeks ago at work... : (


'Backup.vbs
strResp=Msgbox("Outlook should be closed.  Are you ready to continue?", vbYesNo, "Backup")
If strResp=No Then wscript.quit

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")

With objShell
  UserName=.ExpandEnvironmentStrings("%Username%")
  MyDocuments=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\My Documents\" & chr(34)
  Desktop=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\" & chr(34)
  Favorites=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Favorites\" & chr(34)
End With
Email="C:\Email\"
Backup="C:\backup\"

arrFolders=Array(MyDocuments, Favorites, Desktop, Email, Backup)
strTarget=chr(34) & "\\Server\Share\BackUp\" & username & chr(34)
strLog=chr(34) & "\\Server\Share\BackUp\" & username & ".log" & chr(34)
If not objFSO.FolderExists(strTarget) Then objFSO.CreateFolder(strTarget)

For Each Item in arrFolders
  objShell.Run "robocopy " & Item & strTarget & " /tee /mir /r:3 /zb /log+" & strLog
'If this fails to work, please comment the above line (or remove it) and uncomment the next line: and let me know the results
'  objShell.Run "robocopy " & Item & strTarget & " /mir /r:3 /zb"
Next

Set objFSO=Nothing
Set objShell=Nothing
'End code
0
 

Author Comment

by:William_Conway
ID: 18778927
Bad File Name or Number i echodied the strtarget variable and it is right the problem seems to be in the below line
If not objFSO.FolderExists(strTarget) Then objFSO.CreateFolder(strTarget)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18778936
Alter it to this...

If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
  objFSO.CreateFolder(strTarget)
End If

see if it errors at the same point (should see the echo line above)
0
 

Author Comment

by:William_Conway
ID: 18778979
Error remains pointing at the below line - I do see the echo and that appears correct.

  objFSO.CreateFolder(strTarget)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18779011
What is strTarget set to?
Could it be that the 'parent' isn't created yet?

strTarget is set to \\server\share\backup\username

If backup isn't there, it'll fail...
0
 

Author Comment

by:William_Conway
ID: 18779513
have double checked everything the paths are definetly right and the parent folde rexist - even if i create the username folder i still get that error.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18779536
<Smack> - sorry...I think I see the error...sorry for the trouble...remove the paren:

objFSO.CreateFolder strTarget

If that doesn't do it, then I won't feel so bad...but I think it will.
If not, try
set objFolder=objFSO.CreateFolder (strTarget)
0
 

Author Comment

by:William_Conway
ID: 18779724
Still bad filename or number
'Backup.vbs
strResp=Msgbox("Outlook should be closed.  Are you ready to continue?", vbYesNo, "Backup")
If strResp=No Then wscript.quit

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")

With objShell
  UserName=.ExpandEnvironmentStrings("%Username%")
  MyDocuments=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\My Documents\" & chr(34)
  Desktop=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\" & chr(34)
  Favorites=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Favorites\" & chr(34)
End With
Email="C:\Email\"
Backup="C:\backup\"

arrFolders=Array(MyDocuments, Favorites, Desktop, Email, Backup)
strTarget=chr(34) & "\\server\share\BackUp\" & username & chr(34)

wscript.echo strtarget

strLog=chr(34) & "\\server\share\BackUp\" & username & ".log" & chr(34)

If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
set objFolder=objFSO.CreateFolder (strTarget)
End If


For Each Item in arrFolders
  objShell.Run "robocopy " & Item & strTarget & " /tee /mir /r:3 /zb /log+" & strLog
'If this fails to work, please comment the above line (or remove it) and uncomment the next line: and let me know the results
'  objShell.Run "robocopy " & Item & strTarget & " /mir /r:3 /zb"
Next

Set objFSO=Nothing
Set objShell=Nothing
'End code
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18785085
This line is being changed for your environment, correct?
strTarget=chr(34) & "\\server\share\BackUp\" & username & chr(34)
0
 

Author Comment

by:William_Conway
ID: 18791361
Yeah definetly even when you echo it it appears as it should be. I think its going wrong where is differentiates between the source and the destination. Because the destination is always blank or only a few letters and the source is always more than it should be
i.e source c:\backup \\server\share and then destination could be username or something similar. It doesnt seem to see where one ends and the other begins.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18791630
I don't mind working at this until we get it, if you don't....
I know it's possible cause I just did this for a data migration last month.

That said, we'll start with a slimmed down version at first to see if we can get this working...

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")
Username="Testing"
Email="C:\Email\"

strTarget=" " & chr(34) & "\\server\share\BackUp\" & username & chr(34)

If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
  set objFolder=objFSO.CreateFolder (strTarget)
End If

objShell.Run "robocopy " & Email & strTarget & " /tee /mir /r:3 /zb /log+C:\Testing.log"

Set objFSO=Nothing
Set objShell=Nothing
'End code
0
 

Author Comment

by:William_Conway
ID: 18791815
bad file name or number line 10 char 3
Befor that i get an echo of "\\Server\Share\Backup\testing"

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18791822
With that version, the only echo you should get would be
wscript.echo strTarget & " doesn't exist...creating it..."
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18791834
Do these 3 lines work? (change \\server share below)

Dim objShell:Set objShell=CreateObject("Wscript.Shell")
objShell.Run "robocopy C:\Email \\server\share\backup\testing"
Set objShell=Nothing
0
 

Author Comment

by:William_Conway
ID: 18791906
That worked on folder but not subfolder - so it created the testing folder copied all files to it - but didn't copy any subfolders.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18791910
So, this should copy all...

Dim objShell:Set objShell=CreateObject("Wscript.Shell")
objShell.Run "robocopy C:\Email \\server\share\backup\testing /mir"
Set objShell=Nothing

I think the problem has been in the log file...let me look at that bit again..
0
 

Author Comment

by:William_Conway
ID: 18791980
That worked for folder and sub-folders
getting closer!!!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18791996
Let's go back to the last full version and just exclude the log file for now...I think that may be the problem...

'Backup.vbs
strResp=Msgbox("Outlook should be closed.  Are you ready to continue?", vbYesNo, "Backup")
If strResp=No Then wscript.quit

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objShell:Set objShell=CreateObject("Wscript.Shell")

With objShell
  UserName=.ExpandEnvironmentStrings("%Username%")
  MyDocuments=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\My Documents\" & chr(34)
  Desktop=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Desktop\" & chr(34)
  Favorites=chr(34) & .ExpandEnvironmentStrings("%UserProfile%") & "\Favorites\" & chr(34)
End With
Email="C:\Email\"
Backup="C:\backup\"

arrFolders=Array(MyDocuments, Favorites, Desktop, Email, Backup)
strTarget=chr(34) & "\\server\share\BackUp\" & username & chr(34)

wscript.echo strtarget

'strLog=" " & chr(34) & "\\server\share\BackUp\" & username & ".log" & chr(34)

If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
set objFolder=objFSO.CreateFolder (strTarget)
End If


For Each Item in arrFolders
  objShell.Run "robocopy " & Item & strTarget & " /tee /mir /r:3 /zb"
Next

Set objFSO=Nothing
Set objShell=Nothing
'End code
Admin Edit
0
 

Author Comment

by:William_Conway
ID: 18792072
back to bad filename or number on the below line
set objFolder=objFSO.CreateFolder (strTarget)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18792301
So, our culprit is here somewhere... : (

Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
strTarget=chr(34) & "\\server\share\BackUp\" & username & chr(34)
If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
  wscript.echo "Creating " & strTarget
set objFolder=objFSO.CreateFolder (strTarget)
End If

Can you post a screen shot of this?
www.ee-stuff.com is a sister-site upload location..
0
 

Author Comment

by:William_Conway
ID: 18792561
New Code
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
strTarget=chr(34) & "\\amstw23file1\william_conway\BackUp\Username"
If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
  wscript.echo "Creating " & strTarget
set objFolder=objFSO.CreateFolder (strTarget)
End If

0
 

Author Comment

by:William_Conway
ID: 18792610
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18792691
Looks like there's a missing quote...
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
strTarget=chr(34) & "\\amstw23file1\william_conway\BackUp\Username"  & chr(34) '<<<added this last chr(34)
If not objFSO.FolderExists(strTarget) Then
  wscript.echo strTarget & " doesn't exist...creating it..."
  wscript.echo "Creating " & strTarget
set objFolder=objFSO.CreateFolder (strTarget)
End If
0
 

Author Comment

by:William_Conway
ID: 18792762
Same except line 6 char 1 now instead.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18792780
Change strTarget to:

strTarget=chr(34) & "\\amstw23file1\william_conway\BackUp\Username\" & chr(34)
0
 

Author Comment

by:William_Conway
ID: 18793053
Same error Im afraid
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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