通过批处理,实现SQL Server数据库定期备份到网络共享磁盘

@echo off
rem --------------------- 配置: 网络共享, 配置目录(数据库备份目录)---------------------
set "NET_SERVER=192.168.1.100"
set "NET_USERNAME=dbbackup"
set "NET_PASSWORD=123456"
set "NET_DIR=sql_backup"
set "LOCAL_DIR=z:"

 

rem 调用数据库备份存储过程,参数为本地目录. 数据库返回"过期需要删除的备份文件"
echo begin_backup_db
sqlcmd -E -S 127.0.0.1,1333 -d master -v BAK_PATH="'//%NET_SERVER%/%NET_DIR%'" -Q "execute dbo.p_job_backup $(BAK_PATH);" -t 4800 -h -1 -W | findstr bak>>del_file.txt

 

rem 开始删除远程路径上的过期文件,需要先赋予删除权限,见http://blog.csdn.net/xiaoxu0123/archive/2010/05/28/5629779.aspx

echo begin_del_remote
net use %LOCAL_DIR% //%NET_SERVER%/%NET_DIR% %NET_PASSWORD% /user:%NET_USERNAME%
for /f "delims=" %%i in (del_file.txt) do (
del /f/q %LOCAL_DIR%/%%i
)
del /f/q del_file.txt
net use %LOCAL_DIR% /delete

 

rem 启动数据库发送邮件(即发送错误日志)
sqlcmd -E -S 127.0.0.1,1333 -d master -Q "execute dbo.p_sendmail;" -t 120
echo end.
goto :eof

阅读更多
个人分类: Windows
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