Windows的Subversion备份脚本

时间:2023-03-08 17:30:18

2015-12-08更新:备份时添加--revision head 只备份最新的版本,已从脚本中移除。

2013-12-09更新:forfiles命令添加错误输出日志。

2013-12-04更新:添加自动删除过期备份,修改输出日志时间的bug。

不会脚本的去科普下吧:点我去科普

最近有个任务,要在windows系统上备份Svn。如果每次都自己去执行麻烦死了。折腾了1周,终于搞定了这个脚本!

说明:这个脚本是全部备份的脚本。如果想增量备份,自己改备份的参数:svnadmin dump --incremental

本脚本实现了自动添加计划任务备份,自动删除多少天前的备份,保留备份日志。

你需要做的是新建一个bat文件(取什么名字、放在哪儿随便你,脚本做了通配的\(^o^)/~),然后把下面的内容复制粘贴,然后改改基本变量就可以用了。

下面的是全部的脚本:

@echo off
:: 关闭回显 :: 说明:如有命令不明白,请使用帮助命令:命令/? 。如:for/? :: 设置标题
title SVN备份脚本 by zjl rem 设置变量 :: SVN_HOME svn程序的安装路径,指定到bin目录上一级。(如果路径有空格,请用""。)
set SVN_HOME="D:\Program Files\VisualSVN Server"
:: SVN库的路径
set SVN_ROOT=D:\Repositories
:: 备份SVN父目录的路径
set SVN_BACKUP_ROOT=D:\svn_bak
:: 获取当前系统日期和时间用来创建目录,格式:年月日_时分秒毫秒。如:20131129_16275274
set TIME_DIR=%date:~,%%date:~,%%date:~,%_%time:~,%%time:~,%%time:~,%%time:~,%
:: SVN备份子目录的路径
set BACKUP_DIRECTORY=%SVN_BACKUP_ROOT%\%TIME_DIR%
:: 日志文件路径(将日志和备份文件放在一个目录,删除时一起删除了)
set LOG=%BACKUP_DIRECTORY%\backup.log
:: 指定备份文件删除时间(单位:天)。 - 范围内的任何数字。
set NUM= rem 开始备份SVN :: 判断SVN程序是否存在
if not exist %SVN_HOME% goto error goto start :start
:: 查询日志大小(单位:字节),超过1Mb就新建一个 (日志放入备份目录后,此语句无效。仅备份!)
:: for /r %SVN_BACKUP_ROOT% %%I in (backup.log) do if %%~zI GEQ ren %LOG% backup_%TIME%.log
:: 新建目录
mkdir %BACKUP_DIRECTORY%
:: 建立日志
@echo [info]%date:~,% %time:~,%:%time:~,%:%time:~,%建立备份目录:%BACKUP_DIRECTORY% >>%LOG%
:: 备份SVN版本库并输出日志到文件
for /d %%i in (%SVN_ROOT%\*) do (
@echo 备份 %%~ni 库,备份文件名:%%~ni.dmp >>%LOG%
:: 完整备份(增量备份在dump后面添加参数 --incremental)
%SVN_HOME%\bin\svnadmin dump %SVN_ROOT%\%%~ni >%BACKUP_DIRECTORY%\%%~ni.dmp >>%LOG%
)
:: 输出备份命令退出码,用来判断是否备份成功。成功的退出码为0.
@echo [info]%date:~,% %time:~,%:%time:~,%:%time:~,%退出码为:%errorlevel%>>%LOG%
:: 自动设置计划任务:每周六 晚上11点执行本脚本。修改请参考:http://www.doc88.com/p-052298888776.html
at : /next:S %~f0>>%LOG%
:: 删除过期备份
goto delete :delete
:: 输出要删除的目录到日志文件
@echo [info]%date:~,% %time:~,%:%time:~,%:%time:~,%删除%NUM%天前的备份:>>%LOG%
forfiles /P %SVN_BACKUP_ROOT% /M *_* /D -%NUM% /C "cmd /c echo 删除文件夹:@file">>%LOG%
:: 使用rd命令删除超过%NUM%天的备份目录。如果不存在匹配条件的目录,将提示:错误: 用指定的搜索标准没有找到文件。如果存在匹配的文件,则提示:目录名无效
forfiles /P %SVN_BACKUP_ROOT% /M *_* /D -%NUM% /C "cmd /c rd /s/q @path" 2>>%LOG%
goto end :error
echo [error]%date:~,% %time:~,%:%time:~,%:%time:~,%程序目录不存在,请检查:%SVN_HOME%>>%LOG%
goto end :end
:: 输出一行空行分割日志
@echo. >>%LOG%
exit
:: 调试脚本用的命令
:: at : %~f0
:: PAUSE
:: exit

输出日志如下:

[info]-- ::51建立备份目录:D:\svn_bak\20131204_14405196
备份 accounts 库,备份文件名:accounts.dmp * Dumped revision .
备份 community 库,备份文件名:community.dmp
WARNING 0x0001: Mergeinfo referencing revision(s) prior to the oldest dumped revision (r2767). Loading this dump may result in invalid mergeinfo.
WARNING 0x0001: Mergeinfo referencing revision(s) prior to the oldest dumped revision (r2767). Loading this dump may result in invalid mergeinfo.
WARNING 0x0001: Mergeinfo referencing revision(s) prior to the oldest dumped revision (r2767). Loading this dump may result in invalid mergeinfo.
WARNING 0x0001: Mergeinfo referencing revision(s) prior to the oldest dumped revision (r2767). Loading this dump may result in invalid mergeinfo.
WARNING 0x0001: Mergeinfo referencing revision(s) prior to the oldest dumped revision (r2767). Loading this dump may result in invalid mergeinfo.
WARNING 0x0001: Mergeinfo referencing revision(s) prior to the oldest dumped revision (r2767). Loading this dump may result in invalid mergeinfo.
WARNING 0x0001: Mergeinfo referencing revision(s) prior to the oldest dumped revision (r2767). Loading this dump may result in invalid mergeinfo.
WARNING 0x0001: Mergeinfo referencing revision(s) prior to the oldest dumped revision (r2767). Loading this dump may result in invalid mergeinfo.
WARNING 0x0001: Mergeinfo referencing revision(s) prior to the oldest dumped revision (r2767). Loading this dump may result in invalid mergeinfo.
WARNING 0x0001: Mergeinfo referencing revision(s) prior to the oldest dumped revision (r2767). Loading this dump may result in invalid mergeinfo.
WARNING 0x0001: Mergeinfo referencing revision(s) prior to the oldest dumped revision (r2767). Loading this dump may result in invalid mergeinfo.
* Dumped revision .
WARNING 0x0001: The range of revisions dumped contained mergeinfo which reference revisions outside that range.
备份 conf 库,备份文件名:conf.dmp
* Dumped revision .
[info]-- ::09退出码为:
[info]-- ::09删除4天前的备份: 删除文件夹:"20131129_16441602"
删除文件夹:"20131130_23000008"