SQL Server backup Using Windows command line - sql-server

My Windows Server 2003 got corrupted and I'm trying to repair it but before that I'm trying to create a backup of my SQL Server databases.
Can anyone please tell me which files do I need to copy from the Windows command line as I'm not familiar with SQL Server. Database files from which I can restore data.
Its an old server but data is important.
And also if I repair Windows server 2003 using repair disk will it effect on SQL Server files ?
http://postimg.org/image/5jsstbqmd/
When I start server I get this error.

You can use this SQL command (adapt to your specific case):
--Back up the files in SalesGroup1:
BACKUP DATABASE YourDBName
TO DISK = 'Z:\SQLServerBackups\BackupFileName.bck';
GO
See Backup in Transact-SQL for more details.
To run a SQL script from command line:
sqlcmd -S myServer\instanceName -i C:\myScript.sql
Before messing with anything, you could take a complete image of your hard drive using a tool such as clonezilla.

I would get to the root of your disk and run
dir /a /s *.mdf
The .mdf file is the file extension that SQL Server uses, and that command will tell you where they are located. The log files are usually in the same directory.
As per your second question, the disk repair will only affect your database files if they are part of the corruption that is happening; which is quite likely if you were running a high I/O database when it crashed. I would definitely try and copy those files off before running a disk check.

Related

RESTORE .bak-file from c:\users\ directories on SQL Server

I have this update tool for my program. The tool updates the SQL Server database with this code (vb and sql).
Dim sql As Process = Process.Start("sqlcmd.exe", Param + " -i update.sql -o log.txt")
Param contains the name of the .bak file myprogram_update.bak among others.
update.sql starts with
RESTORE DATABASE [myprogram_tmp]
FROM DISK = N'$(db_src)'
WITH FILE = 1,
MOVE N'myprogram_tmp' TO #mdf,
MOVE N'myprogram_tmp_log' TO #ldf,
NOUNLOAD, STATS = 5
#mdf and #ldf are standard paths read from the SQL Server settings.
The update tool is shipped to customers and the problem is that usually SQL Servers aren't allowed to read in user directories of windows like Desktop or Downloads. But many customers unzip the archive to these directories and then they get this error.
Could not access myprogram_update.bak / access denied.
I can't change the settings on every customers server, so is there any way to make it work for these directories? One idea of mine was to run a setup first and unzip it to program files but maybe there is a smarter solution.
Edit: the tool runs in administrator mode.
You should ask for elevation and run as an administrator account, maybe this will help:
http://www.downloadinformer.com/how-to-make-a-vb-net-application-always-run-in-administrator-mode/
I've now put my program into an setup which is usually installed under an ordinary folder, like C:\Program Files (x86)\MyProgram. Here SQL-Server has access.

Ubuntu SQL Server Restoration

I am trying to restore a SQL database using .bak file on an Ubuntu server where SQL Developers is installed.
I used the following command line:
sqlcmd -S localhost,1433 -U SA -Q "RESTORE VERIFYONLY JCIHistorianDB
FROM DISK = '/home/test_db.bak' WITH MOVE 'testDB_Data' TO
'/var/opt/mssql/data/test_db.mdf', MOVE 'testDB_Log' TO
'/var/opt/mssql/data/test_db.ldf'"
As per following, the result is successful:
RESTORE DATABASE successfully processed 528668 pages in 845.191 seconds
However the database data is around half of the size it should be. It's not coming from the .bak file as the restoration from the same file on a windows instance was all good.
Do you have any ideas why this happened? I also have enough space on the Ubuntu Server.
Thank you very much for your time.

How to Start SQL Server without TempDB

