数据库还原(ms sql server)

阅读更多
@ECHO off

ECHO database bakup start!

REM Backup Database Script Log Files
SET dbbak_source_dir=d:\cnad_online\
SET dbbak_source_dir2=d:\cnad_online
SET target_dir=d:\
SET database_dir=e:\database\
if not exist %dbbak_source_dir% mkdir %dbbak_source_dir%

REM Backup Database Name
SET Database_Name=cnad
SET Date_Today=%date:~0,4%%date:~5,2%%date:~8,2%


cd %dbbak_source_dir2%

ECHO download file
REM 下载文件
REM echo wget -c http://ip地址/log/%Database_Name%_%Date_Today%.rar

if not exist %Database_Name%_%Date_Today%.rar wget -c http://218.30.126.163/log/%Database_Name%_%Date_Today%.rar

REM 解压缩文件
rar x -y  %dbbak_source_dir%%Database_Name%_%Date_Today%.rar %target_dir%


ECHO restore start!
REM 删除数据库
ECHO use master >%dbbak_source_dir%restore_%Date_Today%.sql
ECHO go >>%dbbak_source_dir%restore_%Date_Today%.sql
ECHO drop database %Database_Name%>>%dbbak_source_dir%restore_%Date_Today%.sql

REM 还原数据库
ECHO use master >>%dbbak_source_dir%restore_%Date_Today%.sql
ECHO go >>%dbbak_source_dir%restore_%Date_Today%.sql
ECHO restore database %Database_Name% >>%dbbak_source_dir%restore_%Date_Today%.sql
ECHO FROM DISK = '%dbbak_source_dir%%Database_Name%_%Date_Today%.bak' >>%dbbak_source_dir%restore_%Date_Today%.sql
ECHO WITH NORECOVERY,>>%dbbak_source_dir%restore_%Date_Today%.sql
ECHO MOVE 'cnad' TO '%database_dir%cnad.mdf',>>%dbbak_source_dir%restore_%Date_Today%.sql
ECHO MOVE 'cnad_log' TO '%database_dir%cnad_log.ldf';>>%dbbak_source_dir%restore_%Date_Today%.sql
ECHO go >>%dbbak_source_dir%restore_%Date_Today%.sql
REM 使用末move参数是因为现在的服务器上数据库的路径为g盘,因此需要移动到备份机器存放数据库的盘上

REM 还原数据库
ECHO use master >>%dbbak_source_dir%restore_%Date_Today%.sql
ECHO go >>%dbbak_source_dir%restore_%Date_Today%.sql
ECHO restore database %Database_Name% >>%dbbak_source_dir%restore_%Date_Today%.sql
ECHO WITH RECOVERY;>>%dbbak_source_dir%restore_%Date_Today%.sql

REM osql.exe path
SET OSQL_PATH=C:\Program Files\Microsoft SQL Server\90\Tools\Binn\

REM Database user name and password
SET DBUser=***
SET DBpassword=***
ECHO RESTORE DATABASE 

REM ECHO RESTORE DATABASE
"%OSQL_PATH%osql.exe" -U %DBUser% -P %DBpassword% -i %dbbak_source_dir%restore_%Date_Today%.sql -o %dbbak_source_dir%%Database_Name%_%Date_Today%.log

ECHO restore complete!



REM 删除文件
ECHO del start!

REM DEL /Q %dbbak_source_dir%backup_%Date_Today%.sql
DEL /Q %dbbak_source_dir%%Database_Name%_%Date_Today%.log
DEL /Q %dbbak_source_dir%%Database_Name%_%Date_Today%.bak
DEL /Q %dbbak_source_dir%restore_%Date_Today%.sql
REM DEL /Q %dbbak_source_dir%%Database_Name%_%Date_Today%.log

REM 删除1天以前的数据 测试用,正式使用7天
REM FORFILES的目录必须不能以\结尾
FORFILES /P %dbbak_source_dir2% /D -1 -M %Database_Name%*.rar /C "cmd /c del @file"



REM 把信息都记录到日志里
ECHO Date_Today=%date:~0,4%%date:~5,2%%date:~8,2%>>%dbbak_source_dir%%Database_Name%_%Date_Today%.log
ECHO if not exist %Database_Name%_%Date_Today%.rar wget -c http://218.30.126.163/log/%Database_Name%_%Date_Today%.rar>>%dbbak_source_dir%%Database_Name%_%Date_Today%.log
ECHO rar x -y  %dbbak_source_dir%%Database_Name%_%Date_Today%.rar %target_dir%>>%dbbak_source_dir%%Database_Name%_%Date_Today%.log
ECHO "%OSQL_PATH%osql.exe" -U %DBUser% -P %DBpassword% -i %dbbak_source_dir%restore_%Date_Today%.sql -o %dbbak_source_dir%%Database_Name%_%Date_Today%.log>>%dbbak_source_dir%%Database_Name%_%Date_Today%.log
REM ECHO DEL /Q %dbbak_source_dir%backup_%Date_Today%.sql>>%dbbak_source_dir%%Database_Name%_%Date_Today%.log
ECHO DEL /Q %dbbak_source_dir%%Database_Name%_%Date_Today%.log>>%dbbak_source_dir%%Database_Name%_%Date_Today%.log
ECHO DEL /Q %dbbak_source_dir%%Database_Name%_%Date_Today%.bak>>%dbbak_source_dir%%Database_Name%_%Date_Today%.log
ECHO DEL /Q %dbbak_source_dir%restore_%Date_Today%.sql>>%dbbak_source_dir%%Database_Name%_%Date_Today%.log
ECHO FORFILES /P %dbbak_source_dir2% /D -1 -M %Database_Name%*.rar /C "cmd /c del @file">>%dbbak_source_dir%%Database_Name%_%Date_Today%.log




你可能感兴趣的:(SQL,Server,SQL,C,C++,C#)