I have a ELMAH database that I want to script the restore of using the following:
RESTORE DATABASE [Elmah]
FROM DISK = N'E:\Elmah_backup_2012_11_02_030003_1700702.bak'
WITH FILE = 1,
MOVE N'Elmah' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Elmah.mdf',
MOVE N'Elmah_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Elmah.ldf',
NOUNLOAD, STATS = 10
GO
Even though I am not including WITH REPLACE each time I execute this statement it restores over the existing database.
I will always drop all databases before this operation and I never want this code to accidentally restore a database over one in production.
How do I change this code so that it will never overwrite an existing database?
I am actually doing this through SMO objects but the principle and results are the same so I am hoping to keep this simplified to just the TSQL necessary in the hopes that I can generalize that information to what needs to be set on the appropriate SMO.Restore object.
You need to (a) give the restored database a new logical name, and (b) you need to define new physical file names, so the existing ones won't be overwritten. Try something like this:
RESTORE DATABASE [Elmah_Restored] <== new (and unique) logical database name
FROM DISK = N'E:\Elmah_backup_2012_11_02_030003_1700702.bak'
WITH FILE = 1,
MOVE N'Elmah' TO
N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Elmah_restored.mdf', <== new (and unique) physical file name
MOVE N'Elmah_log' TO
N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Elmah_restored_log.ldf', <== new (and unique) physical file name
NOUNLOAD, STATS = 10
GO
Related
I have the current script to backup the 3 databases in use (All 3 DB are inter linked). What I would like to do is set the name to be used as a variable which is used for each db backup (rather than changing it 3 times before each execution. Any help would be greatly appreciated.
BACKUP DATABASE DB1
TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\DB1\TC1\STEP_10.bak' -- Amend the filename
WITH FORMAT,
MEDIANAME = 'C_Program Files_Microsoft SQL Server_MSSQL11.MSSQLSERVER_MSSQL_Backup',
NAME = 'DB1';
GO
USE GATEKEEPER_ELEC;
BACKUP DATABASE GATEKEEPER_ELEC
TO DISK = 'C:\Program Files\Microsoft SQL
Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\DB2\TC1\STEP_10.bak' -- Amend the filename
WITH FORMAT,
MEDIANAME = 'C_Program Files_Microsoft SQL Server_MSSQL11.MSSQLSERVER_MSSQL_Backup',
NAME = 'DB2';
GO
USE PFV;
BACKUP DATABASE PFV
TO DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\DB3\TC1\STEP_10.bak' -- Amend the filename
WITH FORMAT,
MEDIANAME = 'C_Program Files_Microsoft SQL Server_MSSQL11.MSSQLSERVER_MSSQL_Backup',
NAME = 'DB3';
I am trying to create a database with one .BKP file, but i don't have the .mdf & .ldf file, how do create a database without having these files.
RESTORE DATABASE MYDB
FROM DISK = 'C:\Backup Files-selected\MYDB_20170117\MYDB_20170117.bak'
WITH REPLACE,
MOVE 'test' TO 'C:\temp1\test2.mdf',
MOVE 'test_log' TO 'C:\temp1\test2.ldf';
Error: Msg 3234, Level 16, State 2, Line 6
Logical file 'test' is not part of database 'MESDB'. Use RESTORE FILELISTONLY to list the logical file names.
Msg 3013, Level 16, State 1, Line 6
RESTORE DATABASE is terminating abnormally.
Please let me know how to fix this.
In Sql Server Management Studio (SSMS), right click on Databases and select Restore Database...
Enter a Name for new DB.
in the Source select "From Device" and select your .bak.
Check the backup selected (restore column).
go to "Options" Page and select "Overwrite existing..." (first option, With replace).
Hope this help!
Try this using "RESTORE FILELISTONLY" command before the actual RESTORE command
RESTORE FILELISTONLY from disk = 'C:\Backup Files-selected\MYDB_20170117\MYDB_20170117.bak'
RESTORE DATABASE MYDB
FROM DISK = 'C:\Backup Files-selected\MYDB_20170117\MYDB_20170117.bak'
WITH
MOVE 'test' TO 'C:\temp1\test2.mdf',
MOVE 'test_log' TO 'C:\temp1\test2.ldf';
I have an empty database:
DB_Clients
And I want to restore the database from a .bak file:
OldDBClients.bak
This is the path:
C:\OldDBClients.bak
And this is my script:
USE [master]
GO
RESTORE DATABASE DB_Clients
FROM DISK = 'C:\OldDBClients.bak'
When I execute it, I get this error message:
Msg 3154, Level 16, State 4, Line 15
The backup set holds a backup of a database other than the existing 'DB_Clients' database.
Msg 3013, Level 16, State 1, Line 15
RESTORE DATABASE is terminating abnormally.
Can someone tell me why this happen?
I have to point that the file has the permissions to read and write.
Thank's.
You need to use WITH REPLACE option in order to overwrite the existing database.
RESTORE DATABASE DB_Clients
FROM DISK = 'C:\OldDBClients.bak'
WITH REPLACE
Probably you also need to specify WITH MOVE options; in this case:
use RESTORE FILELISTONLY FROM DISK = 'C:\OldDBClients.bak' to know logical name of your MDF/LDF
use WITH MOVE options in your RESTORE
For example:
RESTORE DATABASE DB_Clients
FROM DISK = 'C:\OldDBClients.bak'
WITH REPLACE,
MOVE 'YourMDFLogicalName' TO '<MDF file path>',
MOVE 'YourLDFLogicalName' TO '<LDF file path>'
Please note that you can also DROP your empty DB_Clients database and use a simple RESTORE.
You should this syntax:
USE [master]
GO
RESTORE DATABASE DB_Clients FROM DISK = 'C:\OldDBClients.bak' WITH
MOVE 'DB_Clients' TO 'D:\SQLServer\Data\DB_Clients.mdf',
MOVE 'DB_Clients_log' TO 'D:\SQLServer\Log\DB_Clients.ldf', REPLACE
It instructs SQL Server to overwrite the existing copy and specifies a valid location for your data and log files
Step 1:
Check the logical file names with the help of the following command:
RESTORE FILELISTONLY
FROM DISK = 'E:\DBBackups\mydb.bak'
Step 2: Use the logical names you get from the above query in the below query:
RESTORE DATABASE [mydb_new]
FILE = N'<MDFLogicalName>'
FROM DISK = N'E:\DBBackups\mydb.bak'
WITH
FILE = 1, NOUNLOAD, STATS = 10,
MOVE N'<MDFLogicalname>'
TO N'E:\DBBackups\mydb_new.mdf',
MOVE N'<LDFLogicalName>'
TO N'E:\DBBackups\mydb_new_0.ldf'
After running the above commands with the correct values you will see the output like this:
10 percent processed.
20 percent processed.
30 percent processed.
40 percent processed.
50 percent processed.
60 percent processed.
70 percent processed.
80 percent processed.
90 percent processed.
100 percent processed.
Processed 7672 pages for database 'mydb_new', file '<MDFLogicalname>' on file 1.
Processed 5 pages for database 'mydb_new', file '<LDFLogicalName>' on file 1.
RESTORE DATABASE ... FILE=<name> successfully processed 7677 pages in 0.780 seconds (76.893 MB/sec).
Completion time: 2019-10-20T11:35:31.8343787+05:30
I was trying to create a backup for my SQL Server Database using SQL Server Management Studio 2008 Express. I have created the backup but it is getting saved at some path which I am not able to find. I am saving it on my local HD and I checked in Program Files>Microsoft SQL Server>MSSQL 1.0>MSSQL>DATA> but its not there.
What's the default save path for this DB .bak?
Should be in
Program Files>Microsoft SQL Server>MSSQL 1.0>MSSQL>BACKUP>
In my case it is
C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup
If you use the gui or T-SQL you can specify where you want it
T-SQL example
BACKUP DATABASE [YourDB] TO DISK = N'SomePath\YourDB.bak'
WITH NOFORMAT, NOINIT, NAME = N'YourDB Full Database Backup',
SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
With T-SQL you can also get the location of the backup, see here Getting the physical device name and backup time for a SQL Server database
SELECT physical_device_name,
backup_start_date,
backup_finish_date,
backup_size/1024.0 AS BackupSizeKB
FROM msdb.dbo.backupset b
JOIN msdb.dbo.backupmediafamily m ON b.media_set_id = m.media_set_id
WHERE database_name = 'YourDB'
ORDER BY backup_finish_date DESC
If the backup wasn't created in the default location, you can use this T-SQL (run this in SSMS) to find the file path for the most recent backup for all DBs on your SQL Server instance:
SELECT DatabaseName = x.database_name,
LastBackupFileName = x.physical_device_name,
LastBackupDatetime = x.backup_start_date
FROM ( SELECT bs.database_name,
bs.backup_start_date,
bmf.physical_device_name,
Ordinal = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
FROM msdb.dbo.backupmediafamily bmf
JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
WHERE bs.[type] = 'D'
AND bs.is_copy_only = 0 ) x
WHERE x.Ordinal = 1
ORDER BY DatabaseName;
As said by Faiyaz, to get default backup location for the instance, you cannot get it into msdb, but you have to look into Registry. You can get it in T-SQL in using xp_instance_regread stored procedure like this:
EXEC master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQLServer',N'BackupDirectory'
The double backslash (\\) is because the spaces into that key name part (Microsoft SQL Server).
The "MSSQL12.MSSQLSERVER" part is for default instance name for SQL 2014. You have to adapt to put your own instance name (look into Registry).
Set registry item for your server instance. For example:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.2\MSSQLServer\BackupDirectory
Use the script below, and switch the DatabaseName with then name of the database that you've backed up. On the column physical_device_name, you'll have the full path of your backed-up database:
select a.backup_set_id, a.server_name, a.database_name, a.name, a.user_name, a.position, a.software_major_version, a.backup_start_date, backup_finish_date, a.backup_size, a.recovery_model, b.physical_device_name
from msdb.dbo.backupset a join msdb.dbo.backupmediafamily b
on a.media_set_id = b.media_set_id
where a.database_name = 'DatabaseName'
order by a.backup_finish_date desc
I dont think default backup location is stored within the SQL server itself.
The settings are stored in Registry. Look for "BackupDirectory" key and you'll find the default backup.
The "msdb.dbo.backupset" table consists of list of backups taken, if no backup is taken for a database, it won't show you anything
...\Program Files\Microsoft SQL Server\MSSQL 1.0\MSSQL\Backup
have you tried:
C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\Backup
Script to get all backups in the last week can be found at:
http://wraithnath.blogspot.com/2010/12/how-to-find-all-database-backups-in.html
I have plenty more backup SQL scripts there also at
http://wraithnath.blogspot.com/search/label/SQL
So eventually on SQL Server 2019 you can just call built in SERVERPROPERTY function to query the default backup folder:
select serverproperty('InstanceDefaultBackupPath')
You may want to take a look here, this tool saves a BAK file from a remote SQL Server to your local harddrive: FIDA BAK to local
I'm trying to do an Entity Framework walkthrough so I:
downloaded SQL script here: http://www.learnentityframework.com
in SQL Server Management Studio, I right-clicked Database, Create Database, named it
right-clicked on the new database, New Query
clicked on "Open File" and opened the script file: Create_ProgrammingEFDB1_SQLServer2008.sql
clicked "! Execute"
But the script (756K) has been running for 10 minutes now and still says "executing..."
My questions are:
Is this the standard way to read in an SQL script into SQL Server?
Is it supposed to take this long? This is how I would do it in MySQL/PHPMyAdmin it it might take a couple seconds, so I assume I'm not doing something right.
Here is the beginning of the script, I changed the file paths so they point to the right .mdf and .ldf files:
****/
--PART ONE CREATE THE DATABASE. Note the file paths in the first few commands.
--Change them for your own computer.--
USE [master]
GO
/****** Object: Database [ProgrammingEFDB1] Script Date: 01/28/2009 10:17:44 ******/
CREATE DATABASE [ProgrammingEFDB1] ON PRIMARY
( NAME = N'ProgrammingEFDB1', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ProgrammingEFDB1.mdf' , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'ProgrammingEFDB1_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\ProgrammingEFDB1_log.LDF' , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
GO
ALTER DATABASE [ProgrammingEFDB1] SET COMPATIBILITY_LEVEL = 90
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [ProgrammingEFDB1].[dbo].[sp_fulltext_database] #action = 'disable'
end
...
ANSWER:
I had already created a database with the same name so it was trying to create a database that was already there which made it hang for some reason. I deleted that database, reran the script and it completed successfully in 3 seconds.
I don't know what does your script do exactly in the next 754K, but the lines you posted seem quite harmless.
Try adding the following to your script:
SET STATISTICS TIME ON
This will show queries execution times as they run, and it will help you to locate the problem more exactly.
But the script (756K)
Must be a lot more than just a CREATE DATABASE in the script, so very hard to say when the script is doing.
You can write progress reports from the script back to the client, or use SQL Profiler to see what commands are being executed.