[p4] Can anyone point me to backup/init.d scripts for Debian?

Paul Cody Johnston paul.cody at lucidainc.com
Wed Dec 5 11:19:52 PST 2001


I maintain this one for our own site (solaris).  Should work on Linux.  Also 
helps to see the directory structure:

# Directories in /opt
/opt/perforce/
/opt/perforce/bin/
/opt/perforce/logs/
/opt/perforce/depot/

# Files in /var
/var/perforce/journal
/var/perforce/backup.log
/var/perforce/backup.manifest
/var/perforce/backup-0.tar.gz
/var/perforce/backup-1.tar.gz
/var/perforce/backup-2.tar.gz
/var/perforce/backup-3.tar.gz

#!/bin/sh

####################################################################
# p4init - start, stop, backup the perforce server
#
# Author: Paul Johnston (paul.cody at lucidainc.com)
# Date: Aug 20, 2001
# Credits:  Based on p4init and p4backup in the sunutil.tar from 
#           the perforce ftpserver (weblogic guys)
# 
####################################################################
#
# Variables
#
P4D=/usr/local/bin/p4d
MAILX=/bin/mailx
PS=/usr/bin/ps
GREP=/usr/bin/grep
SED=/usr/bin/sed
GZIP=/bin/gzip
GNUTAR=/usr/local/bin/tar

P4LOGIN=perforce
P4PORTNUM=1666
P4ADMINEMAIL=paul.cody at lucidainc.com

# Unless you are crazy, these two should reside on different physical disks
P4HOME=/opt/perforce
P4SAFE=/var/perforce

P4ROOT=$P4HOME/depot
P4LOGS=$P4HOME/logs
P4RUNLOG=$P4LOGS/run.log
P4CHECKPOINTLOG=$P4LOGS/checkpoint.log
P4JOURNAL=$P4SAFE/journal

BACKUPPREFIX="backup-"
BACKUPSUFFIX=".tar.gz"
BACKUPFILE=$P4SAFE/${BACKUPPREFIX}0${BACKUPSUFFIX}
BACKUPLOG=$P4SAFE/backup.log
BACKUPTOC=$P4SAFE/backup.manifest
NUMKEEP=3

# a global variable that is set by the getpid func
PID=

#
#
#
Trace()
{
	echo "[$0][trace]: $1"
} 

#
# Get the PID of the named process and sets it to the PID var
#
GetPid() 
{
        PID=
	PID=`$PS -e |
	     $GREP $1 |
	     $SED -e 's/^  *//' -e 's/ .*//'`
}

#
# Startup the perforce server
#
Start() 
{
        GetPid p4d
        if [ "$PID" = "" ]; then
		if [ -f $P4RUNLOG ]; then
			mv $P4RUNLOG $P4RUNLOG.last
		fi
		Trace "Starting Perforce server on port $P4PORTNUM at <$P4ROOT>" 
		su $P4LOGIN -c "$P4D -d -p $P4PORTNUM -r $P4ROOT -L $P4RUNLOG -J $P4JOURNAL 
1>$P4RUNLOG 2>&1"
        else
		Trace "Perforce server already appears to be running.  Please stop first."
        fi
}

#
# Stop the perforce server
#
Stop()
{
        GetPid p4d
	if [ "$PID" != "" ]; then
		Trace "Stopping perforce server..."
		kill $PID
        else
		Trace "Cannot stop p4d (not an active process)"
                exit 1
	fi
}

#
# Make a checkpoint
#
Checkpoint()
{
        Trace "Checkpointing Perforce Metadata..."
        su $P4LOGIN -c "$P4D -r $P4ROOT -jc 1>$P4CHECKPOINTLOG 2>&1"
        if [ $? != 0 ]; then
		cat $P4CHECKPOINTLOG | \
                  $MAILX -s "[`date`]: Perforce checkpoint failed on 
`hostname`, please attend!" $P4ADMINEMAIL
        fi
}

#
# Do a backup
#
Backup()
{
	Trace "Backing up Perforce Data..."

        # Run in a subshell
	(

	# Report starting time
	echo "Backup started at `date`"

	# Keep the previous $NUMKEEP tarballs around
	cd $P4SAFE
	num=$NUMKEEP
	while [ $num -gt 0 ]
	do
	  oldfile="${BACKUPPREFIX}${num}$BACKUPSUFFIX"
	  num=`expr $num - 1`
	  newfile="${BACKUPPREFIX}${num}$BACKUPSUFFIX"
	  [ -f $newfile ] && mv $newfile $oldfile
	done

	# Checkpoint the p4 metadata, remove old checkpoint files, 
	# & compress new checkpoint file
	$0 checkpoint
	cd $P4ROOT
	rm checkpoint.*.gz
	$GZIP checkpoint.[0-9]*

	# Make a compressed tarball of the entire depot
	$GNUTAR cvzf $BACKUPFILE ./j* ./[0-i]* ./[k-z]* > $BACKUPTOC

	# Report current file list (by time) and space usage in the tarball 
repository
	echo
	echo "State of compressed tar file repository:"
	cd $P4SAFE
	ls -lt
	df -k .
	echo
	echo "State of P4 Root (top 10 largest directories or files):"
	du -s $P4ROOT $P4ROOT/* $P4ROOT/*/* $P4ROOT/*/*/* $P4ROOT/*/*/*/* \
	    | sort -nr | head
	df -k $P4ROOT

	# Report ending time
	echo
	echo "Backup ended at `date`"

	) > $BACKUPLOG 2>&1

	# Send all output to p4 administrators
	cat $BACKUPLOG | \
	    $MAILX -s "P4 backup $BACKUPFILE created" $P4ADMINEMAIL
}

#
# Main logic
#
case "$1" in
'start')
        Start
	;;

'stop')
        Stop
	;;

'checkpoint')
        Checkpoint
	;;

'backup')
        Backup
	;;
*)
	echo "Usage: $0 { start | stop | checkpoint | backup }"
	;;
esac




More information about the perforce-user mailing list