SQL Server regular backup database (job)

Source: Internet
Author: User
Use isignaturekey -- Data name

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. mysp_all_isignaturekey') and objectproperty (ID, n' isprocedure ') = 1)
Drop procedure [DBO]. mysp_all_isignaturekey
Go

Create procedure mysp_all_isignaturekey
@ Bakpath varchar (4000) -- backup path
As
Backup database isignaturekey to disk = @ bakpath
Go

If exists (select * From DBO. sysobjects where id = object_id (n' [DBO]. [p_createjob] ') and objectproperty (ID, n' isprocedure') = 1)
Drop procedure [DBO]. [p_createjob]
Go

Create proc p_createjob
@ Jobname varchar (100), -- job name
@ SQL varchar (8000), -- command to be executed
@ Servername sysname = '', -- job server name
@ Dbname sysname = '', -- the default value is the current database name.
@ Freqtype varchar (6) = 'day', -- time period, month, week, day
@ Fsinterval Int = 1, -- the number of repetitions relative to the day
@ Time Int = 170000 -- execution start time. For repeated jobs, the time ranges from 0.
As
If isnull (@ dbname, '') ='' set @ dbname = db_name ()
-- Create a job
If exists (select 1 from MSDB .. sysjobs where name = @ jobname)
Exec MSDB .. sp_delete_job @ job_name = @ jobname
Exec MSDB .. sp_add_job @ job_name = @ jobname
-- Create a job
Exec MSDB .. sp_add_jobstep @ job_name = @ jobname,
@ Step_name = 'bakdatastart ',
@ Subsystem = 'tsql ',
@ Database_name = @ dbname,
@ Command = @ SQL,
@ Retry_attempts = 5, -- number of retries
@ Retry_interval = 5 -- Retry Interval
-- Create Scheduling
Declare @ FTYPE int, @ fstype int, @ ffactor int
Select @ FTYPE = case @ freqtype when 'day' then 4
When 'Week' then 8
When 'month' then 16 end
, @ Fstype = case @ fsinterval when 1 then 0 else 8 end
If @ fsinterval <> 1 Set @ time = 0
Set @ ffactor = case @ freqtype when 'day' then 0 else 1 end

Exec MSDB .. sp_add_jobschedule @ job_name = @ jobname,
@ Name = 'schedule ',
@ Freq_type = @ FTYPE, --- every day, 8 weeks, 16 months
@ Freq_interval = 1, -- number of repeated executions
@ Freq_subday_interval = @ fsinterval, -- recurrence
@ Freq_recurrence_factor = @ ffactor,
@ Active_start_time = @ time -- execute at 17:00:00 pm

If @ servername =''
Set @ servername = @ servername
Exec MSDB .. sp_add_jobserver @ job_name = @ jobname,
@ SERVER_NAME = @ servername
Go

Exec p_createjob @ jobname = 'isignaturekey. Bak ',
@ SQL = 'exec mysp_all_isignaturekey @ bakpath = "D: \ isignaturekey. Bak" ', -- bakpath backup path
@ Dbname = 'isignaturekey ',
@ Freqtype = 'day ',
@ Time = '000000' -- the backup time hhmmss (HH: 24 h, MM: min, SS: Sec)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.