请问能用SQL 建立维护计划吗 ? 要求自定义备份文件名称 自定义删除保留备份文件天数
10 个解决方案
#1
可以的, 用SQL执行备份, 配合DOS命令删除过期文件.
#2
参照
http://blog.csdn.net/roy_88/article/details/1758497
删除可以指定备份文件
http://blog.csdn.net/roy_88/article/details/1758497
删除可以指定备份文件
EXECUTE master.dbo.xp_delete_file 0,N'D:\BackupDB',N'bak',N'2016-07-14T23:03:21'--删除指定日期之前文件
#3
首先 谢谢二位版主 回答
我这里想要的是 如何用SQL 建立 备份计划任务 、 调度 而不是用向导
我这里想要的是 如何用SQL 建立 备份计划任务 、 调度 而不是用向导
#4
直接用提供方法,在JOB里调度指定执行日期和周期频率就行了
#5
==> 我们给你的方法即是用纯SQL实现的,非向导.
#6
BACKUP DATABASE [db] TO DISK = 'd:\'+convert(varchar(8),getdate(),112)+'.bak' WITH INIT
查询分析器里面 执行 提示 + 号附近错误
#7
declare @tsql varchar(6000)
select @tsql='BACKUP DATABASE [db] TO DISK = ''d:\'+convert(varchar(8),getdate(),112)+'.bak'' WITH INIT '
exec(@tsql)
#8
或者这样写,
declare @file varchar(100)
select @file='D:\'+convert(varchar(8),getdate(),112)+'.bak'
backup database [db] to disk=@file with init
#9
EXEC ('BEGIN TRANSACTION' + ' ' +
'DECLARE @JobID BINARY(16)' + ' ' +
'DECLARE @ReturnCode INT' + ' ' +
'' + ' ' +
'SELECT @ReturnCode = 0' + ' ' +
'IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N''[Uncategorized (Local)]'') < 1' + ' ' +
' EXECUTE msdb.dbo.sp_add_category @name = N''[Uncategorized (Local)]''' + ' ' +
'SELECT @JobID = job_id FROM msdb.dbo.sysjobs WHERE (name = N''libdataZ 备份'')' + ' ' +
'IF (@JobID IS NOT NULL)' + ' ' +
'BEGIN' + ' ' +
' IF (EXISTS (SELECT * FROM msdb.dbo.sysjobservers WHERE (job_id = @JobID) AND (server_id <> 0)))' + ' ' +
' BEGIN' + ' ' +
' RAISERROR (N''无法导入作业“libdataZ 备份”,因为已经有相同名称的多重服务器作业。'', 16, 1)' + ' ' +
' GOTO QuitWithRollback' + ' ' +
' END' + ' ' +
' ELSE' + ' ' +
' EXECUTE msdb.dbo.sp_delete_job @job_name = N''libdataZ 备份''' + ' ' +
' SELECT @JobID = NULL' + ' ' +
'END' + ' ' +
'BEGIN' + ' ' +
' EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N''libdataZ 备份'', @owner_login_name = N''sa'', @description = N''没有可用的描述。'', @category_name = N''[Uncategorized (WCS-PC\SQL2000)]'', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0' + ' ' +
' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' + ' ' +
' EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N''第 1 步'', '
+'@command = N''BACKUP DATABASE [libdataZ] TO DISK = N''''d:\''+convert(varchar(8),getdate(),112)+''.bak'''' WITH INIT '' '
+', @database_name = N''master'', @server = N'''', @database_user_name = N'''', @subsystem = N''TSQL'', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 0, @output_file_name = N'''', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2' + ' ' +
' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' + ' ' +
' EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1' + ' ' +
' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' + ' ' +
' EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = N''第 1 调度'', @enabled = 1, @freq_type = 4, @active_start_date = null, @active_start_time = 124600, @freq_interval = 1, @freq_subday_type = 1, @freq_subday_interval = 0, @freq_relative_interval = 0, @freq_recurrence_factor = 1, @active_end_date = 99991231, @active_end_time = 235959' + ' ' +
' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' + ' ' +
' EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N''(local)''' + ' ' +
' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' + ' ' +
'END' + ' ' +
'COMMIT TRANSACTION' + ' ' +
'GOTO EndSave' + ' ' +
'QuitWithRollback:' + ' ' +
' IF (@@TRANCOUNT > 0) begin ROLLBACK TRANSACTION end' + ' ' +
'EndSave:
EXEC @RETURNCODE = MSDB.DBO.SP_START_JOB @JOB_ID = @JobID --启动作业
--RETURN @RETURNCODE
')
我想把 备份文件名称改成 日期 格式 convert(varchar(8),getdate(),112) 查询分析器运行 提示 +号附近 有错误
把这句 改成 @command = N''BACKUP DATABASE [libdataZ] TO DISK = N''''d:\123.bak'''' WITH INIT ''
就没问题 不知道错在哪里了
#10
错误在第26行: 执行msdb.dbo.sp_add_jobstep时, 给参数@command赋值时不可用动态拼凑SQL.
须这样写,
须这样写,
declare @tsql varchar(1000)
select @tsql=N'BACKUP DATABASE [libdataZ] TO DISK = N''d:\'+convert(varchar(8),getdate(),112)+'.bak'' WITH INIT '
execute @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N'第 1 步', @command = @tsql , @database_name = N'master', @server = N'', @database_user_name = N'', @subsystem = N'TSQL', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 0, @output_file_name = N'', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2
#1
可以的, 用SQL执行备份, 配合DOS命令删除过期文件.
#2
参照
http://blog.csdn.net/roy_88/article/details/1758497
删除可以指定备份文件
http://blog.csdn.net/roy_88/article/details/1758497
删除可以指定备份文件
EXECUTE master.dbo.xp_delete_file 0,N'D:\BackupDB',N'bak',N'2016-07-14T23:03:21'--删除指定日期之前文件
#3
首先 谢谢二位版主 回答
我这里想要的是 如何用SQL 建立 备份计划任务 、 调度 而不是用向导
我这里想要的是 如何用SQL 建立 备份计划任务 、 调度 而不是用向导
#4
直接用提供方法,在JOB里调度指定执行日期和周期频率就行了
#5
==> 我们给你的方法即是用纯SQL实现的,非向导.
#6
BACKUP DATABASE [db] TO DISK = 'd:\'+convert(varchar(8),getdate(),112)+'.bak' WITH INIT
查询分析器里面 执行 提示 + 号附近错误
#7
declare @tsql varchar(6000)
select @tsql='BACKUP DATABASE [db] TO DISK = ''d:\'+convert(varchar(8),getdate(),112)+'.bak'' WITH INIT '
exec(@tsql)
#8
或者这样写,
declare @file varchar(100)
select @file='D:\'+convert(varchar(8),getdate(),112)+'.bak'
backup database [db] to disk=@file with init
#9
EXEC ('BEGIN TRANSACTION' + ' ' +
'DECLARE @JobID BINARY(16)' + ' ' +
'DECLARE @ReturnCode INT' + ' ' +
'' + ' ' +
'SELECT @ReturnCode = 0' + ' ' +
'IF (SELECT COUNT(*) FROM msdb.dbo.syscategories WHERE name = N''[Uncategorized (Local)]'') < 1' + ' ' +
' EXECUTE msdb.dbo.sp_add_category @name = N''[Uncategorized (Local)]''' + ' ' +
'SELECT @JobID = job_id FROM msdb.dbo.sysjobs WHERE (name = N''libdataZ 备份'')' + ' ' +
'IF (@JobID IS NOT NULL)' + ' ' +
'BEGIN' + ' ' +
' IF (EXISTS (SELECT * FROM msdb.dbo.sysjobservers WHERE (job_id = @JobID) AND (server_id <> 0)))' + ' ' +
' BEGIN' + ' ' +
' RAISERROR (N''无法导入作业“libdataZ 备份”,因为已经有相同名称的多重服务器作业。'', 16, 1)' + ' ' +
' GOTO QuitWithRollback' + ' ' +
' END' + ' ' +
' ELSE' + ' ' +
' EXECUTE msdb.dbo.sp_delete_job @job_name = N''libdataZ 备份''' + ' ' +
' SELECT @JobID = NULL' + ' ' +
'END' + ' ' +
'BEGIN' + ' ' +
' EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N''libdataZ 备份'', @owner_login_name = N''sa'', @description = N''没有可用的描述。'', @category_name = N''[Uncategorized (WCS-PC\SQL2000)]'', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0' + ' ' +
' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' + ' ' +
' EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N''第 1 步'', '
+'@command = N''BACKUP DATABASE [libdataZ] TO DISK = N''''d:\''+convert(varchar(8),getdate(),112)+''.bak'''' WITH INIT '' '
+', @database_name = N''master'', @server = N'''', @database_user_name = N'''', @subsystem = N''TSQL'', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 0, @output_file_name = N'''', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2' + ' ' +
' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' + ' ' +
' EXECUTE @ReturnCode = msdb.dbo.sp_update_job @job_id = @JobID, @start_step_id = 1' + ' ' +
' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' + ' ' +
' EXECUTE @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id = @JobID, @name = N''第 1 调度'', @enabled = 1, @freq_type = 4, @active_start_date = null, @active_start_time = 124600, @freq_interval = 1, @freq_subday_type = 1, @freq_subday_interval = 0, @freq_relative_interval = 0, @freq_recurrence_factor = 1, @active_end_date = 99991231, @active_end_time = 235959' + ' ' +
' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' + ' ' +
' EXECUTE @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @JobID, @server_name = N''(local)''' + ' ' +
' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback' + ' ' +
'END' + ' ' +
'COMMIT TRANSACTION' + ' ' +
'GOTO EndSave' + ' ' +
'QuitWithRollback:' + ' ' +
' IF (@@TRANCOUNT > 0) begin ROLLBACK TRANSACTION end' + ' ' +
'EndSave:
EXEC @RETURNCODE = MSDB.DBO.SP_START_JOB @JOB_ID = @JobID --启动作业
--RETURN @RETURNCODE
')
我想把 备份文件名称改成 日期 格式 convert(varchar(8),getdate(),112) 查询分析器运行 提示 +号附近 有错误
把这句 改成 @command = N''BACKUP DATABASE [libdataZ] TO DISK = N''''d:\123.bak'''' WITH INIT ''
就没问题 不知道错在哪里了
#10
错误在第26行: 执行msdb.dbo.sp_add_jobstep时, 给参数@command赋值时不可用动态拼凑SQL.
须这样写,
须这样写,
declare @tsql varchar(1000)
select @tsql=N'BACKUP DATABASE [libdataZ] TO DISK = N''d:\'+convert(varchar(8),getdate(),112)+'.bak'' WITH INIT '
execute @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID, @step_id = 1, @step_name = N'第 1 步', @command = @tsql , @database_name = N'master', @server = N'', @database_user_name = N'', @subsystem = N'TSQL', @cmdexec_success_code = 0, @flags = 0, @retry_attempts = 0, @retry_interval = 0, @output_file_name = N'', @on_success_step_id = 0, @on_success_action = 1, @on_fail_step_id = 0, @on_fail_action = 2