[p4] Backup procedure help required - script for Perforce in Windows (DOS/VBS)?

Eric L eric8005 at gmail.com
Fri Aug 10 12:29:32 PDT 2007


Thanks to all who replied. Putting a lot of your advice together, I
came up with a script that will hopefully help others in my
predicament.

Make sure to READ THE ENTIRE SCRIPT carefully before blindly using it
because it has some potentially dangerous commands in it, such as md
("make directory") and especially rd ("remove directory")!!

I have this file scheduled to run everyday using Windows Task Scheduler.


============== START OF p4backup.bat ===============
@REM !!!WARNING!!! ENSURE %backuppath% (paramater %3) IS A NEW PATH
WITH NO OTHER FILES IN IT because this batch uses "rd [BACKUP PATH] /s
/q"
@REM I am not responsible for any damage this batch file causes - USE
AT YOUR OWN RISK

@REM This batch file should be scheduled to run on a daily basis
before the daily server backup by using Windows Task Scheduler

@REM ALL 5 parameters must be specified, otherwise, exit with the
error text given at the :missingparameter label
@REM i.e. C:\^>p4backup.bat "C:\Program Files\Perforce" "C:\Program
Files\Perforce\depot" "\\MYSERVER\p4backup" p4verify.log p4backup.log
@REM DO NOT USE A TRAILING BACKSLASH ON PATH NAMES SPECIFIED AS PARAMETERS
@IF ""==""%1 goto :missingparameter
@IF ""==""%2 goto :missingparameter
@IF ""==""%3 goto :missingparameter
@IF ""==""%4 goto :missingparameter
@IF ""==""%5 goto :missingparameter

@REM %p4root% is where the db.* files are i.e. C:\Program Files\Perforce
set p4root=%1

@REM %p4depotpath% is where the depot is i.e. C:\Program Files\Perforce\depot
set p4depotpath=%2

@REM %backuppath% is the server or other location which gets backed up
daily i.e. \\MYSERVER\p4backup
@REM BE CAREFUL THAT %backuppath% is a new  path because an "rd /s /q"
is done on it in this file
set backuppath=%3

@REM %verificationlog% is the file output from "P4 verify -q //..."
i.e. p4verify.log
@REM note that verificationlog must be %4 due to the "if %~z4 EQU 0
goto :gtrzero" check below (the 4 in %~z4 refers to %4)
set verificationlog=%4

@REM %backuplog% is the main output generated by this batch file i.e.
p4backup.log
set backuplog=%5

@REM Perforce Root is located in %p4root%
@REM this changes directory at the beginning so log files are saved
and other commands are performed in the right directory
cd /d %p4root%

@REM verify database consistency with the repository before backup
@REM NOTE: Can take a lot of time. In big databases run it separately
@REM this creates the new %verificationlog%
@REM 	if the size of %verificationlog% is 0 then no errors occurred,
since other output is suppressed by -q)
P4 verify -q //...  > %verificationlog% 2>&1

@REM start a new backup log file (%backuplog%) with a date & time
stamp and title
echo %date%, %time%: Perforce Backup > %backuplog%
echo.>>%backuplog%

@REM if the verification log file size is greater than zero (now that
P4 verify was run earlier), skip the Error part
if %~z4 EQU 0 goto :gtrzero

@REM otherwise, if it is a size zero file, note the errors, and exit...
echo [THE COMMAND "P4 verify -q //..." PRODUCED ERRORS SO NO BACKUP
OCCURRED] >> %backuplog%
echo [AND THEREFORE, LOGS IN %p4root% WERE NOT COPIED TO %backuppath%]
>> %backuplog%
REM email notification of the script
wscript C:\scripts\sendmail-MAPI.vbs %p4root% %verificationlog% %backuplog%
goto :eof

:gtrzero

@REM Delete journal files, except for the active one named "journal"
@REM 2>&1 redirects the error stream to the output stream, so the
%backuplog% can record errors returned
echo %p4root%^>del journal.1* /f /q >> %backuplog%
del journal.1* /f /q >> %backuplog% 2>&1
echo. >> %backuplog%
echo %p4root%^>del journal.2* /f /q >> %backuplog%
del journal.2* /f /q >> %backuplog% 2>&1
echo. >> %backuplog%
echo %p4root%^>del journal.3* /f /q >> %backuplog%
del journal.3* /f /q >> %backuplog% 2>&1
echo. >> %backuplog%
echo %p4root%^>del journal.4* /f /q >> %backuplog%
del journal.4* /f /q >> %backuplog% 2>&1
echo. >> %backuplog%
echo %p4root%^>del journal.5* /f /q >> %backuplog%
del journal.5* /f /q >> %backuplog% 2>&1
echo. >> %backuplog%
echo %p4root%^>del journal.6* /f /q >> %backuplog%
del journal.6* /f /q >> %backuplog% 2>&1
echo. >> %backuplog%
echo %p4root%^>del journal.7* /f /q >> %backuplog%
del journal.7* /f /q >> %backuplog% 2>&1
echo. >> %backuplog%
echo %p4root%^>del journal.8* /f /q >> %backuplog%
del journal.8* /f /q >> %backuplog% 2>&1
echo. >> %backuplog%
echo %p4root%^>del journal.9* /f /q >> %backuplog%
del journal.9* /f /q >> %backuplog% 2>&1
echo. >> %backuplog%

@REM delete existing checkpoint file
echo %p4root%^>del checkpoint.* /f /q >> %backuplog%
del checkpoint.* /f /q >> %backuplog% 2>&1
echo. >> %backuplog%

@REM create a new checkpoint file, truncate the journal file
echo %p4root%^>p4d -r %p4root% -jc -z >> %backuplog%
p4d -r %p4root% -jc -z >> %backuplog% 2>&1
echo. >> %backuplog%

@REM remove the current backup directory (it gets backed up nightly)
echo %p4root%^>rd %backuppath% /s /q >> %backuplog%
rd %backuppath% /s /q >> %backuplog% 2>&1
echo. >> %backuplog%

@REM create a new backup directory
echo %p4root%^>md %backuppath% >> %backuplog%
md %backuppath% >> %backuplog% 2>&1
echo. >> %backuplog%

@REM create a new depot directory within the backup directory
echo %p4root%^>md %backuppath%\depot >> %backuplog%
md %backuppath%\depot >> %backuplog% 2>&1
echo. >> %backuplog%

@REM copy the depot files
echo %p4root%^>xcopy depot %backuppath%\depot /e /y >> %backuplog%
xcopy depot %backuppath%\depot /e /y >> %backuplog% 2>&1
echo. >> %backuplog%

@REM copy the new checkpoint & journal files
echo %p4root%^>copy journal* %backuppath% /y >> %backuplog%
copy journal* %backuppath% /y >> %backuplog% 2>&1
echo. >> %backuplog%
echo %p4root%^>checkpoint.* %backuppath% /y >> %backuplog%
copy checkpoint.* %backuppath% /y >> %backuplog% 2>&1
echo. >> %backuplog%


@REM copy the log files
echo %p4root%^>copy %verificationlog% %backuppath% /y >> %backuplog%
copy %verificationlog% %backuppath% /y >> %backuplog% 2>&1
echo. >> %backuplog%
echo [END OF BACKUP FILE] >> %backuplog%
@REM no backup file output for the last copy command, since it is copying itself
copy %backuplog% %backuppath% /y

goto :eof

:missingparameter
@echo.
@echo You must specify all 5 parameters - DO NOT USE A TRAILING
BACKSLASH FOR PATHS
@echo.
@echo USAGE: p4backup.bat [P4 ROOT PATH] [P4 DEPOT PATH] [BACKUP PATH]
@echo [VERIFICATION LOG FILE NAME] [BACKUP LOG FILE NAME]
@echo.
@echo !!!WARNING!!! ENSURE [BACKUP PATH] (parameter 3) IS A NEW PATH
WITH NO OTHER
@echo FILES IN IT because this batch uses "rd [BACKUP PATH] /s /q"
@echo.
@echo i.e. p4backup.bat "C:\Program Files\Perforce" "C:\Program
Files\Perforce\depot" "\\MYSERVER\p4backup" p4verify.log p4backup.log
============== END OF p4backup.bat ===============


The line "wscript C:\scripts\sendmail-MAPI.vbs %p4root%
%verificationlog% %backuplog%" above will need to be changed depending
on where you keep your scripts and how you want your email
notification done. Because I am limited to MAPI, I use a combination
of the script below (note: if you intend to use the script below, you
may need to modify at least the ****change this**** part) and a
program called Express ClickYes (installed in the default location).

If you have access to an SMTP server, email notification is a lot
simpler. See: http://www.petri.co.il/send_mail_from_script.htm for
several SMTP options.

