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
Related
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
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.
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 the following query from msdn to restore a SQL Server database backup
RESTORE DATABASE <DBName> FROM DISK = '<BackupFilePath>\<BackupFileName>'
GO
What if you want to load the backup to a new database with new name without restoring it to replace the current one?
In the interface I would select Restore Files and Filegroups and change the to database name then it would create a new one with the backup data in it, is this possible with a simple query?
The restore command above can be used to specify a new name, but when the old database still exists on the same machine you'll get an error because it will try to use the same filenames for the data. You can use the WITH MOVE clause to get around that:
RESTORE DATABASE TestDB
FROM DISK = 'C:\Backup.BAK',
WITH MOVE 'AdventureWorks2012_Data' TO 'C:\MySQLServer\testdb.mdf',
MOVE 'AdventureWorks2012_Log' TO 'C:\MySQLServer\testdb.ldf';
GO
The above example came from the MSDN documentation on the restore command:
http://msdn.microsoft.com/en-us/library/ms186858.aspx
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