0: Just thought i would share this... i created it after
1: finding not finding anything with this functionality
2: (a while ago). It was made for my personal needs
3: initially, and was doctored up a few weeks later to
4: be somewhat customizable. Its about a year old.
5:
6: I have a cronjob that runs it every day:
7: 0 1 * * * /home/conteb/cvs/snapshot.pl phynd /var/www/snapshots /var/cvs >> /var/www/snapshots/snapshots.log 2>&1
8:
9: Sample can be found at http://www.phynd.net/snapshots/
10:
11: #!/usr/bin/perl
12: #
13: # Create nightly cvs snapshots of project in a
14: # repository, only saving the snapshot if the module
15: # has changed.
16: #
17: # TODO: Error checking is by no means complete.
18: #
19: use Date::Manip;
20:
21: use strict;
22: use warnings;
23:
24: sub printHelp; # print basic help screen
25:
26: my $tempdir = '/tmp/';
27: my $maxdayspast = 90; # will only check 90 days in the past before recording.
28:
29: # Can take 1 - 3 args - project name and dir to put resulting snapshot
30: my $project = shift(@ARGV) or printHelp;
31: my $dest = shift(@ARGV) or printHelp;
32: my $server = shift(@ARGV) or printHelp;
33:
34:
35: # test $tempdir, $dest for access and writabilty
36: die "Error: No access to /tmp or /tmp not a dir\n"
37: unless (-w $tempdir && -d $tempdir);
38: die "Error: No access to $dest, $dest not a dir, or $dest doen't exist\n"
39: unless (-d $dest && -w $dest);
40:
41:
42: # First checkout the needed cvs directory
43: chdir $tempdir;
44: if (defined $server) {
45: (`cvs -d $server co -P $project 2>&1`=~/cannot find module/) and
46: die "Error: Invalid project specified - could not find module\n";
47: }
48:
49: # get todays date and adjust acordingly...
50: my $date=$1 if &ParseDate('today')=~/(\d{8})/;
51:
52: # create tarball
53: `tar czf $dest/$project-$date.tar.gz $project`;
54:
55: # Now, find the last tarball
56: my $prev=1;
57: my $filename;
58: do {
59: &DateCalc($date, "-".$prev." days")=~/(\d{8})/;
60: $filename=$dest.'/'.$project."-".$1.".tar.gz";
61: $prev++;
62: } while ((!(-e $filename))&&($prev < $maxdayspast));
63:
64: # for some reason, compressed tarballs of identical trees checked out
65: # at different times create slightly different sized tarballs (on my
66: # machine, anyway, using reiserfs). I'm not about to step out of my
67: # league and try to explain it - but it means we have to untar the package
68: # and run a diff on the two trees to see if there is a difference.
69: if ($prev == $maxdayspast) {
70: print "Did not find any snapshots within $maxdayspast days, recording new..."; }
71: else {
72: `mv $project $project.new`;
73: `tar xzvf $filename `;
74: if (`diff -Naur $project $project.new`) {
75: print "Changes detected, recording snapshot...";
76: }
77: else {
78: `rm $dest/$project-$date.tar.gz`; # dont save if nothing changed
79: }
80: `rm -r $project.new`;
81: }
82:
83: # remove project directories
84: `rm -r $project`;
85: print "Done ($date)\n";
86: exit;
87:
88: sub printHelp {
89: print "Usage: snapshot.pl project destination_dir server\n";
90: print " project name of project to check out of cvs\n";
91: print " destination_dir where to put the snapshot\n";
92: print " server server to connect to. used as cvs -d server...\n";
93: exit;
94: }
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|