I have list of SQL databases taken as a backup and stored in D:\Backup\ drive, Task is to restore all backups, back to SQL Server. Looking for stored procedure which will open each file from directory and restore all files one by one to server.
Sample list of Databases
restore database master from disk='D:\BACKUP\DB1.bak' with replace
restore database master from disk='D:\BACKUP\DB2.bak' with replace
restore database master from disk='D:\BACKUP\DB3.bak' with replace
restore database master from disk='D:\BACKUP\DB4.bak' with replace
restore database master from disk='D:\BACKUP\DB5.bak' with replace
restore database master from disk='D:\BACKUP\DB6.bak' with replace
restore database master from disk='D:\BACKUP\DB7.bak' with replace
Thanks
I would strongly encourage you to look at the dbatools powershell module for this. For your needs:
$server = 'yourServer';
$backups = get-dbabackupinformation -SqlInstance $server -Path 'D:\BACKUP';
$backups | restore-dbadatabase -SqlInstance $server -withreplace;
Note - restore-dbadatabase has a bunch of options. The defaults are pretty good for most cases but see what's available.
But also! Two things grab my attention about your sample:
You're restoring the master database. That should only be done if something extraordinary happened (e.g. disk corruption, an admin or malicious actor trashed the server, etc).
You're restoring the same database from multiple different files without specifying with norecovery.
I'd guess that both of these are attributed to fast and loose with the sample code. But it's worth calling out as restoring master will have consequences for your server and restoring multiple files could make your restore take longer than it should (e.g. if each of those backups represents a daily backup, restoring only the one that represents your desired RPO makes sense).
Related
I can't seem to find any SQL that will clone one database in SQL Server within the same server.
Let's say I have a database called MyDB. I simply want to make a copy of MyDB to MyDb2. I thought that this would work:
BACKUP DATABASE MyDB TO MyDB2;
But I get this error when I try to execute it:
Backup device 'DbTestBack' does not exist. To view existing backup devices, use the sys.backup_devices catalog view. To create a new backup device use either sp_addumpdevice or SQL Server Management Studio.
Does anyone know what the best way to do this is? I want an exact duplicate of the original including security permissions.
A simple way is taking a back up copy of current DB and restoring it.
You Can do this in single step with a simple script
backup database MyDB
to disk='D:\MyDB.bak';
restore database MyDB2
from disk='D:\MyDB.bak'
WITH move 'MyDB_Data' to 'D:\MyDB2_Data.mdf',
move 'MyDB_log' to 'D:\MyDB2_Data.ldf';
GO
Note: I made an assumption on your current data file and log file name (MyDB_Data, MyDB_log), you need to check them and make correct
DBAtools is your friend here.
Use Copy-DbaDatabase
ie.
Copy-DbaDatabase -Source SRV1 -Destination SRV1 -Database myDB -BackupRestore -SharedPath \\<<your temporary server location such as c:\temp>>
I have some sql server's backup file(format .bck). In my case, I do not know log file name.
So I can't use with move in restore command.
Is there anyway to restore database without move? If yes, please guide me.
I want to use like below one.
restore database testdb from disk = 'folderpath\aatest.bak'
You sure can know the log file name and use WITH MOVE. Use RESTORE FILELISTONLY to retrieve the list of database files (including log) from the backup first.
I have two backup files
1) is named 'backup.sql' with a bunch of SQL defining TABLES
2) is named 'backup' with a bunch of encoded data, which I believe are the ROWS
I need to restore these TABLES + ROWS, but all I am able to figure out is how to restore the tables.
Any tips on dealing with these files? It's the first time I ever deal with SQL Server.
The backup process would not create a file with actual SQL statements, it would create a binary file. So #1 is not a backup file (it's probably a script someone saved to re-create the schema).
I would try to use SQL Server Management Studio to restore the second file and see what happens. I don't think it will allow you to restore an invalid file, but I would take some basic precautions like backing up the system first.
What is the extension for the 'backup' file? Is the filename backup.bak? If you have a backup file created by sql server then it 'should' contain the logic to create both the tables and restore the data, but it could depend on how the backup was created.
---Edit
It is possible for a .SQL file to contain data values as well as the logic to create the tables/columns for a database. I used to run backups of a MySql database in this way a long time ago...it just is not seen very often with SQL server since it has built in backup/restore funcationality.
Seems unlikely they would export all the rows from all tables into CSV file, and given you said it looks encrypted, it's making me think that's your actual backup file.
try this, save a copy of the "backup" file, rename it to backup.bak and run this from SQL Server Management Studio
restore filelistonly from disk='C:\backup.bak'
(assuming your file is saved on the root of the C: drive)
Any results/errors?
I'm looking for a simple script that I can use to overwrite one database with another one. We have a master db with master schema and data and every so often a developer wants to blow away his messed up db with a complete overwrite from the master. I currently accomplish this with SQL Server Studio and the GUI controls but I want something similar to what we use when restoring from a backup file (just without the backup file step):
RESTORE DATABASE [SlaveDB]
FROM DISK = N'E:\Backup\MasterDB.bak'
WITH FILE = 1,
MOVE N'SlaveDB_Data' TO N'E:\Data\SlaveDB_Data.mdf',
MOVE N'SlaveDB_Log' TO N'E:\Log\SlaveDB_Log.ldf',
NOUNLOAD,
STATS = 10
GO
What's the syntax for getting the db from another db instead of a backup file?
if you want to restore database from a generated script file you can use windows command line.
open CDM and run the below command (database=NorthWind, script file C:\MyScript.sql)
sqlcmd -S localhost -d NorthWind -i "C:\MyScript.sql"
I use this script every day to restore production backup to test database.
db names on production and on test are the same, suppose its name is MyDb.
delete test database
Run the script
RESTORE FILELISTONLY
FROM DISK = 'E:\WorkCopy\BackUp.bak'
RESTORE DATABASE [MyDb]
FROM DISK = 'E:\WorkCopy\BackUp.bak'
WITH
MOVE 'MyDbPrimary' TO 'D:\data\MyDb\WorkCopy.mdf',
MOVE 'MyDbImp' TO 'D:\data\MyDb\WorkCopy_1.ndf',
MOVE 'MyDbCut' TO 'D:\data\MyDb\WorkCopy_2.ndf',
MOVE 'MyDbIX' TO 'D:\data\MyDb\WorkCopy_3.ndf',
MOVE 'MyDbAUD' TO 'D:\data\MyDb\WorkCopy_4.ndf',
MOVE 'MyDbLog' TO 'D:\data\MyDb\WorkCopy_5.ldf',
move 'sysft_FTIndexCatalog' TO 'D:\data\MyDb\FTIndexCatalog'
ALTER DATABASE MyDb
Set RECOVERY SIMPLE
I have been using this query:
BACKUP DATABASE RentalEase
TO DISK = 'C:\RentalEaseBackup\RentalEase.bak'
WITH COPY_ONLY;
GO
To backup my database. Someone deleted something so now I have to restore it from a previous point in time, however I don't want to overwrite new changes (other than the deletions).
What I was thinking I could do it attached the backup to the SQL Server as a new database and then perform the necessary queries to move the few deleted rows over. However, it won't attach the RentalEase.bak file because it says it isn't a primary database file.
How can I attach the database backup so I can run the SQL queries against it?
You have to RESTORE the DB, you can't attach a backup file
RESTORE DATABASE TestDB
FROM DISK = 'c:\Northwind.bak'
WITH MOVE 'Northwind' TO 'c:\test\testdb.mdf',
MOVE 'Northwind_log' TO 'c:\test\testdb.ldf'
Full syntax here
Restore the database to a different database name, and then you can do whatever you want between the two databases (good luck!)
create a new database named RentalEase2, A restore would look like this
RESTORE DATABASE [RentalEase2] FROM DISK = N'C:\RentalEaseBackup\RentalEase.bak'
WITH FILE = 1, NOUNLOAD, STATS = 10
GO