Author |
Topic |
redbrad0
Posting Yak Master
176 Posts |
Posted - 2004-07-05 : 22:40:00
|
I am trying to back up SQL Server onto another server. I have tired by setting up a DB Maintaince plan and set it to a file \\server\c$\sql_database\db but it never seemed to backup. Can someone help me find a way to back up the DB on another server?Quality Web Hosting & Designhttp://www.eznetideas.com |
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-07-05 : 22:52:46
|
Well, first of all don't use the Maintenance Plans because they suck. Second, the account you're running the backup with needs to have access to the share on the other server. After you have accomplished this, you can run in Query Analyzer or schedule as a job:BACKUP DATABASE databaseTO DISK = '\\server\c$\sql_database\db\database.bakWITHINITThen you can actually look at the errors you're getting if there are any, which the maintenance plan never likes to disclose to you. Let us know if you need help at that point.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2004-07-05 : 23:45:13
|
What if I want to backup all of the database's on the SQL Server, this looks like it would work with one DBQuality Web Hosting & Designhttp://www.eznetideas.com |
 |
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-07-06 : 00:37:41
|
Well, there are a million ways to do this. One way is to setup a job for each database and run this script in it. You could also use Tara's script to do it at http://weblogs.sqlteam.com/tarad .Here's another script for you to look at:ALTER PROCEDURE sa_BackupWebDatabases--Name: sa_BackupWebDatabases----Purpose: Creates a nightly backup of all web databases, including any newly created.-- --Creates directories for any newly created databases.-- --The account running this must have the appropriate permissions to do this or it will break.-- --Makes a backup. Should be scheduled to run nightly.----Type: System Administration Utility----Format: EXEC sa_BackupWebDatabases @location----Example_1: EXEC sa_BackupWebDatabases '\\OFFICECOMPUTER\D$\mssql\backup\' -- ****This base location MUST already be built.****----Author: Date: Type: Description:--Derrick Leggett 01/27/2004 Created Creates backups of all web databases, including any newly created.-- Will dynamically create the directories needed.--@location NVARCHAR(256)AS--Record the time procedure started.SELECT 'Backup Process Starting::' + CONVERT(VARCHAR,GETDATE(),22)--Declare needed variables.DECLARE @min INT, @max INT, @sql NVARCHAR(4000), @database_name NVARCHAR(256), @directory_name NVARCHAR(256), @error NVARCHAR(256)--Create error handle.SELECT @error = 'sa_BackupWebDatabases::'--Table to hold the database names.DECLARE @database TABLE( ident INT IDENTITY(1,1) PRIMARY KEY, database_name NVARCHAR(256))INSERT @database(database_name) SELECT name FROM master.dbo.sysdatabases WHERE name <> 'tempdb'IF @@ERROR <> 0 BEGIN SELECT @error = @error + 'Failure on @database insert.' GOTO ERROR_HANDLE END--Record the time directory process started.SELECT 'Directory Process Starting::' + CONVERT(VARCHAR,GETDATE(),22)--Table to hold the directory names.DIRECTORY: --This section verifies that the appropriate directories exist. If they do not, it creates them.IF (SELECT OBJECT_ID('tempdb..#directory')) IS NOT NULL BEGIN DROP TABLE #directory ENDCREATE TABLE #directory( ident INT IDENTITY(1,1) PRIMARY KEY, directory_name NVARCHAR(256))IF @@ERROR <> 0 BEGIN SELECT @error = @error + 'Failure on #directory create.' GOTO ERROR_HANDLE ENDSELECT @sql = 'DIR/A:D /B ' + @locationINSERT #directory(directory_name) EXEC master..xp_cmdshell @sqlIF @@ERROR <> 0 BEGIN SELECT @error = @error + 'Failure on #directory insert.' GOTO ERROR_HANDLE ENDDELETE #directoryWHERE directory_name IS NULLIF @@ERROR <> 0 BEGIN SELECT @error = @error + 'Failure on #directory delete.' GOTO ERROR_HANDLE ENDSELECT @sql = ( SELECT TOP 1 d1.database_name FROM @database d1 LEFT OUTER JOIN #directory d2 ON d1.database_name = d2.directory_name WHERE d2.directory_name IS NULL)IF @@ERROR <> 0 BEGIN SELECT @error = @error + 'Failure on @sql population with database_name.' GOTO ERROR_HANDLE ENDIF @sql IS NOT NULL BEGIN SELECT @sql = 'MD ' + @location + @sql EXEC master..xp_cmdshell @sql GOTO DIRECTORY ENDIF @@ERROR <> 0 BEGIN SELECT @error = @error + 'Failure on make directory statement.' GOTO ERROR_HANDLE END--Record the time directory process ended.SELECT 'Directory Process Ended::' + CONVERT(VARCHAR,GETDATE(),22)--Loop through the database names and backup each database.SELECT @min = 1, @max = (SELECT MAX(ident) FROM @database)WHILE @min <= @maxBEGIN --Capture the database name. SELECT @database_name = (SELECT database_name FROM @database WHERE ident = @min) --Record the database backup started. SELECT 'Database Backup Started::' + CONVERT(VARCHAR,GETDATE(),22) SELECT @sql = ' BACKUP DATABASE ' + @database_name + ' TO DISK = ''' + @location + @database_name + '\' + @database_name + '.bak'' WITH INIT, STATS = 1' PRINT @sql EXEC master..sp_executesql @sqlIF @@ERROR <> 0 BEGIN SELECT @error = @error + 'Failure on database backup.' GOTO ERROR_HANDLE END --Record the database backup ended. SELECT 'Database Backup Ended::' + CONVERT(VARCHAR,GETDATE(),22)SELECT @min = @min + 1END--Record the time procedure ended.SELECT 'Backup Process Ended::' + CONVERT(VARCHAR,GETDATE(),22)RETURN 0--If an error has occurred RAISERROR, and exit out of the procedure with failure.ERROR_HANDLE:SELECT @error = @error + CONVERT(VARCHAR,GETDATE(),22)RAISERROR(@error,16,1)RETURN 1 MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
keithc1
Yak Posting Veteran
88 Posts |
Posted - 2004-07-06 : 01:00:57
|
Very well commented, nice workKeithc MCSE MCSA |
 |
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2004-07-06 : 12:20:20
|
I tried to use derrickleggett code above but when I pasted it and put it into Query Analyzer I got a error saying..Server: Msg 208, Level 16, State 6, Procedure sa_BackupWebDatabases, Line 175Invalid object name 'sa_BackupWebDatabases'.Quality Web Hosting & Designhttp://www.eznetideas.com |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2004-07-06 : 18:35:23
|
Will this replace the old backups? I would really like a way to keep like the last 4 days of backups if at all possibleQuality Web Hosting & Designhttp://www.eznetideas.com |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-07-06 : 18:39:31
|
WITH INIT is what overwrites it if it exists.And yes mine does this and plus keeps 2 days of backups. Here's an updated version that allows you to input how many days you want to keep:CREATE PROC isp_Backup(@Path VARCHAR(100), @dbType VARCHAR(6), @Retention INT = 2)ASSET NOCOUNT ONDECLARE @Now CHAR(14) -- current date in the form of yyyymmddhhmmssDECLARE @DBName SYSNAME -- stores the database name that is currently being processedDECLARE @cmd SYSNAME -- stores the dynamically created DOS commandDECLARE @Result INT -- stores the result of the dir DOS commandDECLARE @RowCnt INT -- stores @@ROWCOUNTDECLARE @filename VARCHAR(200) -- stores the path and file name of the BAK fileCREATE TABLE #WhichDatabase( dbName SYSNAME NOT NULL)-- Get the list of the databases to be backed upIF @dbType = 'All' INSERT INTO #WhichDatabase (dbName) SELECT [name] FROM master.dbo.sysdatabases WHERE [name] <> 'tempdb' ORDER BY [name]ELSEBEGIN IF @dbType = 'System' INSERT INTO #WhichDatabase (dbName) SELECT [name] FROM master.dbo.sysdatabases WHERE [name] IN ('master', 'model', 'msdb') ORDER BY [name] ELSE BEGIN IF @dbType = 'User' INSERT INTO #WhichDatabase (dbName) SELECT [name] FROM master.dbo.sysdatabases WHERE [name] NOT IN ('master', 'model', 'msdb', 'tempdb') ORDER BY [name] ELSE BEGIN DROP TABLE #WhichDatabase RETURN -1 END ENDEND-- Get the database to be backed upSELECT TOP 1 @DBName = dbNameFROM #WhichDatabaseSET @RowCnt = @@ROWCOUNT-- Iterate throught the temp table until no more databases need to be backed upWHILE @RowCnt <> 0BEGIN -- Get the current date using style 120, remove all dashes, spaces, and colons SELECT @Now = REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(50), GETDATE(), 120), '-', ''), ' ', ''), ':', '') -- Build the .BAK path and file name SELECT @filename = @Path + @DBName + '\' + @DBName + '_' + @Now + '.BAK' -- Build the dir command that will check to see if the directory exists SELECT @cmd = 'dir ' + @Path + @DBName -- Run the dir command, put output of xp_cmdshell into @result EXEC @result = master.dbo.xp_cmdshell @cmd, NO_OUTPUT -- If the directory does not exist, we must create it IF @result <> 0 BEGIN -- Build the mkdir command SELECT @cmd = 'mkdir ' + @Path + @DBName -- Create the directory EXEC master.dbo.xp_cmdshell @cmd, NO_OUTPUT END -- The directory exists, so let's delete files older than two days ELSE BEGIN -- Stores the name of the file to be deleted DECLARE @WhichFile VARCHAR(1000) CREATE TABLE #DeleteOldFiles ( DirInfo VARCHAR(7000) ) -- Build the command that will list out all of the files in a directory SELECT @cmd = 'dir ' + @Path + @DBName + ' /OD' -- Run the dir command and put the results into a temp table INSERT INTO #DeleteOldFiles EXEC master.dbo.xp_cmdshell @cmd -- Delete all rows from the temp table except the ones that correspond to the files to be deleted DELETE FROM #DeleteOldFiles WHERE ISDATE(SUBSTRING(DirInfo, 1, 10)) = 0 OR DirInfo LIKE '%<DIR>%' OR SUBSTRING(DirInfo, 1, 10) >= GETDATE() - @Retention -- Get the file name portion of the row that corresponds to the file to be deleted SELECT TOP 1 @WhichFile = SUBSTRING(DirInfo, LEN(DirInfo) - PATINDEX('% %', REVERSE(DirInfo)) + 2, LEN(DirInfo)) FROM #DeleteOldFiles SET @RowCnt = @@ROWCOUNT -- Interate through the temp table until there are no more files to delete WHILE @RowCnt <> 0 BEGIN -- Build the del command SELECT @cmd = 'del ' + @Path + + @DBName + '\' + @WhichFile + ' /Q /F' -- Delete the file EXEC master.dbo.xp_cmdshell @cmd, NO_OUTPUT -- To move to the next file, the current file name needs to be deleted from the temp table DELETE FROM #DeleteOldFiles WHERE SUBSTRING(DirInfo, LEN(DirInfo) - PATINDEX('% %', REVERSE(DirInfo)) + 2, LEN(DirInfo)) = @WhichFile -- Get the file name portion of the row that corresponds to the file to be deleted SELECT TOP 1 @WhichFile = SUBSTRING(DirInfo, LEN(DirInfo) - PATINDEX('% %', REVERSE(DirInfo)) + 2, LEN(DirInfo)) FROM #DeleteOldFiles SET @RowCnt = @@ROWCOUNT END DROP TABLE #DeleteOldFiles END -- Backup the database BACKUP DATABASE @DBName TO DISK = @filename WITH INIT -- To move onto the next database, the current database name needs to be deleted from the temp table DELETE FROM #WhichDatabase WHERE dbName = @DBName -- Get the database to be backed up SELECT TOP 1 @DBName = dbName FROM #WhichDatabase SET @RowCnt = @@ROWCOUNT -- Let the system rest for 5 seconds before starting on the next backup WAITFOR DELAY '00:00:05'ENDDROP TABLE #WhichDatabaseSET NOCOUNT OFFRETURN 0GO Set @Retention to 4 when you execute it. The default is 2.Sample execute:EXEC isp_Backup @Path = 'E:\MSSQL\Backup\', @dbType = 'All', @Retention = 4Tara |
 |
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2004-07-07 : 01:55:56
|
Thanks Tara I will have to try this later. One question for yall though. The backup process is running and it looks like it has spiked the CPU usage up to 100% while the backup is running. Is there anyway this could be limited or just try to run the backup when everyone is off the server?Quality Web Hosting & Designhttp://www.eznetideas.com |
 |
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2004-07-07 : 02:26:48
|
Well after playing around some more on the server I found out that it was not backing up to the external server name given.... \\server\c$\sql_databases\ and was just sitting at 98% CPU usage I am guessing trying to connect. It was running for 1 hour and never even created a folder. As soon as I had it run the job and save it to the local server it finished in a matter of a few minutes. I can not find any setting on what user to run the job as. What user does this use to try and save the data on the other server, and is there a way I can specify what user to use?[edit]Sorry maybe I should say what NT user since the other server does not have SQL installed.[edit]Quality Web Hosting & Designhttp://www.eznetideas.com |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-07-07 : 12:06:52
|
It is highly recommended that you backup databases to the local disk then copy the files to the remote server. If the backup is in a job, it uses the SQLSERVERAGENT service account. This might be set to Local System Account, thus your problem. You need to use a domain account that has local admin privs on the database server and enough permissions on the remote server as well. You should also change the MSSQLSERVER service account so that it uses the same account.Tara |
 |
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2004-07-07 : 12:43:33
|
Ok so I will back it up to the local server first. Then I need to write a batch file to copy the data over to a remote location and schedule it as a job on the 2k server?Quality Web Hosting & Designhttp://www.eznetideas.com |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-07-07 : 12:47:56
|
I would add a job step to the backup job. The second job step would perform an xcopy using cmdexec as the job step type. Or you could do this as its own job on the database server. You could alternatively use this code that I wrote:http://weblogs.sqlteam.com/tarad/archive/2004/04/02/1192.aspxTara |
 |
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-07-07 : 13:35:58
|
Please don't use xcopy. Please....pretty please. Robocopy is F-R-E-E. It's also about 500 times faster and more stable.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2004-07-07 : 13:47:07
|
Quality Web Hosting & Designhttp://www.eznetideas.com |
 |
