A simple Perforce backup script for the home

I use the free license of Perforce on my home network just to ensure version control and management of home coding projects, university thesis stuff, and general files that I just want to ensure I have previous versions of. It’s a really nice part of the Perforce licensing model that I can get two users and five clients for free. My network has my main PC (with Perforce depot), my personal laptop, my wife’s laptop, and my work laptop all connected. It’ll get worse when my four-year old starts wanting to source control things from his PC!

Having been responsible for some pretty heavyweight Perforce installations in my working life, I did not want to forget the golden rule of checkpointing and backing up. While it would not be a disaster if my Perforce data got lost or messed up, it would be an inconvenience. The scripts published on Perforce user forums, however, tend to be focussed on industrial installations and can be quite complex. For the home it is nice to have something more simple.

So the script here is really aimed at the home user. I don’t use Perforce everyday at home, and so a nightly checkpoint and backup is a waste of time. I tend to run it every two to four weeks. My environment is that I have my main home PC running Perforce, and hanging off this on a USB-2 link is a Western-Digital MyBook external drive that shows itself as D: . Sitting in the Perforce root folder (where the P4 db.* files are, and defined by the P4ROOT environment variable), I have a simple batch file called “backup.bat” (see code below).

The basic backup mechanism is this:

  1. Stop the Perforce server
  2. Take a checkpoint
  3. Copy checkpoint to a location on the MyBook external disk
  4. Copy checkpoints to a location on the main PC – just duplication for safety
  5. Delete checkpoints from P4ROOT
  6. Use Robocopy to incrementally backup all depot files for each depot
  7. Restart Perforce server

Here’s the script. Just copy it into a .bat file put it into the P4ROOT folder. To run it, either do as a scheduled task or – as I do – just run it manually at whatever frequency you are happy with.

REM  *** Backs up Perforce
p4 admin stop
p4d -jc -z
copy *.gz d:\Perforce\checkpoints
copy *.gz old_checkpoints
del /Q /F *.gz
rem *** now backup depot files. See following text for explanation
robocopy depot d:\perforce\depotfiles\depot /S /E /MIR /R:1 /W:5 >robolog-depot.txt
robocopy perforce d:\perforce\depotfiles\perforce /S /E /MIR /R:1 /W:5 >>robolog-p4.txt
robocopy work d:\perforce\depotfiles\work /S /E /MIR /R:1 /W:5 >>robolog-work.txt
rem *** Restart server
net start Perforce

The three robocopy lines reflect that I have three depots on my server – ‘depot’, ‘perforce’, and ‘work’. Change these to reflect your own depots.

Robocopy is a free utility that runs under Windows that provides extremely flexible folder copy functionality. See the Wikipedia entry for more details. Just make sure it is either on your search path, or place the EXE in the P4ROOT folder.

The options given to Robocopy just tell it to only copy files that are different to those that already exist. In otherwords, it synchronises one folder structure to another. I pipe the output into a log file so I can see exactly what new files have been copied if I need to.

And that’s it. Dead simple – will take you two minutes to set up, but could save you hours of hassle should something go awry in the future.

1 thought on “A simple Perforce backup script for the home

Leave a Reply