最近裝起了 svn 來玩。
在 svn book 中文翻譯
裡有一個 “漸進式檔案庫傾印 ” 的 Perl Script (Example: 5.3)。
不過呢,在最新版本的SVN book 裡卻找不到這段script。
這個script 非常好用。只不過還是有幾個小小的缺點。
第一個呢,漸進式傾印(Incremental Dump) 出來竟然是寫到同一個檔案,也就是說我如果之前執行這個Script之後 Dump 出來的 file沒先移到其他的地方(並更名),之後再執行這個Script就會把之後的結果給覆蓋掉。這樣一來,就失去把一次次新的revision dump出來的好處了。
第二,如果已經dump出最新的revision,再一次執行這個script時,會跳出Error不執行Dump沒錯,但是呢,過程的暫存檔($dumpfile.tmp) 卻會留下來,沒有處理掉。
所以,我稍稍修改了這個 script,嘗試解決這兩個問題。
修改後的script 如下:
#!/usr/bin/perl -w
use strict;
use POSIX qw(strftime);my $repos_path = ‘/somewhere/repos’;
my $dumpfile = ‘/somewhere/backup/svn-dumpfile’;
my $last_dumped = ‘/somewhere/backup/svn-last-dumped’;my $time_str = strftime “%Y-%m-%d_”, localtime;
$time_str .= time;
$dumpfile = $dumpfile.’_’.$time_str;# Figure out the starting revision. Use 0 if we cannot read the
# last-dumped file, else use the revision in that file incremented
# by 1.
my $new_start = 0;
if (open LASTDUMPED, $last_dumped)
{
my $line = <lastdumped>;
if (defined $line and $line =~ /^(\d+)/)
{
$new_start = $1 + 1;
}
close LASTDUMPED;
}# Query the youngest revision in the repos.
my $youngest = `svnlook youngest $repos_path`;
defined $youngest && $youngest =~ /^\d+$/
or die “$0: ‘svnlook youngest $repos_path’ cannot get youngest revision.\n”;
chomp $youngest;# print “debug: $new_start ,$youngest”;
if ($new_start > $youngest) {
die “$0: The youngest revision is identical to last dumped version (rev: $youngest) .\n”;
}# Do the backup.
system(“svnadmin dump $repos_path –revision $new_start:$youngest –incremental >> $dumpfile.tmp”) == 0
or die “$0: svnadmin dump to ‘$dumpfile.tmp’ failed.\n”;# Store a new last-dumped revision.
open LASTDUMPED, “> $last_dumped.tmp”
or die “$0: cannot open ‘$last_dumped.tmp’ for writing: $!\n”;
print LASTDUMPED “$youngest\n”;
close LASTDUMPED
or die “$0: error in closing ‘$last_dumped.tmp’ for writing: $!\n”;# Rename to final locations.
rename(“$dumpfile.tmp”, $dumpfile)
or die “$0: cannot rename ‘$dumpfile.tmp’ to ‘$dumpfile’: $!\n”;
rename(“$last_dumped.tmp”, $last_dumped)
or die “$0: cannot rename ‘$last_dumped.tmp’ to ‘$last_dumped’: $!\n”;# All done!