码迷,mamicode.com
首页 > 其他好文 > 详细

backup.bat 自动备份脚本

时间:2020-01-11 22:36:27      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:job   reg   数据   microsoft   邮件通知   backup   其它   ntbackup   现在   

@echo off
rem set local_ip=202.101.142.174
rem 取得本机ip地址,做为备份包的文件基名
for /f "skip=5 Tokens=15" %%i in (‘ipconfig‘) do (
set local_ip=%%i
goto out_ipconfig
)
:out_ipconfig

rem 取得当前工作目录
for /f "delims=" %%i in (‘cd‘) do set backupdir=%%~si
rem 判断备份方法,并设置相关参数

set success=Success.
set fail=Unsuccess!!!
set backup_result=%success%
set backup_status=
rem 取得ntbackup.exe文件的8.3格式绝对路径
for /f "delims=" %%i in ("%SystemRoot%") do set ntbackup=%%~si\system32\ntbackup.exe
rem 取得ntbackup.exe程序备份日志的存放位置的8.3格式绝对路径
for /f "delims=" %%i in ("%USERPROFILE%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data") do set ntbackuplogdir=%%~si
rem 设置相关参数
set ntmsdata=%local_ip%.dat
set bkf=%local_ip%.bkf
set bks=backup.bks
set log=%local_ip%.log
rem 取得ntbackup.exe程序索引数据库存放位置的8.3格式绝对路径
for /f "delims=" %%i in ("%SystemRoot%") do set ntmsdatadir=%%~si\system32\ntmsdata

set rar=%backupdir%\Rar.exe
set bkf_rar=%bkf%.rar

rem 设置发送邮件软件及smtp参数
set mua=%backupdir%\blat.exe
set admin=backup_report@ddd.net
set smtp=210.212.154.118
set mail_body=%log%
set ntbklog=%backupdir%\ntbklog.exe
set md5sum=%backupdir%\md5sum.exe
set reg=%backupdir%\reg.exe
set md5file=%local_ip%.md5

rem 其它需要备份的文件或目录
rem set stuff=

set schedule=%1

if not exist %bkf% set schedule=weekly
if /i %schedule%==daily (
rem 设置ntbackup方法为 增量
set backup_method=incremental /a
rem 设置ntbackup日志类型为 摘要
set ntbklogtype=s
rem 设置mail主题
set mail_attach=
set systemstate=
) else (
rem 设置ntbackup方法为 普通
set backup_method=normal
rem 设置ntbackup日志类型为 摘要
set ntbklogtype=s
rem 设置mail主题
set mail_attach=-attach %bks%
set systemstate=systemstate
)
set mail_subject=%schedule% Backup Report:

rem 获取当前日期时间值并转化为变量,以便传递给ntbackup作为参数
for /f %%i in (‘date /t‘) do set date=%%i
for /f %%t in (‘time /t‘) do set time=%%t
set curr_daytime=%date% %time%
echo ####### %local_ip% %schedule% Backup Schedule Begin At %curr_daytime% ####### > %log%

