Why TO DISK = N’NUL’?
I don't understand why you are using TO DISK = N’NUL’:
BACKUP
DATABASE [test0916aj8CJ] TO DISK = N’NUL’
If you do that, the backup is saved to NUL, (ie. = to nowhere/nothing) and cannot be used because its file does not exist.
While NUL can also be used as a destination for LOG Backups, it should not be used either, especially on Prod servers because LOGs will be lost and the Backup chain will be broken. (~ similar to a SHRINKFILE)
LOG Backup
Before adding a DB to the group, you must prepare it.
When you want to prepare a secondary DB, at least 1 Transaction Log backup must be taken and restored. The mirror uses it to figure out which transactions has already been sync on the secondary DB and which transactions are not yet in sync with the primary DB.
Therefore you must Backup the Transaction Logs on the primary DB:
BACKUP LOG [test0916aj8CJ] TO DISK = N'....bak'
WITH COPY_ONLY, FORMAT, INIT, NAME = N'test0916aj8CJ-Transaction Log Backup', STATS = 10
The COPY_ONLY option must be used. It makes sure that the Logs are not truncated at the end of the LOG backup.
Primary DB backup chain
However, you cannot restore a log backup alone, meaning without a backup chain (see Kin answer too).
This means that the Transaction Log backup must be taken after a FULL Database Backup (+ an optional Differential if needed) has been taken.
Since the COPY_ONLY option does not break the backup chain, it also does not create a backup chain. The COPY_ONLY option cannot be used for the Database Backup.
Backups in order:
- FULL Database backup without the
COPY_ONLY option
- Optional Differential backup
- 1 LOG Backup with
COPY_ONLY option
- another (or more) LOG backup if necessary...
Restore the secondary DB
Then the Database Backup must be restored (+ Differential) on the secondary.
It must be restored with the NORECOVERY option because you also want to restore the LOG backup(s) once the FULL Backup has been restored.
Finaly you will restore the LOG Backup. You still need to use the NORECOVERY option because the mirror will keep restoring transactions once in place.
- Restore the FULL backup with the
NORECOVERY option
- Restore the DIFF backup with the
NORECOVERY option
- Restore all LOG backups in order with the
NORECOVERY option
Lets put it all together (adapt it to your env)
On the Primary server run:
USE master
Go
BACKUP DATABASE [test0916aj8CJ] TO DISK = N'....bak'
WITH FORMAT, INIT, NAME = N'test0916aj8CJ-Full Database Backup', STATS = 10
GO
BACKUP LOG [test0916aj8CJ] TO DISK = N'....bak'
WITH COPY_ONLY, FORMAT, INIT, NAME = N'test0916aj8CJ-Transaction Log Backup', STATS = 10
GO
On the Secondary server run:
USE master
Go
RESTORE DATABASE [test0916aj8CJ] FROM DISK = N'....bak'
WITH FILE = 1, NORECOVERY, NOUNLOAD, REPLACE, STATS = 10
GO
RESTORE LOG [test0916aj8CJ] FROM DISK = N'....bak'
WITH FILE = 1, NORECOVERY, NOUNLOAD, STATS = 10
You can then proceed with adding the new secondary DB to the availibility group...
Optional actions
- It is better to set the DISK option to a shared folder that is available from both the Primary and Secondary servers.
- It is also better to store DB files on similar disk and location on both the Primary and Secondary servers.