公司有台SVN服务器,平时不经常备份。领导最近要求说要定期备份,于是整理了一个脚本,这个脚本是一个网友写的吧,感觉不错,然后就翻译了一次。也收藏下,如果看着排版就把附件下载下来用notepad++看吧,在此也谢谢这个脚本的原创作者,感谢分享
@echo off ##关闭显示处理过程。 rem -----设置环境变量-------##rem在cmd中是注释的意思,和::差不多 rem -----设置SVN目录-------- set svnpath=C:\Repositories\##set设置变量 rem -----设置SVN备份目录------- set backuppath=E:\Backup\svn\ rem -----备份时间--- set backupdate=%date:~0,4%%date:~5,2%%date:~8,2%##备份日期,格式为'20170902' set backuptime=%date:~0,4%-%date:~5,2%-%date:~8,2% %time:~0,2%:%time:~3,2%##备份日期,格式为'2017-09-02' rem ----日志文件路径------ set LogFile=%backuppath%%backupdate%\svn.log ##设置备份的日志文件,具体路径套用上述设置的变量值 setlocal enabledelayedexpansion ##启用延迟变量。具体不明白可以http://www.jb51.net/article/29323.htm ----------------------------------------------------------- 以上内容不会被打印到日志 -------------------------------------------------------------- if not exist %backuppath%%backupdate% ( md %backuppath%%backupdate% echo %backuptime% 创建备份目录成功>> %LogFile% ) ###以上做了一个if判断,exist 是cmd判断文件是否存在的命令 使用方法有 exist(存在)、not exist(不存在) ###判断,如果 %backuppath%%backupdate% 不存在则执行()里面的语句进行创建备份目录。 ###cmd下的for语句学习:http://blog.csdn.net/xhhjin/article/details/7373524 ###如果在linux或者cmd中命令执行有问题,则可以通过 echo Command > debug.txt 的形式来打印执行信息,看是不是命令执行有问题,这也就是脚本排错的精髓 echo ------------------------------------------- >> %LogFile% echo %backuptime% 开始备份SVN操作>> %LogFile% echo ------------------------------------------- >> %LogFile% for /f %%I in ('dir %svnpath% /a:d /b') do ( if exist %svnpath%%%I\hooks ( md %backuppath%%backupdate%\%%I ## dir %svnpath% /a:d /b 列出%svnpath%下的信息,将此过程传递给定义的%I变量 ##判断如果存在这个路径,则执行()内的内容 ##根据判断存在,进行此步创建 echo %date:~0,4%-%date:~5,2%-%date:~8,2% !time:~0,2!:!time:~3,2!:!time:~6,2!:开始备份%%I >> %LogFile% ## 打印当前时间,在log中标注此过程为开始备份 svnadmin hotcopy %svnpath%%%I %backuppath%%backupdate%\%%I ##svnadmin hotcopy进行热备一份到备份文件夹下 echo %date:~0,4%-%date:~5,2%-%date:~8,2% !time:~0,2!:!time:~3,2!:!time:~6,2!:完成备份%%I >> %LogFile% ##打印当前时间,在log中标注此过程为开始备份 echo %date:~0,4%-%date:~5,2%-%date:~8,2% !time:~0,2!:!time:~3,2!:!time:~6,2!:开始压缩%%I >> %LogFile% ##打印当前时间,在log中标注此过程为开始压缩 "C:\Program Files\HaoZip\HaoZipC.exe" a -tzip %backuppath%%backupdate%\%%I.zip %backuppath%%backupdate%\%%I >> %LogFile% ##调用好压命令,根据已经备份的文件夹进行压缩,并打印压缩过程 echo %date:~0,4%-%date:~5,2%-%date:~8,2% !time:~0,2!:!time:~3,2!:!time:~6,2!:完成压缩%%I >> %LogFile% ##打印当前时间,在log中标注此过程为完成压缩 echo %date:~0,4%-%date:~5,2%-%date:~8,2% !time:~0,2!:!time:~3,2!:!time:~6,2!:开始删除%%I目录>> %LogFile% ##打印当前时间,将删除目录的时间记录到log日志中 rd %backuppath%%backupdate%\%%I /s/q ##所有文件全部压缩后,删除未被压缩文件,保留已经压缩备份的文件 echo %date:~0,4%-%date:~5,2%-%date:~8,2% !time:~0,2!:!time:~3,2!:!time:~6,2!:完成删除%%I目录>> %LogFile% ##将此阶段在日志中标注为删除目录已完成 ) ) echo %date:~0,4%-%date:~5,2%-%date:~8,2% !time:~0,2!:!time:~3,2!:!time:~6,2!:SVN备份完成>> %LogFile% echo %date:~0,4%-%date:~5,2%-%date:~8,2% !time:~0,2!:!time:~3,2!:!time:~6,2!:开始删除历史备份版本>> %LogFile% ----------------------------------------------------------- 以上备份全部完成,以下为删除历史备份文件 -------------------------------------------------------------- rem 以下为删除历史备份信息,并设置最大保留备份数量 set maxNum=31 for /f "skip=%maxNum%" %%I in ('dir %backuppath% /a:d /o:-d /b') do ( rd %backuppath%%%I /s/q echo %date:~0,4%-%date:~5,2%-%date:~8,2% !time:~0,2!:!time:~3,2!:!time:~6,2!:删除%%I成功>> %LogFile% ) echo %date:~0,4%-%date:~5,2%-%date:~8,2% !time:~0,2!:!time:~3,2!:!time:~6,2!:完成删除历史备份版本>> %LogFile% @echo on