rem 参数说明
rem -install 指明发送邮件的服务器的地址和用户
rem 3 代表重试次数
rem
rem 输出注册表相关项目
rem %reg% export HKLM\SOFTWARE\Ipswitch e:\imailbak\imail.reg
rem if ERRORLEVEL 1 (
rem set backup_status=%backup_status% reg:%ERRORLEVEL%
rem set backup_result=%fail%
rem echo Something ERROR In Export REG! >> %log%
rem )
rem 取现在日期及时间
for /f %%d in ( ‘date /t‘ ) do ( for /f %%t in ( ‘time /t‘ ) do ( set curr_daytime=%%d %%t ) )
echo ======= Ntbackup Begin At %curr_daytime% ======= >> %log%
rem 使用ntbackup工具开始备份bks定义的数据
%ntbackup% backup %systemstate% "@%backupdir%\%bks%" /n "Backup on %local_ip% at %date%" /d "Created by %USERNAME%@%local_ip%" /v:yes /r:no /rs:no /hc:off /m %backup_method% /j "Backup on %local_ip%" /l:%ntbklogtype% /f "%backupdir%\%bkf%"
if ERRORLEVEL 1 (
set backup_status=%backup_status% ntbackup:%ERRORLEVEL%
set backup_result=%fail%
echo Something ERROR In NTBACKUP! >> %log%
)
rem
rem 参数说明:
rem
rem systemstate
rem 指定要备份系统状态数据。此项需加入到%bks%文件的最后一项。
rem
rem /J {"job name"}
rem 指定将在日志文件中使用的作业名。作业名通常描述了当前备份作业中备份的文件和文件夹,以及备份文件的日期和时间。
rem
rem /F {"file name"}
rem 逻辑盘路径和文件名,必须是绝对路径。以下开关不能同此开关一起使用:/P /G /T。
rem
rem /D {"set description"}
rem 为每个备份集指定说明。
rem
rem /V:{yes|no}
rem 在备份完成后验证数据。
rem
rem /L:{f|s|n}
rem 指定日志文件类型:f=完全, s=摘要, n=无(不创建日志文件)。
rem
rem /M {backup type}
rem 指定备份类型。必须是如下之一:普通(normal)、副本(copy)、差异(differential)、增量(incremental)或每日(daily)。
rem
rem 取得ntbackup的log文件
for /f "skip=5 tokens=4" %%I in (‘dir /o-d /a-d %ntbackuplogdir%\backup??.log‘) do (
set ntbackuplogfile=%ntbackuplogdir%\%%I
goto outdir
)
:outdir
rem 将ntbackup的log文件插入到%log%文件中
type %ntbackuplogfile% >> %log%
if ERRORLEVEL 1 (
set backup_status=%backup_status% ntbklog:%ERRORLEVEL%
set backup_result=%fail%
echo Can NOT find the ntbackup log file! >> %log%
)

for /f %%d in ( ‘date /t‘ ) do ( for /f %%t in ( ‘time /t‘ ) do ( set curr_daytime=%%d %%t ) )
echo ======= Ntbackup End At %curr_daytime% ======= >> %log%

rem
rem for /f %%d in ( ‘date /t‘ ) do ( for /f %%t in ( ‘time /t‘ ) do ( set curr_daytime=%%d %%t ) )
rem echo ======= Rar.exe Begin At %curr_daytime% ======= >> %log%
rem 每周压缩备份包一次
rem 首先删除已有的旧的压缩文件包
rem del /F/Q %bkf_rar% >> %log%
rem 压缩备份包及索引数据库和ntbackup备份日志文件
if %schedule%==daily goto result
%rar% a -r -y -inul %ntmsdata%.rar %ntmsdatadir% %ntbackuplogdir%\*.log
if ERRORLEVEL 1 (
set backup_status=%backup_status% rar:%ERRORLEVEL%
set backup_result=%fail%
echo RAR ERROR!!! >> %log%
)
rem
rem 取现在日期及时间
rem for /f %%d in ( ‘date /t‘ ) do ( for /f %%t in ( ‘time /t‘ ) do ( set curr_daytime=%%d %%t ) )
rem echo ======= Rar.exe End At %curr_daytime% ======= >> %log%
:result
for /f %%d in ( ‘date /t‘ ) do ( for /f %%t in ( ‘time /t‘ ) do ( set curr_daytime=%%d %%t ) )
echo +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ >> %log%
echo RESULT: >> %log%
dir %local_ip%* >> %log%
if %schedule%==daily goto end
echo ------------------------------------------------------------- >> %log%
rem 生成md5校验码
echo MD5: >> %log%
%md5sum% %local_ip%.bkf > %md5file%
if ERRORLEVEL 1 (
set backup_status= %backup_status% md5sum:%ERRORLEVEL%
set backup_result=%fail%
echo md5sum ERROR!!! >> %log%
)

%md5sum% %ntmsdata%.rar >> %md5file%
if ERRORLEVEL 1 (
set backup_status= %backup_status% md5sum:%ERRORLEVEL%
set backup_result=%fail%
echo md5sum ERROR!!! >> %log%
)

type %md5file% >> %log%
:end
echo +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ >> %log%
echo ####### %local_ip% %schedule% Backup Schedule End At %curr_daytime% ####### >> %log%
rem 发送电子邮件
rem 使用BLAT命令行邮件工具,通过%smtp%邮件服务器发送邮件通知
%mua% %mail_body% -f %admin% -server %smtp% -to %admin% %mail_attach% -s "%local_ip% %mail_subject% %backup_status% %backup_result%" >> %log%

backup.bat 自动备份脚本

标签:job   reg   数据   microsoft   邮件通知   backup   其它   ntbackup   现在   

原文地址:https://www.cnblogs.com/sfqas/p/12181148.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!