bulk convert .mdf and .ldf files from a folder to .bak - sql-server

I have a folder with more than 300 .mdf and .ldf microsof SQL server database files(recovered from a ransomware attack to our ESXI server then I have no access to SQL server Master db)
Without access to SQL server system databases to fetch database names and ..., How can I bulk convert all these database files to SQL server backup (.bak) files with no need to manually attach them one by one to sql server in SQL management studio.

You won't be able to convert the MDF/LDF to a BAK file, so you are going to need to reattach the databases.
The database name is usually in the file name; do you know the structure of these databases - did they have more than one data/log file for example?
You could use Powershell to generate a list of discrete names using:
Get-ChildItem
Select-Object -unique
And then iterate through that list and use Invoke-SQLCMD to run the CREATE DATABASE FOR ATTACH commands using:
foreach
Where-Object {$listOfFiles -like "$currentItem*.mdf/ldf"}
Invoke-SQLCMD
MSSQLTips Article on Attach/Detach with T-SQL examples

Related

How to attach a database by mdf file that has not been detached

Issue:
OS hard disk was broke before DB backing up, the original .mdf and .ldf files can be transferred but those .mdf and .ldf files can not be attached by SQL Server Management Studio directly.
Solution:
Assuming the DB name is ‘Sample’ and DB files path is ‘D:\DB’
Step 1: Open Microsoft SQL Server Management Studio to create a New Database with
Database name: Sample
Path: D:\DB
Step2: Stop the DB Server
Step3: Delete Sample.mdf and Sample.ldf files from folder D:\DB
Step4: Copy the old Sample.mdf to the folder D:\DB
Step5: Start the DB Server
Step6: execute the following sql to detach DB
exec sp_detach_db Sample,'true'
Step7: execute the following sql to attach DB and then refresh
exec sp_attach_single_file_db 'Sample','D:/DB/Sample.mdf'

How to get BAK files from SQL 2008 R2 to Azure

I know from SQL 2012 you can backup directly to Azure, but I have a SQL 2008 R2 instance where I'm needing to get the backups up to Azure for off-site backup reasons.
Anyone have some good options for what could be considered here?
You can write a PowerShell script, which will backup your database using Backup-SqlDatabase cmdlet, and then copy the BAK file to Azure Blob Storage using Set-AzureStorageBlobContent cmdlet. Of course, you can also use your existing backup solution, but it may be more difficult to plug the call to Set-AzureStorageBlobContent in it. If you use SQL Server Agent, you can add a step with PowerShell or call to external executable to upload the recently made backup.
# Upload file from local disk to Azure Blob Storage
Set-AzureStorageBlobContent -Container "SQLServerBackups" -File "E:\Backups\MyDatabase-2018-01-01.bak" -Blob "MyDatabase-2018-01-01"

how to rename database in local disck to attach again in sql server

I have database in sql server in local disk ,I was renamed it to attach again but this message occer Cannot attach a database with the same name as an existing database
Did you rename mdf /ldf file as well?
Thanks,

How to create BAK file from azure sql db

I want to move my azure sql db to another host. However my new host allows to import sql db backup-files (.BAK) only;
I found out how to generate .BACPAK files with SQL Server Management Studio but I could not find a way to create a BAK-file from my azure sql db.
Please help.
However my new host allows to import sql db backup-files (.BAK) only;
SQL Azure doesn't provide a native way to generate '.bak' format backup file. If you did need this format file, you could import the BACPAC File to a local SQL Server which supports importing BACPAC File. After that, a new User Database will be created. Then you could generate a '.bak' format file from the local SQL Server.
In addition, you also could try a tool named SqlAzureBakMaker which could make '.bak' file for you easily.

What is the SQL Loader equivalent in SQL Server for extracting a database from SQL Server to the same server?

I am trying to replicate or copy a database in SQL Server 2014 in order to create a sandbox. I know SQL Loader does that for Oracle. Is there an equivalent tool in SQL Server?
We use bcp.exe as Loader program. It is located in C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn
You might do this in following ways:
1.Stop the sql server, copy the *.mdf file locating it right clicking on existing database -> properties from the server to the destination sever and attach it to the destination server using SSMS.
2.Create SSIS package and execute that using DTExec.exe.You can create a .bat file ,configure and call the DTexec.exe from that .bat file.This will be alternatve to oracle SQL loader for ETL tasks.
One method would be to create a database backup. Once the backup is completed, restore that backup under a different name.
Steps:
right click database from SSMS
Tasks -> Backup
Follow wizard
Once backup is complete, right click databases folder from the server and select restore database
Follow wizard, ensuring you're restoring the database to a new name.
Here are more detailed tutorials:
https://msdn.microsoft.com/en-us/library/ms187510.aspx
https://msdn.microsoft.com/en-us/library/ms177429.aspx
Note that if your intention is to test an application with the copied DB, ensure you're updating connection strings and whatnot to point to the copy.

Resources