I have a SQL server data backup size is above 20 GB. My database is in E drive have enough space but my SQL software installed in C drive which has low memory. In this scenario how can i restore my backup to E drive.
You can restore database via SQL Server Management Studio.
While restoring , you can specify MDF and LDF file location to your E: drive.
To specify the new location of the database files, select the Files
page, and then click Relocate all files to folder. Provide a new
location for the Data file folder and Log file folder
THIS MIGHT HELP
steps :
ALTER DATABASE database_name SET OFFLINE;
Move the file or files to the new location.
For each file moved, run the following statement.
ALTER DATABASE database_name MODIFY FILE ( NAME = logical_name, FILENAME = 'new_path\os_file_name' );
Run the following statement.
ALTER DATABASE database_name SET ONLINE;
Verify the file change by running the following query.
SELECT name, physical_name AS CurrentLocation, state_desc
FROM sys.master_files
WHERE database_id = DB_ID(N'<database_name>');
for more details go through url
USE MASTER;
GO
ALTER DATABASE DBName
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
-- Detach DB
EXEC MASTER.dbo.sp_detach_db #dbname = N'DBName'
GO
Step 1:Detach DB
USE MASTER;
GO
ALTER DATABASE DBName
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
-- Detach DB
EXEC MASTER.dbo.sp_detach_db #dbname = N'DBName'
GO
Step 2:
Now move the files from C drive to E drive. You can now reattach the files with E drive.
Related
How does one go about viewing a .bak file when they script out a backup command? For example when I run a script that has this SQL in it:
DECLARE #database NVARCHAR(75) = 'MyDatabase';
DECLARE #filename NVARCHAR(75) = 'C:\temp\MyDatabase.bak';
BACKUP DATABASE #database
TO DISK = #filename;
I get a message saying that backup was complete, however, I can't go to the location and view the .bak file. SSMS can pickup that it's there when I restore from device > file, but I can't view the file either in the window explorer or PowerShell prompt. How can one see the backup file that's being created?
Edit
The database is a local MS SQL Server Express running inside of a docker container. I think that should still act like the normal instance of SQL Server, but maybe someone can explain how it differs.
You're not able to see the backup file because it was taken in the SQL instance running in the Docker container. When you run the script to take the backup, the backup is stored in the storage available for the SQL instance (not for the SSMS).
Check how to access here: Accessing Docker container files from Windows or how to copy files from/to the docker container here: https://www.youtube.com/watch?v=ht4gqt4zSyw.
If you have access to the SQL Server instance where the backup was originally run, you should be able to query msdb:
SELECT * FROM msdb.dbo.backupset
WHERE database_name = 'MyDBname' AND type = 'D'
Here is the query that will give me the physical device name, backup start date, backup finish date and the size of the backup.
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 = 'YourDBname'
ORDER BY backup_finish_date DESC
I have created a small scrip to restore databases from backups. Once the script is run it displays
RESTORE DATABASE successfully processed 28818 pages in 1.568 seconds
(143.584 MB/sec).
I have more code to alter the database, alter a few views and sp too but I am getting the following error; User does not have permission to alter database 'GreyGoo', the database does not exist, or the database is not in a state that allows access checks.
I have noticed too that I cannot see the database in the object explorer
this is what I use to restore the DB from a backup
if DB exists set to a single user
if DB exist drop database
Ran the below script
RESTORE DATABASE GreyGoo FROM DISK = 'C:\Bkp\GreyGoo_backup_2020_03_02_180002_5403592.bak'
WITH
MOVE 'GreyGoo' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\GreyGoo.mdf',
MOVE 'GreyGoo_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\GreyGoo.ldf',
REPLACE;
set DB to multi-users and other properties
So what is the problem and how can I solve it, I am currently testing my code on SQL Server 2008
Thanks
During your restore process, you will need to ensure your
login / permissions are correct.
if another previous database with same name is not there, if so delete it. Perhaps from a previous attempt
You can also turn off recovery option like below, recovery is default
RESTORE FILELISTONLY
Sample -- MSDN
USE master;
GO
-- First determine the number and names of the files in the backup.
-- AdventureWorks2012_Backup is the name of the backup device.
RESTORE FILELISTONLY
FROM AdventureWorks2012_Backup;
-- Restore the files for MyAdvWorks.
RESTORE DATABASE MyAdvWorks
FROM AdventureWorks2012_Backup
WITH RECOVERY,
MOVE 'AdventureWorks2012_Data' TO 'D:\MyData\MyAdvWorks_Data.mdf',
MOVE 'AdventureWorks2012_Log' TO 'F:\MyLog\MyAdvWorks_Log.ldf';
GO
EXEC msdb.dbo.sp_delete_database_backuphistory #database_name = N'MYDB'
GO
USE [master]
GO
/* Database [MYDB] */
DROP DATABASE [MYDB]
GO
USE [master]
RESTORE DATABASE [MYDB] FROM DISK = N'E:\FTP\local_MYDB_01.bak' WITH FILE = 1, MOVE N'blank' TO N'E:\DBs\2019\MYDB.mdf', MOVE N'blank_log' TO N'E:\DBs\2019\MYDB.ldf', NOUNLOAD, STATS = 5
GO
I'm working on vb.NET 2013 and SQL server 2008R2.
I have this situation to resolve:
I have a database "DB1". I want to create a copy of this database on the same SQL server with another name "DB2", and after make this database ready to connect.
How can I do this through code from Vb.NET?
Check out this tech net link for restore with new file names.
http://technet.microsoft.com/en-us/library/ms190447(v=sql.105).aspx.
Take a backup of database one (DB1).
Verify backup is valid.
Get correct logical and physical file names
Restore with a move command to create database two (DB2).
Change ownership to SA. Defaults to SQL login that creates the database
Here is a script that I tested on my laptop. Nice thing about backup is no need for down time. If you decide to take off line, copy data files, and create database for attach, down time is created.
-- Sample database
USE AdventureWorks2012;
GO
-- 1 - Make a backup
BACKUP DATABASE AdventureWorks2012
TO DISK = 'C:\mssql\backup\AdventureWorks2012.Bak'
WITH FORMAT,
MEDIANAME = 'SQLServerBackups',
NAME = 'Full Backup of AdventureWorks2012';
GO
-- Use master
USE master
GO
-- 2 - Is the backup valid
RESTORE VERIFYONLY
FROM DISK = 'C:\mssql\backup\AdventureWorks2012.Bak';
GO
-- 3 - Check the logical / physical file names
RESTORE FILELISTONLY
FROM DISK = 'C:\mssql\backup\AdventureWorks2012.Bak';
GO
-- 4 - Restore the files change the location and name
RESTORE DATABASE Adv2012
FROM DISK = 'C:\mssql\backup\AdventureWorks2012.Bak'
WITH RECOVERY,
MOVE 'AdventureWorks2012_Data' TO 'c:\mssql\data\MyAdvWorks_Data.mdf',
MOVE 'AdventureWorks2012_Log' TO 'c:\mssql\log\MyAdvWorks_Data.ldf';
GO
-- 5 - Switch owner to system admin
ALTER AUTHORIZATION ON DATABASE::Adv2012 TO SA;
GO
I am attaching My DB through vb.net code.
Now I request help for the following:
How to detach it using code (uid=sa, pwd=abc123)?
How to take backup?
How to restore backup?
Please note I want to do it all using vb.net code
Thanks a lot.
This article series is a great place to get started and see samples - also in VB.NET.
Getting started with SMO in SQL Server 2005
And even if you encounter C# samples - there's always the online Convert C# to VB.NET site to convert those to VB.NET
Backup:
USE AdventureWorks2008R2;
GO
BACKUP DATABASE AdventureWorks2008R2
TO DISK = 'Z:\SQLServerBackups\AdventureWorks2008R2.Bak'
WITH FORMAT,
MEDIANAME = 'Z_SQLServerBackups',
NAME = 'Full Backup of AdventureWorks2008R2';
GO
Restore:
----Put database into single user mode (terminates open connections - else restore fails)
ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
RESTORE DATABASE YourDB
FROM DISK = 'D:\temp\YourDB.bak'
WITH REPLACE
,MOVE 'YourDB_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\YourDB_Data.mdf'
,MOVE 'YourDB_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\YourDB_Data.ldf'
ALTER DATABASE YourDB SET MULTI_USER
GO
Detach:
EXEC master.dbo.sp_detach_db #dbname = N'AdventureWorks',
#keepfulltextindexfile = N'true'
GO
Attach:
EXEC master.dbo.sp_attach_db #dbname = N'AdventureWorks2008R2',
#filename1 = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\AdventureWorks2008R2_Data.mdf',
#filename2 = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Data\AdventureWorks2008R2_log.ldf';
I'm running a T-SQL script that drops a database and then restores it. The script runs against a SQL Server 2008 database. Sometimes there is a problem with the backup file and the database gets stuck in the restoring state.
IF EXISTS (SELECT 1 FROM master.dbo.sysdatabases WHERE name = 'dbname')
BEGIN
ALTER DATABASE [dbname]
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
END
IF EXISTS (SELECT 1 FROM master.dbo.sysdatabases WHERE name = 'dbname')
BEGIN
DROP DATABASE [dbname]
END
RESTORE DATABASE [dbname]
FROM DISK = N'C:\dbname.bak'
WITH FILE = 1,
NOUNLOAD,
STATS = 10
The next time the script runs the script generates the error message
ALTER DATABASE is not permitted while a database is in the Restoring state.
What is the best way to check if the database is in the restoring state before trying to run the ALTER DATABASE command?
EDIT: The RESTORE DATABASE command that I'm running doesn't use the NO RECOVERY option.
It sounds as though you are performing a database restore with the NORECOVERY option. The reason you would want to do this is if you were planning to apply subsequent transaction log backups after the initial restore.
If you only wish to restore a single database backup then remove the NORECOVERY clause. If you are restoring transaction log backups, the final restore must be done without the NORECOVERY clause or if the last was applied with NORECOVERY you can RESTORE DATABASE DbName WITH RECOVERY to finalize.
To answer your question:
Method 1
SELECT DATABASEPROPERTYEX ('DatabaseName', 'Status')
See SQL Server Books Online: DATABASEPROPERTYEX (Transact-SQL)
Method 2
Review the sys.databases system view in order to determine the current state of a database. For example:
SELECT
state,
state_desc
FROM sys.databases
WHERE [name] = 'DatabaseName'
A state of 1 = RESTORING
See Sys.Databases for documentation regarding this system view.
SELECT DATABASEPROPERTYEX ('MyDb', 'Status')
Others had a similar problem doing a RESTORE with pyodbc. My variation of the problem (with similar symptoms to yours) turned out to be an incorrect bak file. This can be discovered with the following T-SQL, looking for incorrect .mdf or .ldf file names or database names:
RESTORE FILELISTONLY FROM DISK = N'C:\dbname.bak'
Method 2:
SELECT
state,
state_desc
FROM sys.databases
WHERE [name] = 'Databasename'
It is give me exact result.