[SCM]源码管理 - perforce的备份和优化脚本


一 perforce server的备份

perforce server拥有checkpoint机制用来备份server的database。checkpoint,versioned files,journal(只从上次checkpoint后对server的操作)是恢复perforce server的所有的东西。

我们每天晚上创建checkpoint,然后备份versioned files。以防万一我们需要保存几个checkpoints。下面是在Linux上备份perforce server的checkpoint和journal的脚本:

#!/bin/bash
BACKUP_DIR=/home/p4/backup
DEPOT_DIR=/home/p4/srvroot
P4=/opt/p4/p4
HOST=p4:1666
USER=root
PASSWORD=password
DAYS_TO_KEEP=3

# Verify the depot
$P4 -p $HOST -u $USER -P $PASSWORD verify -q //… > /dev/null
$P4 -p $HOST -u $USER -P $PASSWORD verify -u -q //… > /dev/null 2> /dev/null

# Take a checkpoint
$P4 -p $HOST -u $USER -P $PASSWORD admin checkpoint > /dev/null

# Move all journal and checkpoint files to the backup folder
mv $DEPOT_DIR/journal.* $BACKUP_DIR
mv $DEPOT_DIR/checkpoint.* $BACKUP_DIR

# Remove old checkpoint and journal files
find $BACKUP_DIR/journal.* -mtime +$DAYS_TO_KEEP -exec rm -f {} \; > /dev/null 2> /dev/null
find $BACKUP_DIR/checkpoint.* -mtime +$DAYS_TO_KEEP -exec rm -f {} \; > /dev/null 2> /dev/null

二 perforce server的优化

perforce server在运行一段时间后段变慢,可以通过重新导入checkpoint来优化。

为了完成优化perforce server。需要创建checkpoint,停止server,使用checkpoint恢复,然后再启动server。

下列的脚本用来优化perforce server:
# This script describes a way of optimizing and cleaning the metadata for a Perforce server

# The following steps are copied from the following URL:
# ? <http://blog.perforce.com/blog/?p=187>

# ? Stop your server
# ? Take a checkpoint
# ? Move your existing db files to a save directory
# ? Recover from the checkpoint
# ? Check for errors during restore
# ? Restart your server
# ? Delete the files from the save directory

# Instead of deleting the old database files immediately after the process, we
# delete them during the next run instead.

BACKUP_DIR=/home/p4/backup
DEPOT_DIR=/home/p4/srvroot
P4D=/opt/p4/p4d
LOG_FILE=/var/log/p4d-error

# ? Take a checkpoint
echo "Taking a checkpoint…"
/opt/scripts/p4d/makecheckpoint.sh

# ? Stop your server
echo "Stopping the Perforce server…"
/etc/init.d/p4d stop

# ? Delete the files from the save directory
echo "Backing up current database…"
rm -R $BACKUP_DIR/db

# ? Move your existing db files to a save directory
mkdir $BACKUP_DIR/db
mv $DEPOT_DIR/db.* $BACKUP_DIR/db/

# ? Recover from the checkpoint
CHECKPOINT_FILE=`ls -h –sort=time $BACKUP_DIR/checkpoint* | head -1`

echo "Do you wish to restore from checkpoint '$CHECKPOINT_FILE'?"
select yn in "Yes" "No"; do
  case $yn in
    Yes ) echo "Restoring checkpoint…"; su p4 -c "$P4D -r $DEPOT_DIR -L $LOG_FILE -jr $CHECKPOINT_FILE"; break;;
    No ) echo "Optimization aborted; restoring previous database…"; mv $BACKUP_DIR/db/db.* $DEPOT_DIR/; break;;
  esac
done

# ? Restart your server
/etc/init.d/p4d start

三 使用off-line perforce server作备份,来减少main server的停机时间

1)手动创建off-line perforce server
cd C:\P4ROOT
p4d -r C:\P4ROOT -jc
copy C:\P4ROOT\checkpoint.NNN D:\Offline_P4ROOT\checkpoint.NNN
p4d -r D:\Offline_P4ROOT -jr checkpoint.NNN

2)每天对main server进行journal,将老的journal应用到off-line perforce server,然后对off-line perforce server创建checkpoint,最后备份此checkpoint,从而达到对main server备份的作用。
p4d -r C:\P4ROOT -jj D:\Offline_P4ROOT\truncated\journal
p4d -r D:\Offline_P4ROOT -jr .\truncated\journal.jnl.XXX
p4d -r D:\Offline_P4ROOT -jd checkpoint.mmddyyyy

注意: 此时journal使用-jj参数(不能用-jr),表示不创建checkpoint,只是将当前的journal,保存为journal.xxx,然后创建新的journal文件来记录新的操作。 因为不创建checkpoint,所以mainserver此时也不需要lock了。

3)从2010.2 后的版本,我们可以创建replicated server(包含replicated database[metadata]和 versioned files)来作为off-line的server用作备份,从而减少对main server的lock时间。同时replicated server还可以用来作为只读server减少main server的负载,或作为main server的热备份当main server停机后直接启动replicated server。

参考:

http://kb.perforce.com/article/126/offline-checkpoints


完!

posted @ 2011-08-24 17:05  iTech  阅读(1457)  评论(0编辑  收藏