To test the case where "P4 verify -q //..." produces an error (in
which case an exception email notification is sent), add some text
into a p4verify.log (or whatever you use for the %4 verificationlog
command line parameter) file in the p4root and make the file read
only.


============== START OF C:\scripts\sendmail-MAPI.vbs ===============
Dim ArgObj, p4root, verificationlog, backuplog
Set ArgObj = WScript.Arguments

' get the command line arguments passed in by the wscript line in p4backup.bat
p4root = ArgObj(0)
verificationlog = ArgObj(1)
backuplog = ArgObj(2)

Set objApp = CreateObject("WScript.Shell")
' .Run works as follows: object.Run(strCommand, [intWindowStyle],
[bWaitOnReturn])
objApp.Run "cmd /c ""C:\Program Files\Express ClickYes\ClickYes.exe""
-activate", 0, False   'this runs a program which says Yes to any
Outlook security prompts that appear
WScript.sleep 1500  'wait 1.5 seconds to make sure the program has started

    ' below based on http://www.rgagnon.com/wshdetails/wsh-0018.html

    Dim ToAddress
    Dim MessageSubject
    Dim MessageBody
    Dim MessageAttachment

    Dim ol, ns, newMail

    ToAddress = "LastName, FirstName"   ' ****change this****...
    MessageSubject = "Perforce Verification Error - Backup Aborted"
    MessageBody = "The p4backup.bat backup script encountered a
verification error. Please see the output from ""P4 verify -q //...""
in the attached verification log file (" & verificationlog & ") for
details. The aborted backup log (" & backuplog & ") is also attached."

    Set ol = WScript.CreateObject("Outlook.Application")
    Set ns = ol.getNamespace("MAPI")
    ns.logon "","",true,false
    Set newMail = ol.CreateItem(olMailItem)
    newMail.Subject = MessageSubject
    newMail.Body = MessageBody & vbCrLf
    newMail.Attachments.Add(p4root & "\" & verificationlog)
    newMail.Attachments.Add(p4root & "\" & backuplog)

    ' validate the recipient, just in case...
    Set myRecipient = ns.CreateRecipient(ToAddress)
    myRecipient.Resolve
    If Not myRecipient.Resolved Then
       MsgBox "unknown recipient"
    Else
       newMail.Recipients.Add(myRecipient)
       newMail.Send
    End If

    Set ol = Nothing

objApp.Run "cmd /c ""C:\Program Files\Express ClickYes\ClickYes.exe""
-suspend", 0, False   'this suspends the Outlook security prompt
overriding program (so the security is back to normal)

WScript.sleep 1500  'wait 1.5 seconds to make sure the program has suspended

objApp.Run "cmd /c ""C:\Program Files\Express ClickYes\ClickYes.exe""
-stop", 0, False   'this ends the Outlook security prompt overriding
program

'Clear object out of memory
set ArgObj = Nothing
set p4root = Nothing
set backuplog = Nothing
set verificationlog = Nothing
============== END OF sendmail-MAPI.vbs ===============



On 7/26/07, Qazwart wrote:
> It's been a while since I wrote DOS Batch scripts, but I believe that
> "if errorlevel" can access the exiting error codes. See <  http://
> www.robvanderwoude.com/errorlevel.html> for more information.
>
> On Jul 26, 2007, at 11:47 AM, Eric L wrote:
>
> > Hello, I am a new Perforce user.
> >
> > Does anyone have an automated backup script for Perforce running on
> > Windows, which they could share?
> >
> > Also, I do not understand what this step in the Perforce backup
> > procedure means:
> >
> > "3. Ensure that the checkpoint has been created successfully before
> > backing up any  files. (After a disk crash, the last thing you want to
> > discover is that the  checkpoints you've been backing up for the past
> > three weeks were incomplete!)
> >
> >  You can tell that the checkpoint command has completed successfully
> > by  examining the error code returned from p4d -jc, or by observing
> > the  truncation of the current journal file."
> >
> > A. How do I examine the error code, and what do I look for?
> > B. What am I supposed to observe about the truncation of the current
> > journal file?
> >
> > Thanks in advance,
> > Eric
> > _______________________________________________
> > perforce-user mailing list  -  perforce-user at perforce.com
> > http://maillist.perforce.com/mailman/listinfo/perforce-user
>



More information about the perforce-user mailing list