在D盘建立文件夹bak
--第一步: 创建自动备份作业脚本代码如下(可以直接跑):
USE msdb--管理sqlserver作业
GO
/****** Object: Job [备份JSTMBS] Script Date: 2016/8/22 16:08:29 ******/
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
/****** Object: JobCategory [Database Maintenance] Script Date: 2016/8/22 16:08:29 ******/
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'Database Maintenance' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Database Maintenance'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'备份JSTMBS',
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'每天上午数据库备份',
@category_name=N'Database Maintenance',
@owner_login_name=N'jstmbsuser', @job_id = @jobId OUTPUT
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
/****** Object: Step [每天备份] Script Date: 2016/8/22 16:08:29 ******/
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'每天备份',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'declare @strDate nvarchar(max);
set @strDate = REPLACE( N''D:\bak\JSTMBS_backup_'' + Convert(nvarchar(10),getdate(),23) + N''.bak'',''-'',''_'')
BACKUP DATABASE [JSTMBS] TO DISK = @strDate WITH NOFORMAT, NOINIT, NAME = N''JSTMBS_backup-完整 数据库 备份'', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO',
@database_name=N'JSTMBS',
@flags=0
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'每天备份',
@enabled=1,
@freq_type=4,
@freq_interval=1,
@freq_subday_type=1,
@freq_subday_interval=0,
@freq_relative_interval=0,
@freq_recurrence_factor=0,
@active_start_date=20160822,
@active_end_date=99991231,
@active_start_time=50000,
@active_end_time=235959,
@schedule_uid=N'f90cfdc3-cb5d-4e19-9e93-99b8921d36c1'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO
第二步:压缩
1.下载WinARA解压程序默认安装在c盘;
2.建立.bat文件,bak.bat 内容如下:
c:\Progra~2\WinRAR\WinRAR.exe a D:\bak\JSTMBS_backup_%date:~0,4%_%date:~5,2%_%date:~8,2% d:\bak\JSTMBS_backup_*%date:~0,4%_%date:~5,2%_%date:~8,2%.bak
3.建立计划:控制面板——所有控制面板项——管理工具——任务计划——创建基本任务——操作——新建——程序或脚本——浏览——bak.bat
第三步:删除多余的压缩文件和备份文件:
1.创建vbs文件:autoDeleteOldBak.vbs ,内容如下:
const reMainCount = 1 '保留几day
Dim fso,dc,file,files,folder,subfolders,subfolder
set fso=createobject("Scripting.FileSystemObject")
set folder=fso.getfolder("D:\bak")
Dim today
today=Now
for each tempFile in folder.files
dim arr
arr=Split( tempFile,"_backup_")
if UBound(arr)>0 then
dim tempDate,diff
tempDate=Cdate( Replace( Left(arr(1),10),"_","-"))
diff= DateDiff("d",tempDate,today)
if (diff>reMainCount) then
fso.deleteFile(tempFile),true
end if
end if
next
set fso=nothing
2.创建autoDeleteOld.bat 文件,内容如下:
cscript D:/bak/autoDeleteOldBak.vbs
3.建立windows计划,防第二步。