Dynamically get DB logical name when restoring from .bak file - sql

I am trying to create a python script that will dynamically restore databases from .bak files. Running the query
RESTORE DATABASE db_name FROM DISK = '\\path\to\db\db_name.bak' WITH REPLACE
works great if the original database name doesn't exist, however since there is multiple servers and often duplicate names this is not always the case.
In the case that the .mdf name contained in the .bak file does exist, the WITH MOVE syntax is useful as shown below
RESTORE DATABASE db_name
FROM DISK = '\\path\to\db\db_name.bak'
WITH
MOVE 'Logical_name' TO '\\somepath\...\MyDB.mdf'
Unfortunately, this requires knowledge of the logical name before the query is run and is therefore not very useful for a general purpose script. Is there any way to get the logical name and populate it dynamically to make this more general purpose?

Use restore filelistonly commamd

Related

Create database backup, ignore column

I'd like to create a database backup using SSMS.
The backup file will be a .bak file, but I would like to ignore 1 column in a certain table, because this column isn't necessary, but it takes up 95% of the backup size.
The column values should all be replaced by 0x00 (column type is varbinary(max), not null).
What's the best way to do this?
FYI: I know how to generate a regular backup using Tasks => Back Up..
There is a long way of doing what you ask. Its basically create a new restored database, remove the non required data and then do a new backup again.
Create a Backup of the production database.
Restore the backup locally on production with a new name
Update the column with 0x00
Shrink the database (Shrink is helpful when doing a restore. This wont reduce the bak file size)
Take the backup of the new database (Also use Backup Compression to reduce the size even more)
Ftp the bak file
If you only needed a few tables, you could have used bcp but that looks out of the picture for your current requirement.
From SQL Server native backups, you can't. You'd have to restore the database to some other location and then migrate usefull data.
You can create a copy of your table without the column and backup using filegroups https://msdn.microsoft.com/en-us/library/ms191539(SQL.90).aspx

Backup SQL Server database using WITH FORMAT

I'm having a bit of trouble understanding the purpose of the WITH FORMAT option of the SQL Server BACKUP DATABASE command.
Using the MSDN example:
USE AdventureWorks2012;
GO
BACKUP DATABASE AdventureWorks2012
TO DISK = 'Z:\SQLServerBackups\AdventureWorks2012.Bak'
WITH FORMAT,
MEDIANAME = 'Z_SQLServerBackups',
NAME = 'Full Backup of AdventureWorks2012';
GO
I understand this deletes any existing backups and creates a new one.
If I was to omit the WITH FORMAT line, and I already had a backup at Z:\SQLServerBackups\AdventureWorks2012.Bak, what does this mean to my backup?
Is it incremental or does it overwrite with a full one?
It's a full backup, essentially appended to the file if it exists already, you'll see a File number 2 reference when you run it, and you'll see the backup file double in size. Each backup is independent of each other and either can be restored.

Restore database in sql server

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.

SQL Server 2005 backup and restore

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?

How to do a partial database backup and restore?

Simple problem. I'm working on a single SQL Server database which is shared between several offices. Each office has their own schema inside this database, thus dividing the database in logical pieces. (Plus one schema that is shared between multiple offices.) The database is stored on a dedicated server and we use a single database to keep the backup/restore procedure easier.
The problem, however, is that the Accounting Office might be modifying a lot of data and then the Secretary Office makes a mistake which requires restoration of a backup. Unfortunately, restoring the backup means that Accounting will lose their recently added data.
So, the alternative solution is by restoring the backup into a new database, remove the data from the old accounting schema and move the data for accounting only from the backup top the original database. This is the current solution and it's time-consuming and error-prone.
So, is there a way to make backups of a single schema, possibly through code? And then to restore just that schema, probably through code too?
You could create a script that copies each of the schemas to a separate database (backup_Accounting, backup_Secretary, backup_Shared), and then creates a backup file for each of those databases. If you ever need to do a restore, you can restore the backup file into the appropriate database and then run a script to copy the data back into the main DB.
You could use filegroups and the partial backup command.
You'll need to move each schema to a different filegroup and then use partial backup/restores as required.
See here for info on partial backups: http://msdn.microsoft.com/en-us/library/ms191539.aspx
See here for info on file groups: http://msdn.microsoft.com/en-us/library/ms179316.aspx
See here for info on piecemeal restore: http://msdn.microsoft.com/en-us/library/ms177425.aspx
There is no way to backup and restore just a single schema.
However, you may try this approach: Restore the entire database (all schemas) to a different database xyz_OLD or something like that.
You could then fix the data using a script like:
UPDATE y
SET col1=o.col1
FROM xyz.YourTable y
INNER JOIN xyz_Old.xyz.YourTable o ON y.PK=o.PK
INSERT INTO xyz.YourTable
(col1, col2, col3,...)
SELECT
col1, col2, col3,...
FROM xyz_Old.xyz.YourTable o
WHERE NOT EXISTS (SELECT 1 FROM xyz.YourTable y WHERE o.PK=y.Pk)
etc...