declare @sql varchar(500)select @sql='\\192.168.8.130\dbbak\Dcip.Sys_1.23' '_db_' convert(varchar(10),getdate(),112) substring(convert(varchar(10),getdate(),108),1,2) '.bak' exec master..xp_cmdshell 'net use \\192.168.8.130\dbbak Luxijituan@123 /user:dbbak'backup database [Dcip.Sys_1.23] to disk=@sql --备份数据库,这里的10.2.0.12为远程机器的ip,WIN-5TLP2MKGFG5为远程机器的机器名,dbname为本地sqlserver服务器要备份的数据库 go declare @sql varchar(500) select @sql='d:\dbbak\Dcip.Sys_1.23' '_db_' convert(varchar(10),getdate(),112) substring(convert(varchar(10),getdate(),108),1,2) '.bak' select @sqlbackup database Dcip.Sys_1.23 to disk=@sql godeclare @sql varchar(500)select @sql='del ' '\\192.168.8.130\dbbak\Dcip.Sys_1.23' '_db_' convert(varchar(10),dateadd(day,-15,getdate()),112) substring(convert(varchar(10),dateadd(day,-15,getdate()),108),1,2) '.bak'select @sqlexec master..xp_cmdshell @sql --删除7天前的备份,也就是只保留7个最新备份go--本地备份数据库 declare @sql varchar(500),@dir varchar(50),@dbname varchar(50) set @dir = 'd:\dbbak\' set @dbname = '[BM.Sys_1.23.1]' select @sql=@dir @dbname convert(varchar(8),getdate(),112) substring(convert(varchar(10),getdate(),108),1,2) '.bak' backup database [BM.Sys_1.23.1] to disk=@sql go
评论