An SQL database Administrator has many job responsibilities related to the health of the SQL database. The SQL database’s data should remain accessible, quick, and safe. To maintain the properties of the database, the Administrator should be in regular practice to takes the backup of database files in a separate BAK file. It reduces the size of the database and makes it quicker to respond to the queries of users. Later, when the user requires any information from the backup file, then the administrator can restore the BAK file back to the SQL Server.
While taking a backup, there is no need to save the backup file at the same database, but you can use various methods for a flexible restore process. The first method is available within the SQL Server Management Studio software and the second method includes the process where administrator is required to run the Transaction-SQL commands for database restoration.
In both methods, the Administrator will get the option to rename the backup database and save it at the appropriate place.
Note: To restore a database under the full recovery model, administrator is required to backup the active transaction log too and for encrypted databases restore, the access permission for certificate or asymmetric key is must.
Restore the database using SQL Server Management Studio
- Start SQL Server Management Studio, and connect with the SQL Server instance with proper authentication.
- In the Object Explorer window, go to the respective database, right-click it and choose the option Restore database.
- In the Restore Database window, you will see two options to select the source – Database and Device.
- Database. If you choose the first option of the database, then it will show the list of all the databases that were backed up by the msdb backup technique.
- Device. When you choose the Device option, then, click the Browse button to select the BAK file from its saving path location.
- After selecting the backup database, go to the Destination section and rename the database with a new name that you want to assign to the restored database. You can also change the log file names along with.
- Change the directory name of SQL Server folder to save the database file on it. It is recommended to avoid new database saving with the existing database folder for unnecessary confusions.
- In the ‘Restore To’ option, choose the option ‘The last backup taken.’ You can also click the Timeline button and there will be a backup timeline of all the backups of the same database taken at different times. You can select anyone.
- At the ‘Backup sets to restore grid’, choose the available backup files for the given location. Click OK.
- Under the Restore options, there are three options to choose from –
- Overwrite the existing database.
- Preserve the replication settings.
- Restrict access to the restored database.
Select the first option “Overwrite the existing database” to continue restoring.
- In the Recovery State drop-down, select ‘restore with recovery.’
- In the Tail-Log backup, check the option ‘leave source database in the restoring state’.
- Click OK and let the restore process begin.
- After the completion of the restore process, you can get the message that the Database is restored successfully.
- Find the database with the similar name under the Databases section.
Limitations of the method
- The user who is restoring the database should be the only user who is accessing the database.
- For the full recovery of the database, you should also restore the active transaction logs.
- For the restoration of an encrypted database, the user must have the asymmetric or the certificate to encrypt the database.
T-SQL command to restore a database
It is relatively easier to restore the database using the T-SQL command with a new name as you can complete the job with a single command only.
Before using this command, you need to have the physical locations of the backup database file and the log file along with their logical names.
Here is the command;
MOVE ‘JacobWork2016_Data’ TO ‘D:\Jacob\JacobWork_Data.mdf’,
MOVE ‘JacobWork2016_Log’ TO ‘E:\Jacob\log\JacobWork_Log.ldf’;
The command will restore the new database JacobWork from the JacobWork2016 backup along with the JacobWork_Data and JacobWork_Log files.
Limitations of the method
- SQL permissions to run T-SQL command is not assigned
- Not enough space available to restore database
- Invalid file format, source file path
- Technical skills are required
- Incorrect database file names
- Chances of Database inconsistency errors
The methods to restore the SQL database with a different name are quick using the SQL Server Management Studio and the T-SQL Commands but there are many prerequisites and settings that the user needs to fulfill. A single wrong step can not only stop the restoration process but also corrupt the database or the backup files also.
You can face several errors during the migration like the invalid source file path, invalid file format, incorrect names to the MDF and LDF files, not enough space for the restored database, or insufficient permissions to access or run the T-SQL command. One common error for the failed restoration of the SQL Server database is.
Exclusive access could not be obtained because the database is in use.”
To overcome any crucial situation or save the backup data during the restoration, you should use any professional SQL Server recovery software that will take care of all the process and give you the desired results while securing your data.