I have a backup application and some of my customers want their SQL Server databases backed up. I need SQL Server to give me compressed files with file fixed name (without timestamp). I have tried using the command 'SqlCmd -E -S...' but it does not compress the database (need to change configuration in SQL Server) which customers are not comfortable with. Also due to lack of free space on the hard disk we need it compressed. With 'SQL Enterprise Studio' the backup that takes place always has a timestamp in the name. I need the backup filename fixed eg: ABC.BAK
Do you necessarily have to use this backup application you mentioned? I'd just whip the T-SQL at it:
BACKUP DATABASE [CustomerDB] TO DISK = N'D:\ABC.BAK' WITH COMPRESSION
GO;
That'll let you define the file names as well as override the server's default compression for that backup.
You could use sqlcmd to both compress the backup and give the backup file whatever name you want. sqlcmd can take an input file to execute against a SQL Server. Here is an example of the sqlcmd you would run
sqlcmd -S localhost -E -i ./SQLQuery1.sql
Where I'm connecting to my localhost installation of SQL Server (default instance name), and a trusted connection. You know this bit already of course. The new thing is the command-line option at the end.
-i ./SQLQuery1.sql
Here is an example SQLQuery1.sql file (you can of course call it whatever you would like)
USE [master]
GO
DECLARE #Path NVARCHAR = N'C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup\Example.bak';
BACKUP DATABASE [Example] TO DISK = #Path
WITH NAME = N'Example-Full Database Backup'
,COMPRESSION;
GO
Change the #Path variable to wherever you want the backup to be written and change the name of the file at the end of the #Path variable to whatever you want to call the file.
Related
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.
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.
I have a database server with two SQL instances on:
SQLExpress
SQLWebEdition
I have a backup script that is executed via a batch file and schedule to back up the databases. Normally this works fine, but I have one particular server I'm setting the scripts up on that has two instances of SQL. I'm able to do this for the SQLExpress instance, but cannot back up the WebEdition ones.
I think the script can only see the one instance and not the other instance.
Is there something I can put in the script so that the SQL script can see a named SQL instance?
eg
This sees one instance of the SQL on the server
BACKUP DATABASE [database] TO DISK = #backupfile WITH NOFORMAT, INIT, SKIP, STATS = 10, COMPRESSION
Where #backupfile is a variable holding the path disk.
the pseudo code of what I need to achieve (or similar)
BACKUP DATABASE the_server_instance \ [database] TO DISK = #backupfile WITH NOFORMAT, INIT, SKIP, STATS = 10, COMPRESSION
Not at all. That is not possible. You can validate that in - cough - the documentation explaining the BACKUP DATABASE command - which you can find at http://msdn.microsoft.com/en-us/library/ms186865.aspx
As you can clearly see there, there is no way to enter a server name. It also makes sense - backup database backs up the database of the active connection.
What you need is 2 scripts, sending each one to the separate database instance. The plus of multiple scripts is that - hey, they can run in parallel.
But if you can explain why you retain the Express edition - there is no reason to have 2 instances like that. Obviously outside of "waste memory and make things less efficient".
My Query is regarding using NOT hard coded File Locations to initialize the the Variables DefaultDataPath and DefaultLogPath. Prior to adopt Database Projects as our standard Deployment and Database Management Tools and migrating our existing Scripts to Database projects we have been using the SET of CREATE and INITIALIZE scripts for Setting up Database. We are having following SQL Query to CREATE the Database with the FILE location:
SET #data_path = (SELECT SUBSTRING(filename, 1, CHARINDEX(N'master.mdf', LOWER(filename)) - 1)
FROM sys.sysaltfiles WHERE dbid = 1 AND fileid = 1);
set #mdb_file=#data_path + 'CF_DB.mdf'
set #cfdata='CF_DB_Data'
set #cflog='CF_DB_Log'
set #ldf_file=#data_path + 'CF_DB_log.ldf'
declare #sql nvarchar(500)
set #sql = 'CREATE DATABASE [CF_DB] ON (NAME = ' + quotename(#cfdata) + ',FILENAME =' + quotename(#mdb_file) + ',SIZE = 53, FILEGROWTH = 10%) LOG ON (NAME =' + quotename(#cflog) + ',FILENAME = ' + quotename(#ldf_file) + ', SIZE = 31, FILEGROWTH = 10%)COLLATE SQL_Latin1_General_CP1_CI_AS'
exec(#sql)
Here we are trying to figure out the location of MDF file for MASTER DB and using the same location to CREATE DATABASE.
Problem: With the scripts generated (after Deploy action) , there is an auto Generated SQLCMD variables , initialized with some default path (hardcoded one ) or Empty strings (which are using Default Datafile path used by SQL Server 2008 or 2005).
:setvar DatabaseName "CF"
:setvar DefaultDataPath "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER2008\MSSQL\DATA\"
:setvar DefaultLogPath "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER2008\MSSQL\DATA\"
We need to make it work like our existing system. We need to know path of MASTER DB data and log files and using the same path to initialize DefaultDataPath and DefaultLogPath. We can't go with PreDeployment scripts because Database settings are done by Database Project generated script before embedding PreDeploymentScript in the final Deploy Scripts.
NEXT big thing: Developer need to switch to SQLCMD Mode in SQL Server Management Studio to run the scripts generated by DB Project. This is our implementation Team's requirement NOT TO USE SQLCMD mode to setup DATBASE. To overcome these step, I need to modify the generated SQL file and use SQL Variables instead of SQLCMD variables. Can we generate the clean SQL Statements and keeping automation script generation intact? I know both of these issues are corelated thus the solution for one is going to Fix the other one.
Thanks for any good suggestions or help upon the above discussions.
Regards
Sumeet
Not sure how best to handle your file path, though I suspect you will want to not use the Default File Path setting and instead use a new file path that you can control through a variable.
It sounds to me like you're trying to have the developers update their local machines easily. My recommendation would be to build out some batch files that do the following:
Set the PATH to include the location for MSBuild.exe
Get the location for your master database
Pass that location in to a variable
Run the MSBuild command to set your path variables to the local master path
and publish the database/changes.
Overall, that sounds like more trouble than it's really worth. I'd recommend that you send out a SQL Script to all of the developers getting them to properly set up their Default Data/Log paths and just use the defaults.
I do recommend that you look into setting up some batch files to run the MSBuild commands. You'll have a lot easier time getting the database builds to your developers without them generating scripts and running them locally. Alternatively, you could have them change their SSMS defaults to set SQLCMD mode on for their connections. SSDT made this a little nicer because it won't run at all without SQLCMD mode turned on - eliminated a lot of the messiness from VS2008/VS2010 DBProjects.
I used something like the following to build and deploy when we were on DB Projects:
msbuild /m .\MyDB\MyDB.dbproj /t:build
msbuild /m .\MyDB\MyDB.dbproj /t:deploy /p:TargetConnectionString="Data Source=localhost;Integrated Security=True;Pooling=False;" /p:TargetDatabase="MyDB"
When SSDT/VS generates the SQL file, it actually runs some queries against the server you've told it to connect to. The settings you're getting here for example...
:setvar DatabaseName "CF"
:setvar DefaultDataPath "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER2008\MSSQL\DATA\"
:setvar DefaultLogPath "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER2008\MSSQL\DATA\"
Are obtained from server settings from the target database connection you specified in your publish file/profile.
On the server that you are using to generate your scripts, open regedit.exe and search for the keys "DefaultLog" and "DefaultData" under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft - they should be in the same location. See if they match the settings your scripts are generating.
You can change these on the server/your PC (where ever you are pointing to) and it will generate the locations you enter in your generate SQL Scripts. Be cautious naturally around a server you do not own, or that is in use for production etc as this will change a setting on the server which points SQL Server where to place new databases. This is a different setting it seems than the one you enter in SQL Server properties -> Database Settings.
Hope that helps!
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.