Perforce 2005.1 System Administrator's Guide
<< Previous Chapter
Welcome to Perforce:
Installing and Upgrading

Table of Contents
Index
Perforce on the Web
Next Chapter >>
Administering Perforce:
Superuser Tasks


Chapter 2
Supporting Perforce:
Backup and Recovery

The Perforce server stores two kinds of data: versioned files and metadata. Both are stored in the server's root directory.

Backup and Recovery Concepts

Disk space shortages, hardware failures, and system crashes can corrupt any of the Perforce server's files. That's why the entire Perforce root directory structure (your versioned files and your database) should be backed up regularly.

As mentioned earlier, versioned files are stored in subdirectories beneath your Perforce server root, and can be restored directly from backups without any loss of integrity.

The files making up the Perforce database, on the other hand, may not have been in a state of transactional integrity at the moment they were copied to the system backups. Restoring the db.* files from system backups may result in an inconsistent database. The only way to guarantee the integrity of the database after it's been damaged is to reconstruct the db.* files from Perforce checkpoint and journal files.

The checkpoint file is often much smaller than the original database, and can be made smaller still by compressing it. The journal file, on the other hand, can grow quite large; it is truncated whenever a checkpoint is made, and the older journal is renamed. The older journal files can then be backed up offline, freeing up more space locally.

Both the checkpoint and journal are text files, and have the same format. A checkpoint and, if available, its subsequent journal, can restore the Perforce database.

Warning!

Checkpoints and journals archive only the Perforce database files, not the files in the depot directories! You must always back up the depot files (your versioned files) with the standard OS backup commands after checkpointing.

Because the information stored in the Perforce database is as irreplaceable as your versioned files, checkpointing and journaling are an integral part of administering a Perforce server, and should be performed regularly.

Checkpoint files

A checkpoint is a file that contains all information necessary to recreate the metadata in the Perforce database. When you create a checkpoint, the Perforce database is locked, enabling you to take an internally consistent snapshot of that database.

Versioned files are backed up separately from checkpoints. This means that a checkpoint does not contain the contents of versioned files, and as such, you cannot restore any versioned files from a checkpoint. You can, however, restore all changelists, labels, jobs, and so on, from a checkpoint.

To guarantee database integrity upon restoration, the checkpoint must be as old as, or older than, the versioned files in the depot. This means that the database should be checkpointed, and the checkpoint generation must be complete, before the backup of the versioned files starts.

Regular checkpointing is important to keep the journal from getting too long. Making a checkpoint immediately before backing up your system is good practice.

Creating a checkpoint

Checkpoints are not created automatically; someone or something must run the checkpoint command on the Perforce server machine. To create a checkpoint, invoke the p4d program with the -jc (journal-create) flag:

You can create a checkpoint while the Perforce server (p4d) is running. The checkpoint is created in your server root directory (P4ROOT).

To make the checkpoint, p4d locks the database and then dumps its contents to a file named checkpoint.n in the P4ROOT directory, where n is a sequence number. Before unlocking the database, p4d also copies the journal file to a file named journal.n-1 in the P4ROOT directory (regardless of the directory in which the current journal is stored), and then truncates the current journal. This guarantees that the last checkpoint (checkpoint.n) combined with the current journal (journal) always reflects the full contents of the database at the time the checkpoint was created.

The sequence numbers reflect the roll-forward nature of the journal; to restore databases to older checkpoints, match the sequence numbers. That is, the database reflected by checkpoint.6 can be restored by restoring the database stored in checkpoint.5 and rolling forward the changes recorded in journal.5. In most cases, you're only interested in restoring the current database, which is reflected by the highest-numbered checkpoint.n rolled forward with the changes in the current journal.

To specify a prefix or directory location for the checkpoint and journal, use the -jc option. For instance, if you create a checkpoint with:

your checkpoint and journal files will be named prefix.ckp.n, or prefix.jnl.n respectively, where prefix is as specified on the command line and n is a sequence number. If no prefix is specified, the default filenames checkpoint.n and journal.n are used. You can store checkpoints and journals in the directory of your choice by specifying the directory as part of the prefix. (Rotated journals are stored in the P4ROOT directory, regardless of the directory in which the current journal is stored.)

To create a checkpoint without being logged in to the machine running the Perforce server, use the command:

Running p4 admin checkpoint is equivalent to p4d -jc. You must be a Perforce superuser to use p4 admin.

