Introduction to SQL Backup
SQL Backup helps us to improve the availability and service of the SQL database continuously to the users. If you are using the SQL database in the production environment for your clients then you need to make sure that your database is always working fine and is available 24 * 7. For the availability of the database, you must often keep the backup of your client’s database. In case if the database is corrupted or is crashed or lost then you should be able to restore the data in the database.
For this reason, SQL provides us with a facility to back up the database using SQL backup utility. This utility can only be used if your database is accessible and the select privilege on the tables of that database is assigned to you and the database is running. This utility creates a logical backup containing the SQL statements that can be run again to bring back the database to the state when this file was created. This utility can be used for single or multiple database backup.
You must backup your database frequently to have the updated backup of your database available with you. Whenever the backup is restored the database will be back to the state when that backup file was being created using SQL backup. In this article, we will learn about the types of backups available in SQL and also study some of them in detail.
Types of Backups in SQL
There are many types of backup available in SQL and all of them differ in strategy, content, scope, and methodology of backup and restore process.
The types of the backups in SQL are specified below out of them some are commonly used.
- Full backup
- Transaction log
- Differential backup
- Tail Log backup
- File backups
- Copy-only backup
- Partial backups
1. Full backup
In this type of backup strategy, all the data of the database is backed up in the disk or memory location that we have specified in this query. As whole data from the database is being backed up the process of backup may take some time especially when there is huge data to be backed up. Also, the restoration process becomes time-consuming.
But once the backup is created using a full backup strategy, at the time of necessity the database can be restored to the state when it was being backed up. In SQL, we use the BACKUP DATABASE command to create a full backup of the database with which we also have to mention the name of the database that we wish to back up and the disk location where the backup needs to be created.
2. Differential backup: BACKUP DATABASE
Backing up the whole database every time can be very time consuming and unnecessary the old data is hardly been modified. In such cases, we can only backup the data that has recently been added and modified in the database from the time when the last backup was created. This saves a sufficient amount of resources and time that must have consumed and required for backing up the old that that has already been backup in the past. While performing differential backup only a small amount of data is being backup up between the time interval of last backup and current one provided if the number of transactions that are made in that time duration as considerably small.
In case if a huge amount of data is being generated each day then differential backups can also become very time-consuming. Also, note that the restoration process doesn’t get affected and still consumes the same time as consumed while restoring full backup as in both the case whole database is being restored. The command used to perform the differential backup in SQL is the same BACKUP DATABASE along with an extra clause of the specification that is DIFFERENTIAL along with which the name of database and disk location needs to be specified.
3. Transaction log Backup
When bulk-logged or full recovery models are being used in the SQL database then we can use the transactional log backups. In this type of backup, we can take the backup of all the logs of the transactions that have been made on the database so that the data can be recovered at any specific point of time where we want as all the history of the modifications on the database is stored in the logs of transactional log backup.
This type of backup consists of all the logs that have not been backed up in the previous transactional log backup. The nature of the backup of the transaction log is incremental. When an accident occurs that results in the data loss and you want to restore the database to the specific point of time that is just before the time of the accident then you will have to restore latest differential backup data and all the transaction logs whose modifications you wish to desire to be restored.
In SQL, to perform the transaction log we will have to use the BACKUP LOG command along with the TRANSACTION LOG clause in the query statement along with the name of the database and the device location where we want to backup the logs.
Examples to Implement SQL Backup
Below are the examples mentioned :
Example #1
Full backup:
Code:
BACKUP DATABASE [EDUCBA_SQL_DEMO_DB]
To DISK='f:\SQLBackup\EDUCBA_SQL_DEMO_DB.BAK'
WITH FORMAT,
MEDIANAME = 'EducbaSQLServerBackup',
NAME = 'FULL_EDUCBA_SQL_DEMO_DB_Backup';
Output:
Example #2
Differential backup –BACKUP DATABASE:
Code:
BACKUP DATABASE [EDUCBA_SQL_DEMO_DB]
To DISK='f:\SQLBackup\EDUCBA_SQL_DEMO_DB_Diff.BAK'
WITH DIFFERENTIAL,
MEDIANAME = 'EducbaSQLServerDiffBackup',
NAME = 'DIFFERENTIAL_EDUCBA_SQL_DEMO_DB_Backup';
Output:
Example #3
Transaction log Backup:
Code:
BACKUP LOG [EDUCBA_SQL_DEMO_DB]
To DISK='f:\SQLBackup\EDUCBA_SQL_DEMO_DB_Log.trn'
WITH
MEDIANAME = 'EducbaSQLServerLogBackup',
NAME = 'LOG_EDUCBA_SQL_DEMO_DB_Backup';
GO
Output:
Conclusion
We can use the backup and restore facility in SQL collectively to make the user available with data and service of the SQL database throughout the time as long as it is required. In case of accidents such as crashing of servers and corrupting of data from the databases the data that is backed up can be restored to bring back the SQL database to a state when the database was being backed up. We can use various ways for backing up the data and the type we choose for backup depends upon the requirement and amount of data as well as strategy we want to back up the data in SQL.
Recommended Articles
We hope that this EDUCBA information on “SQL Backup” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
10 Online Courses | 8 Hands-on Projects | 80+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses