This article mainly for SQL server learners, who is going to become a DBA, in this article I am going to explain the backup and restore using T-SQL.
Why I choose the T-SQL instead of GUI?
Below are reasons,
I have seen a number of articles and blogs shows the GUI.
Anyone can use the GUI to restore the database, if they don’t well in SQL.
Again most of the DBAs hasn’t used the command and they don’t know the T-SQL syntax to restore the database. I recommend using the T-SQL to restore the database because it’s the greatest tool when you are migrating a huge number of databases (Not to right click each database to restore) and time saver.
I enjoyed when I read BrentOzar’s backup best practice articles. I have recommended my readers too.
Let’s see the funny story
You have some problem with your mobile phone. You lost all your contact numbers even your girlfriend (wife) number too. Suddenly, she called to you then you asked who are you? …
So, keep doing the backup regularly otherwise ready to update your resume –
Before going to restore section keep verify your backups regularly (sometimes your backup might be damaged)
Important: Backup is the free D/R solution, when you lost your database.
Step by step restore: Backup has been taken in D drive.
USE MASTER GO BACKUP DATABASE DBA_TEST TO DISK='D:\BACKUP\DBA_TEST.BAK' with copy_only
Filelistonly returns the logical file names, physical files of data, log locations & file sizes.
USE MASTER GO RESTORE FILELISTONLY FROM DISK = 'BACKUP LOCATION' RESTORE FILELISTONLY FROM DISK ='D:\BACKUP\DBA_TEST.BAK'
Create the same copy\content of the database DBA_new from DBA_test. Use XP_fixeddrives to find the drive free space.
EXEC XP_FIXEDDRIVES SELECT * FROM SYS.MASTER_FILES
“sys.master_files” will show the current database files location. All the companies have certain rules like, data file will go one separate physical LUN (RAID level) and the log file will go another physical LUN (RAID level). It will help to improve the performance.
So, before going to restore checks and ask the same.
Restore database <database name> from disk =’backup location’
Move ‘logical name of data file’ to ’data file location’,
Move ‘logical name of log file’ to ’log file location’
RESTORE DATABASE DBA_NEW FROM DISK ='D:\BACKUP\DBA_TEST.BAK' WITH MOVE 'DBA' TO 'D:\DATA\DBA.MDF', MOVE 'DBA_LOG' TO 'E:\LOG\DBA_LOG.LDF'
RESTORE DATABASE DBA_TEST FROM DISK ='D:\BACKUP\DBA_TEST.BAK' WITH REPLACE
SELECT * FROM SYS.MASTER_FILES ALTER DATABASE DBA_NEW MODIFY FILE (NAME = DBA,NEWNAME = DBA_NEW ) ALTER DATABASE DBA_NEW MODIFY FILE (NAME = DBA_LOG,NEWNAME = DBA_NEW_LOG )
The database restoration has done in different machine then, transfers the logins and the passwords by using this Microsoft script.