After the scheduled maintenance when the DBA tried to start the SQL Server;
it failed due to some corruption issue with storage subsystem.
Later on, we identified that the drive on which we had our TempDB's data and log files was corrupt and it was preventing SQL Server from starting successfully.
(Drive was corrupt, so I am unable to read anything from that drive)
So basically we did not have Tempdb database on the server.
And we had to start SQL Server without TempDB
So how do we start the SQL Server without TempDB and how do we fix this?
Before you try anything make sure you backup your data. If one drive failed, another one might fail and leave you without your data. Drives that are purchases at around the same time tend to fail around the same time too.
You need to do that even if some of the data is stored in a RAID array - RAID isn't the same as a backup. If something happens to the array, your best case scenario is that you'll wait for a few hours to recover the data. Worst case, you could lose it all.
The process is described in The SQL Server Instance That Will not Start in the TempDB location does not exist section, and other sites like Start SQL Server without tempdb.
You'll have to start SQL Server with Minimal Configuration. In that state, tempdb isn't used. You can do this with the -f command-line parameter. You can specify this parameter in the service's property page, or by calling sqlservr.exe -f from the command line, eg:
sqlservr -f
Another option is to use the -t3608 trace flag which starts only the master database.
sqlservr -t3608
After that, you need to connect to the server with the sqlcmd utility, eg :
sqlcmd -S myservername -E
to connect using Windows authentication.
Once you do this, you can go to the master database and change the file location of the tempdb files:
USE master;
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, FILENAME = 'E:\SQLData\tempdb.mdf');
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = templog, FILENAME = 'F:\SQLLog\templog.ldf');
GO
After that, remove the parameters from the service (if you set them there) and restart the service.
Finally, you may have to reconsider the placement of TempDB. TempDB is used heavily for sorting, calculating window functions or in situations where the available RAM isn't enough. Some operations require creating intermediate results, which get stored in TempDB. In general, you should have
multiple tempdb files, although the exact number depends on the server's workload.
How to Start SQL Server without TempDB database?
Step 1: Start the SQL Server in minimal configuration mode.
Click here
to see, "How to start the SQL Server in minimal mode using command prompt".
Step 2: Once SQL Server has started with minimum configuration mode;
connect to SQL Server instance and move TempDB data and log file to a new location.
See, move TempDB data and log files to new location
Step 3: Once you have performed the troubleshooting steps; exit SQLCMD window by typing Quit and Press Enter.
Step 4: . In the initial window click CTRL C and enter Y to Stop SQL Server Service.
Step 5 : Eventually, start the SQL Server Database Engine by Using SQL Server Configuration Manager.
What version of SQL Server it is? One simple solution is to move the tempdb.* files from that location and restart the SQL Server it will create new tempdb files. If you keep those files in that same location it will fail to start.
In SQL Server 2016 If you remove the tempdb physical files, on startup it will see they are missing and rebuild them on the fly in the location they are supposed to be in sysdatabases.

Not enough storage is available error when trying to open a HUGE sql file

On the surface, i know this file is huge and it sounds like i'm out of hdd space. I have over 193gb available. Now here's my question. I have a .sql file that's 65gb in size. I generated it by right clicking the database and generating scripts. I included the schema and data as well.
I want to run this script so I can have a good copy of the database on my local sql server. I try to open the file in sql server management studio 2012 and i get the following error.
I cant backup the database and restore it (the preferred method) so I googled this error but I got results about everything but my issue. What do i need to change in order to open and run this ginormous file. Thanks
I doubt that sqlcmd will give you any better results, but here is a reference page sqlcmd
here is a sample:
sqlcmd -S myServer\instanceName -i C:\myScript.sql

Using SQL Server Management Studio Express to Backup DB

I am wanting to backup a SQl Server 2005 db at a remote site (ie. to ultimately have a backup file of the db here locally on my machine).
Can this be done using SQL Server Management Studio Express ? I have this installed and running.. but cannot seem to find a way of backing up using it.
If this isn't possible, how do I create a backup of my remote db some other way ?
Thank you,
Bazza
It should be possible, right click on the database, select Tasks->Backup.
The other good option is the bcp command line utility. If your backup needs to be done regularly it's a better option since you can use it in a bacth file or script and create a scehduler task for it.
I'm not sure, however if you have remote access to the system concerned you should be able to run a SQLCMD and issue the backup from the command line.
1) create a script called say backup.sql
USE [master]
GO
BACKUP DATABASE [somedatabase]
TO DISK = N'C:\somedatabase.bak'
WITH NOFORMAT,
INIT,
NAME = N'Full Database Backup of somedatabase',
SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
2) issue:
sqlcmd -U username -P password -i backup.sql
Then copy the file over.
Unfortunately, I can't answer your specific question regarding SSMS Express - I've not used the tool extensively. However, I do know that you can open a New Query window and issue a BACKUP DATABASE command. An example of this would be:
backup database <dbname,,> to disk='c:\mydbbackup.bak';
You could then use standard methods (such as FTP) to get the file copied locally. Hope that helps!
This works in SSMS 2008 Express:
Right-click the database name in object explorer > Tasks > Backupā€¦
Select "Full" (should already be default), enter a name, and at the bottom click "Add" and create the file to which you want to back up.
Run the backup.
Copy the file to your local machine.
Connected to the local machine, select the "Databases" node in Object Explorer > Restore Databaseā€¦
Now name the new database, select "from device" and choose the backup file from which to create the new database, and go.
Done.

Resources