[p4] trim journal file versions

Steve M. Robbins steve at sumost.ca
Wed Nov 7 13:07:09 PST 2007


Hi,

While cleaning out my mailbox, I came across an old discussion on
perforce backup script for windows:
http://maillist.perforce.com/pipermail/perforce-user/2007-August/022175.html


On Tue, Aug 14, 2007 at 10:46:17AM -0400, Keith Graham - ID wrote:

> After we create the new checkpoint, we then use "forfiles" to delete the
> checkpoints and journals that are over a certain age.  (2 weeks in our
> case.)  We have plenty of disk, so keeping 10 copies of the checkpoint
> and journal isn't a problem, and acts as an extra backup in case one or
> more of the files ends up corrupted.

I come from a unix background, where I'm used to rotating log files
and keeping a fixed number of recent logs.  So I solved this problem
by writing the following python script.  Do with it as you like.

My backup script (batch file) ends with the line:

    python D:\Tools\common\trim-files.py 5 checkpoint journal


Cheers,
-Steve

#! /usr/bin/python
# -*- coding: iso-8859-1 -*-

# Trim a list of files of the form:
#   prefix.1, prefix.2, ....
# keeping only the N files with the highest-numbered suffix.


import os,re,sys
import win32con, win32api


def usage( arg0 ):
    print "usage: %s N prefix [prefix ...]" % arg0
    print """    Find all files in current directory of form prefix.ddd
    where ddd is any integer.  Remove all but the N files with the
    highest-numbered suffixes"""



class FileMatcher:
    def __init__( self, prefix ):
        self.filematch_re = re.compile( re.escape( prefix + "." ) + "(\d+)$" )
    def matcher( self, x ):
        return self.filematch_re.match( x ) != None
    def suffix( self, x ):
        return int( self.filematch_re.match( x ).group( 1 ) )
    def sorter( self, x, y ):
        return cmp( self.suffix( x ), self.suffix( y ) )

# Force removal of file, even if READONLY bit set
def forceRemove( file ):
    win32api.SetFileAttributes( file, win32con.FILE_ATTRIBUTE_NORMAL )
    os.remove( file )

def main():
    if ( len( sys.argv ) < 3 ):
        usage( sys.argv[0] )
        return 1

    numKeep = int( sys.argv[1] )

    for prefix in sys.argv[2:]:
        fm = FileMatcher( prefix )
        matchfiles = filter( fm.matcher, os.listdir( '.' ) )
        matchfiles.sort( fm.sorter )
        for file in matchfiles[:-numKeep]:
            print "Removing", file
            forceRemove( file )

main()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://maillist.perforce.com/pipermail/perforce-user/attachments/20071107/d359c257/attachment.bin


More information about the perforce-user mailing list