#Backup Perforce database
#Author: Song Hu
#Created: May, 2009
"Backup Perforce Database."
def main():
"main function"
#the directory of the backup files
BackupDir = "G:/BackupPerforce/"
#the directory of the p4 server root
P4ROOTDir = "C:/Program Files/Perforce/"
# get the current date and time
from time import strftime
strTime = strftime("%Y-%m-%d_%H%M%S")
#update backup directory according to current date
BackupDir = BackupDir + strTime + '/'
import os
if not os.path.exists(BackupDir):
os.makedirs(BackupDir)
# logging
import logging
logging.basicConfig(level = logging.DEBUG,
format ='%(asctime)s (%(levelname)s) \t: %(message)s',
filename = BackupDir+'log',
filemode = 'w')
# step 1. connect to perforce
logging.info('Connecting to perforce server...')
import sys
#import the perforce modlule
from P4 import P4 , P4Exception
p4 = P4()
p4.port = "your port" #perforce server and port
p4.user = "your account name" #user name, should be admin
p4.client = "your work space" #perforce work space
try:
p4.connect()
except P4Exception:
for e in p4.errors:
logging.error(e)
logging.error('Failed to connect to perforce!')
_close(p4,logging)
return
logging.info('Connected.')
# step 2. verify the integrity of the perforce server
logging.info('Verify the integrity of the perforce server (//...) ... ')
rt = p4.run_verify('-q','//...')
if 0 != len(rt):
logging.error('Failed to verify the data on the perforce server!')
_close(p4,logging)
logging.info('Verification finished.')
# step 3. create perforce checkpoint
logging.info('Create perforce checkpoint...')
prefix = BackupDir + strTime
try:
p4.run("admin", "checkpoint",prefix)
except P4Exception:
for e in p4.errors:
logging.error(e)
logging.error('Failed to create perforce checkpoint!')
_close(p4,logging)
return
logging.info('Checkpoint created.')
# step 4. backup versioned files
logging.info('Backup versioned files...')
import shutil
try:
shutil.copytree(P4ROOTDir + 'depot/',
BackupDir + strTime + '_depot/')
except shutil.Error, er:
for e in er:
logging.error(e)
logging.error('Failed to copy versioned files!')
_close(p4,logging)
except os.error, er:
logging.error(str(er))
logging.error('Failed to copy versioned files!')
_close(p4,logging)
except:
logging.error('Unexpected error!')
logging.error('Failed to copy versioned files!')
_close(p4,logging)
logging.info('Versioned files copied to the directory: ' + BackupDir + strTime + '_depot/')
#clean up
_close(p4,logging)
def _close(p4,logging):
"this function disconnect from perforce server and close the logging"
#close the connection to perforce server
if p4.connected():
p4.disconnect()
logging.info('Disconnected from perforce.')
logging.shutdown()
if __name__ == "__main__":
main()
If everything goes right a backup directory,which is named using the current date and time, will be created and it shold contain:
- a copy of the directory of the Perforce versioned files,
- a Perforce checkpoint file (.ckp.n),
- a Perforce journal file (.jnl.n-1),
- a log.
No comments:
Post a Comment