Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Script to copy new backups to remote server?

Author  Topic 

aiken
Aged Yak Warrior

525 Posts

Posted - 2004-04-02 : 16:50:12
Hiya, everyone. I'm in need of a script (probably WSH, but whatever) to copy recent backups to a remote server. It's really a piece of cake, probably using xcopy /M, but I just figured I'd ask around before reinventing the wheel. Anyone doing something like that with a script that they'd care to share?

Cheers
-b

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-04-02 : 16:56:02
I have a stored procedure that will take the most recent backup file from a server and restore it onto another server. With slight modifications, I have this:



SET NOCOUNT ON

DECLARE @SQL VARCHAR(7000)
DECLARE @DBName SYSNAME
DECLARE @BkpFileName NVARCHAR(260)

SELECT bs.database_name AS DatabaseName, MAX(bms.physical_device_name) AS FullBackupName
INTO #Backups
FROM msdb.dbo.backupset bs
INNER JOIN
msdb.dbo.backupmediafamily bms
ON bs.media_set_id = bms.media_set_id
INNER JOIN
master.dbo.sysdatabases s
ON bs.database_name = s.name
WHERE CONVERT(VARCHAR(20), bs.backup_finish_date, 101) = CONVERT(VARCHAR(20), GETDATE(), 101) AND
s.name NOT IN ('master', 'msdb', 'model', 'pubs', 'Northwind')
GROUP BY
bs.database_name

WHILE (SELECT COUNT(*) FROM #Backups) <> 0
BEGIN

SELECT TOP 1 @DBName = DatabaseName, @BkpFileName = FullBackupName
FROM #Backups
ORDER BY DatabaseName

SELECT @SQL = 'xcopy ' + @BkpFileName + ' \\SomeServer\Someshare\'

EXEC master.dbo.xp_cmdshell @SQL

DELETE FROM #Backups
WHERE DatabaseName = @DBName

END

DROP TABLE #Backups



So it actually finds out what the most recent backup files are for the user databases, then copies them over. So in case someone ran a manual backup to a non-standard location, this code will catch those too.

Tara
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2004-04-02 : 17:45:25
http://weblogs.sqlteam.com/tarad/archive/2004/04/02/1192.aspx

Tara
Go to Top of Page
   

- Advertisement -