|
Kristen
Test
22859 Posts |
Posted - 2004-07-07 : 14:00:58
|
Unless its xXcopy :-)Kristen |
 |
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2004-07-09 : 20:18:49
|
Can someone help me write a script in SQL or in 2k to help me copy any of the new files over to a different server?Quality Web Hosting & Designhttp://www.eznetideas.com |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2004-07-09 : 20:34:20
|
You just got a link for everything dont you When I use your script here is what the print out of the SQL is...xcopy C:\SQL_BackupsActiveKB\ActiveKB_20040709191606.BAK \\biblesoft01\C$\SQL_Backupsxcopy C:\SQL_BackupsBiblesoftSQL\BiblesoftSQL_20040709191612.BAK \\biblesoft01\C$\SQL_Backupsxcopy C:\SQL_Backupsbrad\brad_20040709191620.BAK \\biblesoft01\C$\SQL_Backupsxcopy C:\SQL_Backups\eShopOnline\eShopOnline_20040709192840.BAK \\biblesoft01\C$\SQL_Backupsxcopy C:\SQL_Backups\phpBBForums\phpBBForums_20040709192929.BAK \\biblesoft01\C$\SQL_BackupsThe folder where everything is located in is C:\SQL_Backups\ and then the folder name of the DB then the file. The last two xcopy commands are correct, but the first ones do not have a \ after the root folder. Any idea why?Quality Web Hosting & Designhttp://www.eznetideas.com |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-07-09 : 22:48:42
|
The problem is in the system tables that store backup information. I'm pulling the file name directly from there. For some of my other weblogs, I am building the path information, but not for this one. So you must have these entries (C:\SQL_BackupsDBName) in the backup system tables. Run this to see:SELECT bs.database_name AS DatabaseName, MAX(bms.physical_device_name) AS FullBackupNameFROM msdb.dbo.backupset bsINNER JOIN msdb.dbo.backupmediafamily bms ON bs.media_set_id = bms.media_set_idINNER JOIN master.dbo.sysdatabases s ON bs.database_name = s.nameWHERE CONVERT(VARCHAR(20), bs.backup_finish_date, 101) = CONVERT(VARCHAR(20), GETDATE(), 101) AND s.name NOT IN ('master', 'msdb', 'model', 'pubs', 'Northwind') AND bs.type = 'D'GROUP BY bs.database_name So if you look at the result set, you'll notice a FullBackupName column name. Does your data show what you are seeing from the PRINT? If it does, are you sure that C:\SQL_BackupsActiveKB doesn't really exist? I don't believe that you would have that entry unless a successful backup ran and was put into that directory.Let me know your findings.Tara |
 |
|
Next Page
|