备份

2017-04-18  本文已影响0人  Asa_Guo
--1、启用xp_cmdshell
USE master
EXEC sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
EXEC sp_configure   'show advanced options', 0
RECONFIGURE WITH OVERRIDE
--2、使用变量进行备份
declare @db sysname
declare @dbpath varchar(120)
set @db='shenyin'
--文件名根据数据库名称+日期YYYYMMDD+扩展名
set @dbpath='D:\'+@db+convert(varchar(10),getdate(),112)+'.bak'

--3、在sql中建个映射
exec master..xp_cmdshell 'net use Y: \\192.168.0.156\DBBackup "jindou" /user:192.168.0.156\Administrator'
/*
 说明:
 Y:是映射网络路径对应本机的盘符
 192.168.0.156是我本机的IP
 DBBackup是我本机共享的文件夹
 Administrator是登陆的用户名 
 "jindou" 是Administrator用户的密码
*/
--4、备份(@db是要备份的数据库名称)
Backup Database @db To disk=@dbpath
--5、COPY
declare @copypath varchar(200)
set @copypath = 'copy '+@dbpath+' Y:'
select @copypath
exec master..xp_cmdshell @copypath 
--6、删除(这句可以去掉)
declare @deletepath varchar(200)
set @deletepath = 'del '+@dbpath
select @deletepath
exec master..xp_cmdshell @deletepath 
--7、完成后删除映射
exec master..xp_cmdshell 'net use Y: /delete'
--8、关闭xp_cmdshell
USE master 
EXEC sp_configure 'show advanced options', 1 
RECONFIGURE WITH OVERRIDE 
EXEC sp_configure 'xp_cmdshell', 0 
RECONFIGURE WITH OVERRIDE 
EXEC sp_configure   'show advanced options', 0
RECONFIGURE WITH OVERRIDE 
上一篇下一篇

猜你喜欢

热点阅读