/****** Object: Stored Procedure dbo.usp_dbbackup Script Date: 23/12/99 10:34:44 ******/ if exists (select * from sysobjects where id = object_id('dbo.usp_dbbackup') and sysstat & 0xf = 4) drop procedure dbo.usp_dbbackup GO
SELECT @jobname = 'usp_dbbackup' SELECT @cmdshell = 'master..xp_cmdshell' SELECT @usp_dbccrun = 'master..usp_dbccrun' SET NOCOUNT ON
/* Check backup option is either DATABASE or TRANSLOG */
IF (@bkup_type <> 'DATABASE') IF (@bkup_type <> 'TRANSLOG') BEGIN SELECT @errmsg = 'Backup type ' + @bkup_type + ' is invalid. Use either DATABASE or TRANSLOG options.' RAISERROR(@errmsg, -1, -1) RETURN (1) END
IF @dbname1 IS NULL SELECT @dbname1 = '%' ELSE BEGIN IF NOT EXISTS (SELECT * FROM sysdatabases WHERE name LIKE @dbname1) BEGIN RAISERROR(15010, -1, -1, @dbname1) RETURN (1) END END
IF (@bkup_wkend = 'Y' AND (@day = 'Sunday' OR @day = 'Saturday')) SELECT @do_backup = 'Y' ELSE IF (@day in ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')) SELECT @do_backup = 'Y' ELSE SELECT @do_backup = 'N'
IF @do_backup = 'Y' BEGIN DECLARE @dbname2 VARCHAR(50)
DECLARE c_dbname CURSOR FOR SELECT name FROM sysdatabases WHERE name LIKE @dbname1 AND name NOT IN ('tempdb') --ORDER BY dbid /* rem out because of "DB-Library Process Dead - Connection Broken" */
OPEN c_dbname
FETCH NEXT FROM c_dbname INTO @dbname2
WHILE (@@FETCH_STATUS <> -1) BEGIN IF (@@FETCH_STATUS = -2) BEGIN FETCH NEXT FROM c_dbname INTO @dbname2 CONTINUE END
/* Perform existance checks for DATABASE, TRANSLOG and BKUPLOGS directories */
IF (@result <> 0) BEGIN SELECT @errmsg = @jobname + ': ' + @dbname2 + ' destination BKUPLOGS backup directory does not exist.' RAISERROR(@errmsg, -1, -1) SELECT @command = 'mkdir ' + @bkuplogs_dir EXECUTE @result = @cmdshell @command, NO_OUTPUT IF (@result = 0) BEGIN SELECT @msg = @jobname + ': BKUPLOGS directory ' + @bkuplogs_dir + ' created.' PRINT @msg END ELSE BEGIN SELECT @msg = @jobname + ': BKUPLOGS Directory ' + @bkuplogs_dir + ' could not be created.' PRINT @msg END END
IF (@bkup_type = 'DATABASE' OR @bkup_type = 'TRANSLOG') BEGIN
/* Check syslogs does exists in its own segment. If true backup tlog.*/
IF EXISTS (SELECT a.* FROM sysdatabases a, sysusages b, spt_values c WHERE a.dbid = b.dbid AND c.number = b.segmap AND c.type = 'S' AND b.segmap = 4 AND a.status NOT IN (4, 8) AND a.name LIKE @dbname2) BEGIN SELECT @msg = @jobname + ': Transaction log on seperate device. Backing up transaction log for ' + @dbname2 + '...' PRINT @msg
IF (@result <> 0) BEGIN SELECT @errmsg = @jobname + ': Error backing up transaction log for ' + @dbname2 + '.' PRINT @errmsg END
ELSE BEGIN SELECT @msg = @jobname + ': Transaction log for ' + @dbname2 + ' backed up successfully.' PRINT @msg END
END ELSE BEGIN SELECT @msg = @jobname + ': Transaction log not on seperate device. Cannot backup transaction log for ' + @dbname2 + '.' PRINT @msg END END
Jeg skal bare lave en enkelt backup af en enkelt database i min SQL server instans. Jeg er overbevist om (håber) at SQL 2K har en funktion der kan gøre det lidt lettere end en 200 liniers stored procedure....
ocp -> dit eksemple se mere overskueligt ud, hvor kan jeg få en lidt mere uddybene forklaring af hvad der skal til for at effektuere det?
USE master -- Tilføj en backup device, "MyNwind_1", som er en fil ("disk") med navnet MyNwind_1.dat EXEC sp_addumpdevice 'disk', 'MyNwind_1', DISK ='c:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\MyNwind_1.dat'
-- Back up the full MyNwind database. BACKUP DATABASE MyNwind TO MyNwind_1
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.