How to backup and restore SQL Server on Linux
How does SQL Server create a backup?
You can create a SQL Server backup by running the T-SQL command
When creating a backup, you do not need to stop SQL Server or switch it to single-user mode, and the database continues to work as usual during this operation.
Tools
To create a SQL Server database backup, you need to execute an SQL command. There are two utilities that allow you to execute arbitrary SQL commands:
- sqlcmd – is a command-line utility. To run an arbitrary query, use the -Q, parameter, like this:
- Through SQL Server Management Studio (SSMS). You can install it on Windows and connect it to your SQL Server on Linux.
Privileges
SQL Server is running on behalf of the mssql user. The mssql user must have write permissions to the backup directory. For example, let’s create a directory for backups in the root directory:
and assign the mssql user as the owner of this directory:
It is also necessary for the database user to have permission to create a backup. The system administrator (sa) has this privilege by default. The process of assigning the necessary privileges to other users is described in this article.
Backup creation
To create a backup, run the
The
Restoring the same database
For SQL Server to read a backup, the mssql user must have read permissions for the directory where the backup is located. You can restore the backup with the
Please note that the
Creating a new database from backup
To create a new database from a backup command in the restore, you must specify the name of the new database and the path to the data files that will be created after the creation of a backup.
First, you need to understand what files are in the backup. You can do this by running the
It is better to run this command in SSMS, but if that is not possible, sqlcmd will also work. In the picture below, the logical names of the database files (test-database and test-database_log) and the physical paths to which these files will be restored by default are highlighted in red. If these files already belong to another database, the restore will fail.
In the restore command, for each file, you must map the path to a new data file (the file will be created automatically) using
The
Differential and Transaction Log backups
SQL Server allows you to make not only complete copies of the database but also incremental. Incremental backups are those that contain only what has changed since the last backup was created. SQL Server has two types of incremental backups:
A differential backup contains all data that has changed since the last full backup was created. To make a Differential backup, specify
To restore the database, you will need not only a differential backup, but also a preceding full backup:
Transaction log backup contains all data that has changed since any last backup, including another transaction log backup. Thus, Transaction Log backups can be combined into chains, which can be very long. But it takes the whole chain to recover it. Any chain of a transaction log backup starts with a full backup.
Now you can create transaction log backups
and another one; you can make many such backups:
You will need all the files that were created by the last full backup. The intermediate files must be restored with the
Transaction Logs can be created often, even every 15 minutes, and stored for a long time because they take up very little space. The disadvantage of incremental backups is that they are difficult to manage. If you make a mistake, such as losing one file, you lose the ability to recover the entire chain.
Backup automation
Backups should be created regularly and automatically. You can automate SQL Server backups in different ways. The following ways will be discussed below:
- Shell script and crontab
- SqlBak
- SQL Server Agent
Let’s take a closer look at them:
Shell script and crontab
Below is a script that does three simple things.
- Creates a compressed backup in the specified directory
- Removes backups that were created more than 30 days ago
- Sends a backup status notification to your email
You will need to install the utilities to send email notifications. You can do this with this command:
Now create a text file do-backup.sh and insert the following text. In the first lines of the script, specify the data to connect to the database:
You can schedule this script to run regularly with crontab. Enter:
and add the following line at the end:
backup creation will start at midnight.
SqlBak
SqlBak is a service for creating backups and sending them to the cloud. SqlBak can automatically make a backup of your SQL Server database, compress it, send it to any cloud storage, and notify you via email if something goes wrong. SqlBak greatly simplifies the management of incremental backups. You only need to set up regular backups once, and your backup is just a click away from restoring.
Below is a step-by-step instruction on how to backup and restore SQL Server databases via SqlBak.
Backup
- First of all, you need to run the SqlBak app on Linux and set a connection with sqlbak.com. The detailed instruction on how to it can be found on the “Download” page. If everything is set correctly your server will be displayed on the “Dashboard” page.
- Now it’s time to set a connection to your SQL Server. The connection can be added in the following way:
- Once the connection is set, click on the “Add new job” button and create a new backup job.
- At the opened window, please choose the databases you are going to backup
- The next step is to set the destinations where the backups will be stored, for example, a local folder and Google Drive
- Now you can set a schedule when a backup job should be run
- And the last setting from the minimum set is the notification setting
That’s all, to run the backups according to the schedule you need to save the settings by clicking the “Save & Exit” button at the top of the page.
Restore
The backups can be easily restored by clicking the “restore” icon on your backup job’s settings page. Just choose a backup you need to restore, click the “restore” icon and follow the instructions. If it is a transaction log or differential backup, all the needed backups will be restored automatically.
Also, the very last backup can be restored from your “Dashboard” page, just click the “restore” icon at the “LAST RUN” column.
SQL Server Agent
SQL Server Agent is a separate component of SQL Server that runs in a separate process independently of SQL Server and allows you to run maintenance tasks. This component is installed together with SQL Server.
By default, the SQL Server Agent is not running. To start it, run the following commands:
You can set up tasks in SQL Server agent by running SQL commands, but it is not very convenient. SQL Server Agent is configured through a set of stored procedures. The parameters of these procedures and the links between them are not human-friendly. Usually, these commands are executed in the backend, and the user configures what they need through SQL Server Management Studio. If you still want to configure the SQL server agent via stored procedures, see the official documentation.
SQL Server Management Studio can only be installed on Windows, but that’s okay because you can connect to SQL Server remotely through port 1433.
Find the SQL Server Agent section in Object Explorer, right-click on Jobs, and select New Job….
In the General section, enter the name of the job you want to add, for example, Daily Backup
In the Steps section, click New, and enter the following script:
It will create a backup in the directory specified at the beginning of the script and delete files that are older than 30 days. Please note that the second line has a path that starts with the windows drive label: ‘c:/backups/sql-server/’. It is correct. The path c:/ is equivalent to the root directory.
Under “New Job Schedule,” create a new schedule for daily backups.
After that, save your settings.
Now click on the job in the job list, and select Start job at Step. If everything is set up correctly, the following message will pop up:
And a new backup will appear in the catalog.
Bottom line
Backing up SQL Server on Linux is no more difficult than on Windows. The differences are in file path format and permissions.
Automated Differential and Transaction log backups can significantly increase the protection of the database by reducing the weight of backups and the speed of their creation. To avoid confusion, we suggest using the SqlBak service. It manages backups and allows you to immediately take a backup outside of the server where the SQL Server is installed, for example, to the cloud storage.
No matter how you make backups, remember the main thing: backups are created so that they can be restored. Any server can crash. So be sure to make sure your backups are working, ideally in a test environment.