You can set up an automated program to create your checkpoints on a regular schedule. Be sure to always check the program's output to ensure that checkpoint creation was started. After successful creation, a checkpoint file may be compressed, archived, or moved onto another disk. At that time or shortly thereafter, back up the versioned files stored in the depot subdirectories.

To restore from a backup, the checkpoint must be at least as old as the files in the depots. (that is, the versioned files can be newer than the checkpoint, but not the other way around.) As you might expect, the shorter this time gap, the better.

If the checkpoint command itself fails, contact Perforce technical support immediately. Checkpoint failure is usually a symptom of a resource problem (disk space, permissions, etc.) that can put your database at risk if not handled correctly.

Journal files

The journal is the running transaction log that keeps track of all database modifications since the last checkpoint. It's the bridge between two checkpoints.

If you have Monday's checkpoint and the journal that was collected from then until Wednesday, those two files (Monday's checkpoint plus the accumulated journal) contain the same information as a checkpoint made Wednesday. If a disk crash were to cause corruption in your Perforce database on Wednesday at noon, for instance, you could still restore the database even though Wednesday's checkpoint hadn't yet been made.

Warning!

By default, the current journal file name is journal and it resides in the P4ROOT directory. However, if a disk failure corrupts that root directory, your journal file will be inaccessible too.

We strongly recommend that you set up your system so that the journal is written to a filesystem other than the P4ROOT filesystem. You can specify this from the command line, or set P4JOURNAL before starting the Perforce server to tell it where to write the journal.

To restore your database, you only need to keep the most recent journal file accessible, but it doesn't hurt to archive old journals with old checkpoints, should you ever need to restore to an older checkpoint.

Enabling journaling on Windows

For Windows installations, if you used the installer (perforce.exe) to install a Perforce server or service, journaling is turned on for you.

If you installed Perforce without the installer (for an example of when you might do this, see "Multiple Perforce services under Windows" on page 129), you do not have to create an empty file named journal in order to enable journaling under a manual installation on Windows.

Enabling journaling on UNIX

For UNIX installations, journaling is also automatically enabled.

If P4JOURNAL is left unset (and no location is specified on the command line), the default location for the journal is $P4ROOT/journal.

After enabling journaling

Be sure to create a new checkpoint with p4d -jc (and -J journalfile if required) immediately after enabling journaling. Once journaling is enabled, you'll need make regular checkpoints to control the size of the journal file. An extremely large current journal is a sign that a checkpoint is needed.

Every checkpoint after your first checkpoint starts a new journal file and renames the old one. The old journal is renamed to journal.n, where n is a sequence number, and a new journal file is created.

By default, the journal is written to the file journal in the server root directory (P4ROOT). Because there is no sure protection against disk crashes, the journal file and the Perforce server root should be located on different filesystems, ideally on different physical drives. The name and location of the journal can be changed by specifying the name of the journal file in the environment variable P4JOURNAL, or by providing the -J filename flag to p4d.

Warning!

If you create a journal file with the -J filename flag, make sure that subsequent checkpoints use the same file, or the journal will not be properly renamed.

Whether you use P4JOURNAL or the -J journalfile option to p4d, the journal file name can be provided either as an absolute path, or as a path relative to the server root.

Disabling journaling

To disable journaling, stop the server, remove the existing journal file (if it exists), set the environment variable P4JOURNAL to off, and restart p4d without the -J flag.

Versioned files

Your checkpoint and journal files are used to reconstruct the Perforce database files only. Your versioned files are stored in directories under the Perforce server root, and must be backed up separately.

Versioned file formats

Versioned files are stored in subdirectories beneath your server root. Text files are stored in RCS format, with filenames of the form filename,v. There is generally one RCS-format (,v) file per text file. Binary files are stored in full in their own directories named filename,d. Depending on the Perforce file type selected by the user storing the file, there may be one or more archived binary files in each filename,d directory. If more than one file resides in a filename,d directory, each one refers to a different revision of the binary file, and is named 1.n, where n is the revision number.

Perforce also supports the AppleSingle file format for Macintosh. These files are stored on the server in full and compressed, just like other binary files. They are stored in the Mac's AppleSingle file format; if need be, the files can be copied directly from the server root, uncompressed, and used as-is on a Macintosh.

Because Perforce uses compression in the depot files, do not assume compressibility of the data when sizing backup media. Both text and binary files are either compressed by the Perforce server (denoted by the .gz suffix) before storage, or are stored uncompressed. At most installations, if any binary files in the depot subdirectories are being stored uncompressed, they were probably incompressible to begin with. (For example, many image, music, and video file formats are incompressible.)

Back up after checkpointing

In order to ensure that the versioned files reflect all the information in the database after a post-crash restoration, the db.* files must be restored from a checkpoint that is at least as old as (or older than) your versioned files. For this reason, create the checkpoint before backing up the versioned files in the depot directory or directories.

While your versioned files can be newer than the data stored in your checkpoint, it is in your best interest to keep this difference to a minimum; in general, you'll want your backup script to back up your versioned files immediately after successfully completing a checkpoint.

Backup Procedures

To back up your Perforce server, perform the following steps as part of your nightly backup procedure:

  1. Verify the integrity of your server and add MD5 digests and file length metadata to any new files:

    You may wish to pass the -q (quiet) option to p4 verify. If called with the -q option, p4 verify produces output only when errors are detected.

    The p4 verify command recomputes the MD5 signatures of all of your archived files and compares them with those stored when the files were first stored, and that all files known to Perforce exist in the depot subdirectories.

    By running p4 verify before the backup, you ensure that you create and store checksums and file length metadata for any files new to the depot since your last backup, and that this information is stored as part of the backup you're about to make.

    Regular use of p4 verify is good practice not only because it enables you to spot any server corruption before a backup is made, but because it also gives you the ability, following a crash, to determine whether or not the files restored from your backups are in good condition.

    Note

    If your site is very large, p4 verify may take some time to run, and you may wish to perform this step on a weekly basis rather than on a daily basis. For more about the p4 verify command, see "File verification by signature" on page 47.

  2. Make a checkpoint by invoking p4d with the -jc (journal-create) flag, or by using the p4 admin command. Use one of:

    or:

    Because p4d locks the entire database when making the checkpoint, you do not generally have to stop your Perforce server during any part of the backup procedure.

    Note

    If your site is very large (say, several gigabytes off db.* files), creating a checkpoint may take a considerable length of time.

    Under such circumstances, you may wish to defer checkpoint creation and journal truncation until times of low system activity. You might, for instance, archive only the journal file in your nightly backup, and only create checkpoints and roll the journal file on a weekly basis.

  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.

  4. Once the checkpoint has been created successfully, back up the checkpoint file, the old journal file, and your versioned files.

    (If you don't require an audit trail, you don't actually need to back up the journal. It is, however, usually good practice to do so.)

    Note

    There are rare instances (for instance, users obliterating files during backup, or submitting files on Windows during the file backup portion of the process) in which your depot files may change during the interval between the time the checkpoint was taken and the time at which the depot files get backed up by the backup utility.

    Most sites are affected by these issues; having the Perforce server available on a 24/7 basis is generally a benefit worth this minor risk, especially if backups are being performed at times of low system activity.

    If, however, the reliability of every backup is of paramount importance, consider stopping the Perforce server before checkpointing, and restarting it after the backup process has completed. Doing so will eliminate all risk of the system state changing during the backup process.

    You never need to back up the db.* files. Your latest checkpoint and journal contain all the information necessary to re-create them. More significantly, a database restored from db.* files is not guaranteed to be in a state of transactional integrity. A database restored from a checkpoint is.

    Windows

    On Windows, if you make your system backup while the Perforce server is running, you must ensure that your backup program doesn't attempt to back up the db.* files.

    If you try to back up the db.* files with a running server, Windows locks them while the backup program backs them up. During this brief period, the Perforce server is unable to access the files; if a user attempts to perform an operation that would update the file, the server may fail.

    If your backup software doesn't allow you to exclude the db.* files from the backup process, stop the server with p4 admin stop before backing up, and restart the server after the backup process is complete.

Recovery Procedures

If the database files become corrupted or lost, either because of disk errors, a hardware failure such as a disk crash, the database can be recreated with your stored checkpoint and journal.

There are many ways in which systems can fail; while this guide cannot address all of them, it can at least provide a general guideline for recovery from the two most common situations, specifically:

The recovery procedures for each failure are slightly different, and are discussed separately in the following two sections.

If you suspect corruption in either your database or versioned files, contact Perforce technical support.

Database corruption, versioned files unaffected

If only your database has been corrupted, (that is, your db.* files were on a drive that crashed, but you were using symbolic links to store your versioned files on a separate physical drive), you need only re-create your database.

You will need:

You will not need:

