Quantcast
Channel: Rod Cerrada - SQL Server/T-SQL
Viewing all articles
Browse latest Browse all 7

Creating Scheduled Backup in SQL Server Express

0
0

SQL Server experess does not includes SQL Server Agent that manages SQL jobs. In this case we could not create job that will manage the schedule of backup. We can create backup manually by using SQL Server Express Management Studio backup task or by creating a script that you can run in management studio.

With the absence of SQL Server Agent we can use Windows built-in Scheduled Tasks in scheduling backup and combination of batch script and osql command line to run the backup. Follow the steps below to create scheduled backup for SQL express.

Creating the SQL Backup Script and Batch File

  1. Open SQL Server Express Management Studio or any text editor.
  2. Create a backup script using the edtior. You can copy the script below and change the database name and path. Then test the script by running the it in SQL Server Express Management Studio.
    DECLARE @Database NVARCHAR(64)
    DECLARE @Path NVARCHAR(512)
    DECLARE @DateTime  datetime
    
    SET @DateTime = getdate()
    
    -- Enter Database name here
    Set @Database = 'RodBlog2'
    
    -- Enter Destination/filename of the backup
    SET @Path = 'D:\Project Backup\' + @Database + '\'
      + @Database + '-' 
      + convert(varchar, @DateTime, 105) + '.bak'
    
    BACKUP DATABASE @Database 
      TO  DISK = @Path WITH NOFORMAT, INIT,  
      NAME = N'Database Backup', 
      SKIP, NOREWIND, NOUNLOAD,  STATS = 10, CHECKSUM
    
    DECLARE @backupSetId AS INT
    
    SELECT @backupSetId = position 
      FROM msdb..backupset 
      WHERE database_name=@Database 
        AND backup_set_id=
        (
           SELECT MAX(backup_set_id) 
             FROM msdb..backupset 
             WHERE database_name=@Database 
        )
    
    IF @backupSetId IS NULL 
    BEGIN 
      RAISERROR(N'Verify failed. Backup information not found.', 16, 1) 
    END
    
    RESTORE VERIFYONLY 
      FROM  DISK = @Path 
      WITH  FILE = @backupSetId,  NOUNLOAD,  NOREWIND
    
    GO
  3. Save the script in SQL file (ex: Backup.sql).
  4. After creating the SQL script, we need to create a batch file that will run the script using osql commandline tool.
  5. Open a text editor and enter the batch script to run the backup.sql using osql.
    @ECHO OFF
    PATH C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727;C:\Program Files\Microsoft Visual Studio 8\VC\bin;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE;C:\Program Files\Microsoft Visual Studio 8\VC\vcpackages;
    PATH %PATH% C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Intel\Wireless\Bin\;C:\Program Files\ThinkPad\ConnectUtilities;C:\Program Files\Common Files\Lenovo;C:\Program Files\Intel\Wireless\Bin\;C:\Program Files\Intel\Wireless\Bin\;C:\Program Files\Microsoft SQL Server\80\Tools\Binn\;C:\Program Files\Microsoft SQL Server\90\DTS\Binn\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\;C:\Program Files\MySQL\MySQL Server 4.1\bin;C:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files\Microsoft Visual Studio\Common\Tools;C:\Program Files\Microsoft Visual Studio\VC98\bin
    REM
    ECHO.
    ECHO -------------------------------------------
    ECHO            Backup in Progress...
    ECHO -------------------------------------------
    osql -E -S localhost\SQLExpress2008 -i "Backup.sql" -o backup.sql.txt -n
    ECHO.
    ECHO.
    ECHO ------------------------------------------------------------------
    ECHO.
    ECHO    Please check backup.sql.txt if it is successfull.
    ECHO ------------------------------------------------------------------
    ECHO.
  6. Save the batch script in BackupDB.bat file. Note the osql command. Its the command responsible running the Backup.sql we created in step 4. To test the bach script, double click the BackupDB.bat file. After successfully running the batch script a backup file should appear in the path you defined in Backup.sql (Step 2).

Creating Scheduled Tasks

  1. Go to Start -> Programs -> Accessories -> System Tools -> Schedule Tasks to create tasks.
  2. On the Schedule Task window, double click the Add Scheduled Task icon. This will launch Scheduled Task Wizard to guide you through in creating a scheduled task.
  3. When ask for the executable to run, map it to BackupDB.bat file.

Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images