标签归档:SVN

做了一个SVN的备份脚本

不知道公司租了个什么破写字楼,隔三差五的跳闸、停电。想想内部PC服务器也没有个UPS扛一下,真是捏把汗,万一硬盘坏了,平时又没有做备份,项目代码可就找不到了。因此就想到写一个脚本,定期的备份SVN库。我的备份策略是:

  • 每天进行一次增量备份。
  • 每周进行一次全备份。
  • 备份的文件复制到另一台服务器上。

因为服务器采用的是ubuntu系统,所以实现起来也比较顺利。linux中的cron搞定定时功能,而svn支持增量备份和全备份,使用scp复制文件到另外一个ubuntu服务器。由于scp复制过程中需要输入密码,就使用了expect脚本。

weekly-full-backup.sh脚本如下:

#!/bin/sh
#
# Perform a weekly backup of a subversion repository,
# logging the most-recently-backed-up revision so an
# incremental script can be run other days.

svn_repos="your svn repos";
backups_dir="your backup dir";
next_backup_file="xxx-weekly-full-backup.$(date +%Y%m%d)"
last_backed_rev="last_backed_rev"

youngest=$(svnlook youngest $svn_repos);

echo "Backing up to revision $youngest"
svnadmin dump $svn_repos > $backups_dir/$next_backup_file

echo "Compressing dump file …"
gzip -9 $backups_dir/$next_backup_file
echo $youngest > $backups_dir/$last_backed_rev

echo "$next_backup_file        rev 0  ~ $youngest" >> ${backups_dir}/readme

$backups_dir/cp-another-server.sh $backups_dir ${next_backup_file}.gz

daily-backup.sh脚本如下:

#!/bin/bash
#
# Perform a daily backup of a subversion repository,
# using the previous most-recently-backed-up revision
# to create just an incremental dump.

svn_repos="your svn repos"
backups_dir="your backup dir"
next_backup_file="xxx-daily-incremental-backup.$(date +%Y%m%d)"
last_backed_rev="last_backed_rev"

read previous_youngest < $backups_dir/$last_backed_rev

youngest=$(svnlook youngest $svn_repos)
if [ $youngest -eq $previous_youngest ]
then
    echo "No new revision to backup"
    exit
fi

# We need to backup from the last backed up revision
# to the lastest(youngest) revision in the repository
let first_rev=previous_youngest+1
last_rev=$youngest

echo "Backing up revision $first_rev to $last_rev …"

svnadmin dump -incremental -revision $first_rev:$last_rev $svn_repos > $backups_dir/$next_backup_file

echo "Compressing dump file …"
gzip -9 $backups_dir/$next_backup_file

echo $last_rev >$backups_dir/$last_backed_rev

echo "$next_backup_file        rev $first_rev ~ $last_rev" >> $backups_dir/readme

$backups_dir/cp-another-server.sh $backups_dir ${next_backup_file}.gz

cp-another-server.sh脚本如下:

#!/usr/bin/expect
if {$argc<2} {
    send_user "usage: $argv0 backup_dir filenamen"
    exit
}

set backup_dir [lindex $argv 0]
set backup_filename [lindex $argv 1]
send_user "execute:$argv0 $backup_dir $backup_filenamen"

spawn scp $backup_dir/$backup_filename alexz@10.3.48.8:/home/alexz/svn-backup/$backup_filename
expect {
    "*password*" {send "alexz123n"}
}

expect eof
exit

请注意,脚本中涉及到项目名、路径名的,都进行了替换。