To recover the database

  1. Stop the current instance of p4d:

    (You must be a Perforce superuser to use p4 admin.)

  2. Rename (or move) the database ("db.*") files:

    There can be no db.* files in the $P4ROOT directory when you start recovery from a checkpoint. Although the old db.* files are never used during recovery, it's good practice not to delete them until you're certain your restoration was successful.

  3. Invoke p4d with the -jr (journal-restore) flag, specifying your most recent checkpoint and current journal. If you explicitly specify the server root ($P4ROOT), the -r $P4ROOT argument must precede the -jr flag:

    This recovers the database as it existed when the last checkpoint was taken, and then apply the changes recorded in the journal file since the checkpoint was taken.

    Note

    If you're using the -z (compress) option to compress your checkpoints upon creation, you'll have to restore the uncompressed journal file separately from the compressed checkpoint.

    That is, instead of using:

    p4d -r $P4ROOT -jr checkpoint_file journal_file

    you'll use two commands:

    p4d -r $P4ROOT -z -jr checkpoint_file.gz
    p4d -r $P4ROOT -jr journal_file

    You must explicitly specify the .gz extension yourself when using the -z flag, and ensure that the -r $P4ROOT argument precedes the -jr flag.

Check your system

Your restoration is complete. See "Ensuring system integrity after any restoration" on page 37 to make sure your restoration was successful.

Your system state

The database recovered from your most recent checkpoint, after you've applied the accumulated changes stored in the current journal file, is up to date as of the time of failure.

After recovery, both your database and versioned files should reflect all changes made up to the time of the crash, and no data should have been lost.

Both database and versioned files lost or damaged

If both your database and your versioned files were corrupted, you need to restore both the database and your versioned files, and you'll need to ensure that the versioned files are no older than the restored database.

You will need:

You will not need:

To recover the database

  1. Stop the current instance of p4d:

    (You must be a Perforce superuser to use p4 admin.)

  2. Rename (or move) the corrupt database ("db.") files:

    The corrupt db.* files aren't actually used in the restoration process, but it's safe practice not to delete them until you're certain your restoration was successful.

  3. Invoke p4d with the -jr (journal-restore) flag, specifying only your most recent checkpoint:

    This recovers the database as it existed when the last checkpoint was taken, but does not apply any of the changes in the journal file. (The -r $P4ROOT argument must precede the -jr flag.)

    The database recovery without the roll-forward of changes in the journal file brings the database up to date as of the time of your last backup. In this scenario, you do not want to apply the changes in the journal file, because the versioned files you restored reflect only the depot as it existed as of the last checkpoint.

To recover your versioned files

  1. After recovering the database, you then need to restore the versioned files according to your system's restoration procedures (for instance, the UNIX restore(1) command) to ensure that they are as new as the database.

Check your system

Your restoration is complete. See "Ensuring system integrity after any restoration" on page 37 to make sure your restoration was successful.

Note that files submitted to the depot between the time of the last system backup and the disk crash will not be present in the restored depot.

Note

Although "new" files (submitted to the depot but not yet backed up) will not appear in the depot after restoration, it's possible (indeed, highly probable!) that at one or more of your users have up-to-date copies of such files present in their client workspaces.

Your users can find such files by using Perforce to examine how files in their client workspaces differ from those in the depot. If they run:

p4 diff -se

...they'll be provided with a list of files in their workspace which differ from the files Perforce believes them to have. After verifying that these files are indeed the files you wish to restore, you may wish to have one of your users open these files for edit and submit them to the depot in a changelist.

Your system state

After recovery, your depot directories may not contain the newest versioned files. That is, files submitted after the last system backup but before the disk crash may have been lost.

In either case, contact Perforce technical support for further assistance.

Ensuring system integrity after any restoration

After any restoration, it's wise to run p4 verify to ensure the versioned files are at least as new as the database:

This command verifies the integrity of the versioned files. The -q (quiet) option tells the command to only produce output on error conditions. Ideally, this command should produce no output.

If any versioned files are reported as MISSING by the p4 verify command, you'll know that there is information in the database concerning files that didn't get restored. The usual cause is that you restored from a checkpoint and journal made after the backup of your versioned files. (that is, that your backup of the versioned files was older than the database.)

If (as recommended) you've been using p4 verify as part of your backup routine, you can run p4 verify on the server after restoration to reassure yourself that your restoration was successful.

If you have any difficulties restoring your system after a crash, contact Perforce Technical Support for assistance.


Perforce 2005.1 System Administrator's Guide
<< Previous Chapter
Welcome to Perforce:
Installing and Upgrading

Table of Contents
Index
Perforce on the Web
Next Chapter >>
Administering Perforce:
Superuser Tasks

Please send comments and questions about this manual to [email protected].
Copyright 1999-2005 Perforce Software. All rights reserved.
Last updated: 05/12/05