I was looking into ways to easily back up my local SQL Server database from the command line, and copy the resulting backup to my fileserver. That way I could kick off a backup manually or from a scheduled task. There are a few articles out there but they're more specific to SQL 2000 or earlier. Here's how to do it for SQL Server 2005.
I first made directory c:\sqlbackup and added a SQL script called backup.sql:
BACKUP DATABASE streamline TO DISK = 'c:\sqlbackup\streamline.bak' WITH INIT GO BACKUP LOG streamline TO DISK = 'c:\sqlbackup\streamline_log.bak' WITH INIT GO
The above commands simply create log & database backups of the "streamline" database. I used the INIT parameter so that the backup files only create one version of the database (otherwise the backup files will grow & grow, containing every version of the database, each time you run a BACKUP command).
Then I made a simple batch file called backup.bat:
@echo off REM get today's date and time as one big string for /f "tokens=2-4 delims=/ " %%i in ( 'date /t') do set theday=%%k%%i%%j for /f "tokens=1-2 delims=: " %%i in ( 'time /t') do set thetime=%%i%%j set now=%theday%%thetime% REM create backup files C: cd "\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn" sqlcmd -S localhost -U sa -P somepassword -i c:\sqlbackup\backup.sql -o c:\sqlbackup\log.txt REM copy backup files to backup device xcopy "C:\sqlbackup\*.*" \\10.1.10.50\ben_backup\sqlbackup\%now%\ /E /H /R
The first section uses some DOS batch trickery to create a variable called %now% that contains the current day and time in the form of yyyymmddhhmm. The second section runs the backup script & writes the output to c:\sqlbackup\log.txt. The last section copies the all the backup files to my fileserver, inside a date & time specific folder. Notice how I used the %now% variable in the xcopy destination parameter.
Now my backup folder contains timestamped folders containing everything I need to restore my "streamline" database. You can use a similar technique to make file backups.
Powered by: newtelligence dasBlog 2.0.7226.0
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Ben Strackany
E-mail