Tag: Database Backups

  • How to Backup a SQL Server Database

    How to Backup a SQL Server Database

    This post contains a demo on how to backup a SQL Server Database and includes some information on the types of backups available in MS SQL.

    The 3 types of backups that will be covered in this post are:
    # MS SQL Full Backup
    # MS SQL Transaction Log Backup
    # MS SQL Differential Backup

    Before backing up a database, you should verify the disk the backup is going to has enough space to hold the backup. You may also want to check when your databases were last backed up, to ensure running a backup is necessary at this time, or for general review.

    There’s a lot to understand with backups in SQL Server. Always refer to MS Docs when performing a change to a Production environment. This is just demo & information sharing.

    MS SQL Full Backup

    A Full Database Backup is a backup that will contain the whole database, all of the data included until the point in time the backup was taken. This is our main Restore Point.

    By running the command below, we are able to run a Full Backup of the database, with compression.

    Amend the database name in [square-brackets] and location/name on disk in ‘quotes’ –

    -- Run full database backup with compression
    BACKUP DATABASE [demoBlog] TO DISK = N'd:\D:\mssql_backups\demoBlog_full_01082022_1300.bak' WITH COMPRESSION, STATS;
    SQL Server Database Backup

    Backing up a database can sometimes take a very long time. It depends on various factors why a backup takes longer, but one common factor is, that the bigger your database gets, the longer it will take to backup & restore.

    I’ve seen a Full Backup in a Production environment take more than a day to complete (multi-terabyte). This is why a script to check estimated backup & restore time comes in useful for a DBA.

    MS SQL Transaction Log Backup

    Transaction Log Backups in SQL Server help log & store transactions that are made in your database which can then be used to bring databases back online to a point in time, in a disaster scenario.

    When running the command below we want to have an associated Full Backup that chains to this Transaction Log Backup. That means, having a Full Backup you performed, and your LSN chain hasn’t been broken by a manual Full Backup without including the Copy_Only parameter.

    To run a Transaction Log Backup –

    -- Run tlog backup 
    -- (amend database name & location/file on disk)
    BACKUP LOG [demoBlog] TO DISK = N'd:\mssql_backups\demoBlog_tlog_21082022_2130.bak' WITH COMPRESSION, STATS;
    SQL Transaction Log Backup

    Using the above Transaction Log backup, we can restore data that happened after the last Full Backup up until the point in time this Transaction Log backup was initiated.

    The reasoning behind using Transaction Log backups generally comes down to your business’s desire to ensure High Availability in your SQL Server environment. If you don’t need a recent restore point and can live with (e.g.) daily Full Backups, Transaction Logs are not for you.

    MS SQL Differential Backup

    Differential Backups capture only the data that changes since the last Full Backup has run. A Full backup can be considered the base of a Diff Backup (except for Copy_Only backups).

    The Differential Backup is one I see rarely in Production environments. A major benefit of Diffs is that they are quick to run. The size of a Diff Backup depends on the volume of data that has changed since its last rebase (last Full Backup).

    In the example below I’m running a simple Differential Backup of my demoBlog Database –

    -- Run diff backup
    BACKUP DATABASE [demoBlog] TO DISK = N'd:\mssql_backups\demoBlog_diff_21082022_2133.bak' WITH DIFFERENTIAL, COMPRESSION, STATS;
    SQL Differential Backup

    That’s us done, a quick review of the main types of database backup in SQL Server. For backup tips, check out the Database Backups tag.

  • Get Estimated Backup Restore Time in SQL Server

    Get Estimated Backup Restore Time in SQL Server

    This post contains a SQL script to get estimated completion times and process information for your currently running database backups & restores in SQL Server.

    Backing up a database can often take a very long time. It depends on various factors why a backup can take longer to complete. One more obvious reason is, that the bigger your database gets, the longer it will take to backup & restore.

    Other backup performance considerations include whether your server is busy with transactions/queries, or you may be running on old hardware, and I’ve seen having fragmented heaps cause extended backup runtimes too.

    Get Estimated Database Backup or Restore Time MSSQL

    When you’re waiting on a database backup or restore to complete in SQL Server, we can run the script below to get an estimated time of how long the backup has left to go –

    SQL Estimated Database Restore Time

    We have one 5GB database here currently being backed up at the time of executing the DMV script. The backup progress is at 27% and is due to complete 1 minute after the backup was initiated.

    This script will also be useful for getting the full command of the currently running SQL process on your SQL Server Instance, by removing the filter (WHERE Clause) on the query.

  • How to Get Last Backup Times for all Databases in SQL Server

    How to Get Last Backup Times for all Databases in SQL Server

    This is a post on how to get the last backup dates and times for all databases on a SQL Server Instance.

    Database backups are as important as the availability of your server. If your database gets corrupted, or there’s an accidental data deletion and you need to recover data, then we must rely on backups to get the data back. If you have no backups, you have no way to recover your lost data.

    This demo post includes the following SQL scripts to help gain database backup history information for review:
    # Get Most Recent Database Backup Info
    # Get All Database Backup Info

    If you discover a database without an associated backup you should consider reviewing the situation.


    Get Most Recent Database Backup Info

    The following script returns the most recent Full, Differential & TLog Backup times for all databases on a SQL Server Instance.

    Get Most Recent Last Backup SQL Server

    We can see from the above screenshot all databases on this SQL Server Instance have been backed up in the last 30 days. Only the demoBlog has had recent Differential and TLog Backups.

    The type column within the backupset table gives us a way of checking whether the backup was a Full, Differential or Transaction Log.


    Get All Database Backup Info

    This script is more useful if you are reviewing the history of database backups, ensuring they are being backed up on a regular schedule/interval.

    The following script will show all backups in the log, in the past 2 months.

    You can simply remove the WHERE Clause to bring back the full history if necessary.

    The above screenshot includes examples of Full, Differential & Transaction Log backups. This data can be very valuable for verifying backup chains are not broken, as well as backup sizes (including backup compression savings).

  • Get Last Database Restore DateTimes in SQL Server

    Get Last Database Restore DateTimes in SQL Server

    This post contains a script to help you get the last dates and times for when your databases were last restored in SQL Server.

    The script below queries the M.S.D.B database sys.databases and restorehistory tables. You should get into the habit of searching MS Docs for every system table you query in SQL Server. This gives you the reference point you need for documentation on columns and query examples.

    If your database was not created by a RESTORE procedure then the restore_date column value will be NULL. For example, if you’ve installed SQL Server and created a new database, then this script may not be of much use.

    database last restore time sql

    The above restore_type column shows ‘D’ to indicate a Full backup restore has happened. ‘L’ would indicate a transaction log restore, and ‘I’ is a differential restore. The column meanings can be found in the MS table docs as linked above in this post.

    If you’re interested in reading more about querying SQL Server currently running processes, have a look at my MSSQL SPIDs blog tag, or have a look at my MS SQL DBA Blog Posts for random tips from a DBA.

  • How to Restore a Database in SQL Server

    How to Restore a Database in SQL Server

    Restoring a database in SQL Server is a straightforward task, and one that SQL Database Administrators have to perform thousands of times throughout their career.

    A database restore can be done via GUI (SSMS Wizard) or via command (TSQL/PowerShell). The method you choose to restore a database is usually driven by the number of databases you have to restore. If you have to restore more than 5 databases, perhaps it’s time to automate. If you are an experienced engineer you might write the restore command as second nature, always having it as a preference rather than restoring via the SSMS GUI Wizard.

    One complexity when restoring databases is that your database backup media might be split into more than one file – this just means we need to add more paths during the restore.

    In the demo below I am running a Full Backup of a database, dropping it, and restoring it back online. Following this, I do the same again but split the backup media into more than one file.

    # Restore a Database via Command
    # Restore a Database with Multiple Files

    Restore a SQL Server Database via Command

    I have 2 user databases on my local MSSQL instance here : [ThePetShop] & [lemonadestand]

    To prep for this database restore demo, I’m going to run a Full Backup for the [lemonadestand] database to a local directory.

    ssms object explorer
    -- Perform full database backup to local temp directory with compression
    BACKUP DATABASE [lemonadestand] TO DISK = N'c:\temp\lemonadestand_full_11082022.bak' WITH COMPRESSION;

    Next, I’ll drop the database and refresh my SSMS Object Explorer.

    MSSQL Database Backup

    We no longer have the [lemonadestand] database available to us.

    To restore this database, back to the point in time I ran the backup command, we need to run the RESTORE DATABASE command. I run the command and refresh SSMS.

    -- Restore database ms sql
    RESTORE DATABASE [lemonadestand] FROM  DISK = N'C:\TEMP\lemonadestand_full_11082022.bak';
    MSSQL Database Restore

    The database is now back online and ready to use again.

    Restore a Database with Multiple Files

    This time we are going to perform another Full Backup of the [lemonadestand] database, but this time splitting the backup media across 2 files instead of 1.

    Backing up to multiple files is often used to improve performance for backups, and/or for managing available disk space on an MSSQL host.

    -- Run Full database backup to local temp directory with compression
    BACKUP DATABASE [lemonadestand] TO DISK = N'c:\temp\lemonadestand_full_11082022_part1.bak', DISK = N'c:\temp\lemonadestand_full_11082022_part2' WITH COMPRESSION;
    
    -- Drop the lemonadestand database
    DROP DATABASE [lemonadestand];
    
    -- Restore the lemonadestand database
    RESTORE DATABASE [lemonadestand] FROM  DISK = N'C:\TEMP\lemonadestand_full_11082022_part1.bak', DISK = N'c:\temp\lemonadestand_full_11082022_part2';
    MSSQL Database Restore

    That’s it for this one. Have a look at the Restoring Databases Tag for more tips on this area – I hope to touch on points in time recovery more.

  • Show Last Backup Times in SQL Server

    Show Last Backup Times in SQL Server

    If you’re running a SQL Server and you care about the data held in it, you should already be backing up the databases. But it’s always surprising the number of times you encounter business-critical databases not being backed up as a DBA!

    When given the opportunity to look at a new SQL Server instance one of the first scripts I would run is to check when the databases were last backed up. I’m likely being asked to look at this server because there’s an issue. Issues need fixing, and before any of my changes are executed I need a point-in-time revert back to in-case I make things worse.

    This blog post is to note a script that’ll return last Full, Differential, and Log backups for all databases on a SQL Server instance.


    Get Last Backup Times in SQL Server

    -- Show last backups on all databases
    SELECT 
      ISNULL(d.[name], bs.[database_name]) AS [Database], d.recovery_model_desc AS [Recovery Model], 
        MAX(CASE WHEN [type] = 'D' THEN bs.backup_finish_date ELSE NULL END) AS [Last Full Backup],
        MAX(CASE WHEN [type] = 'I' THEN bs.backup_finish_date ELSE NULL END) AS [Last Differential Backup],
        MAX(CASE WHEN [type] = 'L' THEN bs.backup_finish_date ELSE NULL END) AS [Last Log Backup]
    FROM 
      sys.databases AS d WITH (NOLOCK)
      LEFT OUTER JOIN msdb.dbo.backupset AS bs WITH (NOLOCK)
      ON bs.[database_name] = d.[name] 
      AND bs.backup_finish_date > GETDATE()- 30
    WHERE d.name <> N'tempdb'
    GROUP BY ISNULL(d.[name], bs.[database_name]), d.recovery_model_desc, d.log_reuse_wait_desc, d.[name] 
    ORDER BY d.recovery_model_desc, d.[name] OPTION (RECOMPILE);

    The above shows my simple wee test database ‘blah’ is the only database that has had a backup on this instance.


  • Backing up a SQL Server Database with Encryption

    Backing up a SQL Server Database with Encryption

    To backup a database in SQL Server with Encryption, run the BACKUP DATABASE command with the ENCRYPTION option added to the SQL statement, and specify the valid encryption certificate name.

    Backing up a SQL Server Database with Encryption is the secure way of performing your SQL Server backups. If your MSSQL database backups are off-site and/or you have sensitive customer information stored on them, then you may consider encrypting your database backups.

    Since SQL Server 2012, it has not been possible to create a SQL backup with a password (PASSWORD/MEIAMPASSWORD backup options).

    The demo below guide was done following Microsoft Docs – Create an Encrypted Backup.

    Backup Database with Encryption

    Create a Database Master Key (stored in the database).

    Create a Certificate to use for the database backups (gets stored in the Master Database) – Microsoft Docs; Create Certificate

    SQL Server Create Certificate

    Have a look at the new certificate by querying sys.certificates (think about expiry dates).

    SQL Server Select Sys.Certificates

    Now backup the database.

    -- backup database with encryption mssql
    BACKUP DATABASE [Computer_World] TO DISK = N'C:\Temp_MSSQL_Backups\computer_world_full.bak'  
    WITH COMPRESSION, ENCRYPTION (ALGORITHM = AES_256,SERVER CERTIFICATE = AT_Backup_Cert),STATS
    GO
    SQL Server Backup Database With Encryption

    The database has successfully been backed up with encryption.

    The warning we see is regarding the certificate created prior to the backup.

    Warning: The certificate used for encrypting the database encryption key has not been backed up. You should immediately back up the certificate and the private key associated with the certificate. If the certificate ever becomes unavailable or if you must restore or attach the database on another server, you must have backups of both the certificate and the private key or you will not be able to open the database.

    I have another post to guide through a certificate backup here.

  • Backing up a SQL Server Certificate

    Backing up a SQL Server Certificate

    It’s important to backup SQL Server Certificates, but only if you’re using them really.

    If for example, your SQL Server database backups are encrypted and you need to restore it on another server, then you will need both the certificate and private key that was used.

    This post is a guide on backing up a SQL Server certificate, ensuring we have a copy of the certificate and private keys.


    Backup a SQL Server Certificate

    Check your existing server certificates by querying sys.certificates, you’ll need the certificate name.

    Use Master  
    GO  
    	SELECT name, pvt_key_encryption_type_desc, subject, start_date, expiry_date, key_length
    	FROM sys.certificates
    GO
    SQL Server Select Sys.Certificates

    Backup certificate (Microsoft Docs – Backup Options) – amending file paths & password.

    BACKUP CERTIFICATE AT_Backup_Cert TO FILE ='C:\Temp_MSSQL_Backups\mssql_at_backup_cert.cer'  
          WITH PRIVATE KEY (   
    		FILE ='C:\Temp_MSSQL_Backups\mssql_at_backup_key.pvk',  
    		ENCRYPTION BY PASSWORD ='Str0ng3P4sSw0rd!' ) 

    Then, move those files into a safe space.


  • Backup & Restore (WITH FILE)

    Backup & Restore (WITH FILE)


    In this post I’m going to show what the WITH FILE = X option does when backing-up or restoring a SQL Server database.

    microsoft_documentation

    The first thing we need to know is that a backup file can contain multiple databases, and even multiples of the same database. If you run a quick backup command on a small database, then hit F5 again without amending the file path, you’ll end up with 2 database backups within the same media.

    When restoring from a backup we can specify which version of the database we want by using the WITH FILE option, with the help of the RESTORE HEADERONLY command.

    This post is a run-through of how the above works!


    Backup & Restore With File

    1. To get started, create a database & insert some test data.

    2. Backup the database.

    The backup file;

    3. Insert some more data into the test table and then backup the file to the very same location.

    The backup file;

    4. Drop the database, readying for a restore.

    5. Using RESTORE HEADERONLY, we can have a look into the backup media to find out more information. The Position column is what we need from this.

    6. Restore the first backup performed above WITH FILE = 1.

    7. Check the data.

    8. Restore the database in position number 2, with REPLACE to avoid the need of a drop.

    9. Verify again which backup it is.

    10. Finally, restore again without WITH FILE.

    We can see that the first backup taken has been restored as default.

    If you are backing up multiple different databases into the same media, the above won’t work. You’ll have to specify the position. You’d also probably want to give it a backup name using WITH NAME = ‘backupName.

    Hope this is useful stuff!


  • Backing Up a SQL Server Database

    Backing Up a SQL Server Database


    Backing up a database is something I’ve always found easier done within a query window. There’s not much to it;

    BACKUP DATABASE [DATABASENAME] 
    	TO DISK = N'e:\directory\databasename_backupdate.bak' 
    		WITH COMPRESSION, STATS

    That’s what I’m used to writing. I’ve no need to worry about the copy_only option these days, or check SQL Server Editions to see if I can use compression. Good times that was. Measuring available disk space against estimated backup sizes, and sometimes even adding temporary virtual disks to the server to accommodate the backup file size. The SQL statement for backups is simple… but there’s a lot to consider when performing a one. It just depends on the restrictions of the servers you are working with really.

    Below is a simple run-through for backing up a database using the Back Up Database Wizard. As always, have a look at the manual first.


    How to Backup a SQL Server Database

    1. Right click your database, select Tasks and click Back Up…

    SQL Server Backup SSMS

    2. In the General tab of the prompted window you’ll see it’s already set-up for a Full Backup on AdventureWorks which is using the Simple Recovery model.

    SQL Server Backup Wizard

    3. I’ll remove the destination file and move it somewhere else.

    SQL Server Backup Destination

    The directory has been picked up by my chosen Database Settings. Type the new backup file name here, remembering the .bak at the end.

    SQL Server Backup Destination

    4. Over to the Media Options tab, there’s a few options for consideration. Such as verifying the backup or performing checksums. You might think that verifying backups with automatic offsite restores works for you. Or, maybe you’re happy relying on the VM backups? As the importance of your database backups grows, the deeper it goes. I’m leaving those options unchecked as I don’t need them, and all other options are set as default.

    SQL Server Backup Media Options

    5. In the Backup Options tab, I’m leaving all options set to their defaults. No backup expiry or encryption, and I’m going to trust I’m using compression as default on my SQL Server.

    SQL Server Backup Options

    6. Click the Script button at the top of the window to see the generated SQL code behind this backup operation.

    SQL Server Backup Generate Script

    7. Some of the code contained within the statement is unnecessary, but it’s doing the same thing anyway. We can run this command or click OK on the Wizard.

    SQL Server Backup Script

    8. And when you’re done…

    SQL Server Backup Successful

    You have a backup of your database!