需求:备份数据库后用WinRAR压缩保存到指定目录
- SQL SERVER 自动备份数据库脚本
-- 创建文件名
declare @filename varchar(1024)
declare @path varchar(1024)
set @path = N'E:\DB_Backup\Chemical\';
declare @extension_name varchar(16)
set @extension_name = N'bak';
set @filename = convert(varchar(1024), getdate(), 120)
set @filename = replace(@filename, ':', '')
set @filename = replace(@filename, '-', '')
set @filename = replace(@filename, ' ', '')
set @filename = N'Chemical_' + @filename + '_' + convert (varchar(3), datepart(ms, getdate())) + N'.' + @extension_name
-- 得到完整目标文件,数据库将备份到这个中
set @filename = @path + @filename
-- select @filename
-- 开始备份
backup database [Chemical] to disk = @filename with noformat, noinit, name = N'Chemical', skip, norewind, nounload, stats = 10
注:以上脚本源自于互联网,如有侵权,请联系作者删除
- 批处理运行备份脚本后调用RAR打包保存
sqlcmd -S . -i E:\DB_Backup\Ch.sql
D:\"Program Files"\WinRAR\RAR.exe a -ep -df -agyyyymmddhhmmss E:\DB_Backup\Chemical\Chemical_ E:\DB_Backup\Chemical\*.bak