How to setup weekly auto backup in SQL Server 2012? - sql

Please advice how can i setup automated database backup in my SQL Server 2012.
I need to take all databases (currently it contains only 3 ) in SQL server an automated weekly backup which runs on Every Friday at 0100 h (1 AM). These back up files (*.bak) should be placed in E:\Backups folder.

In Microsoft SQL Server Management Studio, open the Object Explorer and then:
Right-clic on Management > Maintenance Plans
Clic on New Maintenance Plan...
Give a name to your plan
Create as many subplans as you need for your strategy
Select a subplan and drag'n'drop the appropriate tasks from the Toolbox panel
To backup a database, the appropriate task is Back Up Database Task
For the configuration of the backup schedule, you just need to follow the wizard and define what you want. If you need more information, i suggest you to go on the official website of Microsoft:
Create a Full Database Backup
Hope this will help you

You can either create a SQL Server agent job or maintenance plan in ssms as mentioned, or use a 3rd party application. I use ApexSQL Backup at the moment, as it offers in depth schedule for any created job. You can specify if you want to create daily, weekly or monthly schedule. Besides, you can always pause, or delete these schedules if you don’t want to use them for some reason.

Go to MS SQL Server Management studio→SQL Server Agent→New Job
Under General tab enter Backup name
Under Steps tab:
Type Step name
Select database you want to backup
Enter backup query
Note: Sample backup query here with backup name date and time (e.g. TestBackup_Apr 4 2017 6,00PM.bak)
DECLARE #MyFileName nvarchar(max)
SELECT #MyFileName = (SELECT N'E:\TestbackupFolder\TestBackup_' +replace(rtrim(convert(char,getdate())), ':',',')+ '.bak')
BACKUP DATABASE [yourdatabasename] TO DISK=#MyFileName
WITH INIT;
In Schedules→New, go to new schedule and set date times as required

Check this opensource backup windows service MFSQLBackupService, it did a great job for me.

I can advice you to try the software Cloudberry as a backup agent to backup the data you wish. It sets the automated backup in the way you want to do it and place the backups where you want.

Related

How to backup script for subset of tables in SQL Express DB

I have developed a SQL Express database. I need to backup all but one table in that database in an automated way. I was thinking i could write a SQL script to do this, trigger it using sqlcmd from a batch file but not sure how to write that SQL script.
I was also thinking, if nothing else possible, i could create a second db that has the tables i want to backup then i write a script that copys data 'into' the second db and then do a auto backup of that entire db. This has the disadvantage of having a procrastinated unpacking of that backup when wanting to use it - its not a small install script.
Is this a possibility, is it the only option or is there tools for SQL Express to do this?
There is no option to exclude just one table while backing up .Few things i could think of
1.Right click database ->Tasks ->generate scripts ->exclude the table you want and choose to save the script and run this every time
2.you could also choose Export option,but since you are using SQL Express,you wont have the option to save this package
Keep the large table in a different database and just backup the original database. You can still use the large table even in a different database, i.e.
SELECT *
FROM MyDb.dbo.SomeTable s
JOIN OtherDb.dbo.LargeTable l
ON (expression);

SQL Server 2012: Table data backup in Excel

Is it possible to take a table's data backup as Excel in SQL Server 2012?
We can get the full database backup by using a maintenance plan automatically in .bak format or using import/export wizard to take Excel backup manually.
But I need to take a table's data backup in Excel (.xlsx/.xls) after 6 hour time interval automatically. Is it possible in SQL Server 2012?
No, you won't be able to do that. The SQL Server backup creates a .BAK file and that is the only option you have. However, what I might suggest you do is to create an SSIS package that takes the data in your database and creates an Excel file, and schedule the SSIS package to fire every 6 hours.
This might be a bit tedious to set up, especially if you have a lot of tables, but it can be done. And, as you probably know, if any of your tables has more than 1M rows, you're going to lose data sending it to Excel.

How to undo a SQL Server UPDATE query?

In SQL Server Management Studio, I did the query below.
Unfortunately, I forgot to uncomment the WHERE clause.
1647 rows were updated instead of 4.
How can I undo the last statement?
Unfortunately, I've only just finished translating those 1647 rows and was doing final corrections , and thus don't have a backup.
UPDATE [dbo].[T_Language]
SET
[LANG_DE] = 'Mietvertrag' --<LANG_DE, varchar(255),>
,[LANG_FR] = 'Contrat de bail' -- <LANG_FR, varchar(255),>
,[LANG_IT] = 'Contratto di locazione' -- <LANG_IT, varchar(255),>
,[LANG_EN] = 'Tenancy agreement' -- <LANG_EN, varchar(255),>
--WHERE [LANG_DE] like 'Mietvertrag'
There is a transaction protocol, at least I hope so.
A non-committed transaction can be reverted by issuing the command ROLLBACK
But if you are running in auto-commit mode there is nothing you can do....
If you already have a full backup from your database, fortunately, you have an option in SQL Management Studio. In this case, you can use the following steps:
Right click on database -> Tasks -> Restore -> Database.
In General tab, click on Timeline -> select Specific date and time option.
Move the timeline slider to before update command time -> click OK.
In the destination database name, type a new name.
In the Files tab, check in Reallocate all files to folder and then select a new path to save your recovered database.
In the options tab, check in Overwrite ... and remove Take tail-log... check option.
Finally, click on OK and wait until the recovery process is over.
I have used this method myself in an operational database and it was very useful.
Considering that you already have a full backup I’d just restore that backup into separate database and migrate the data from there.
If your data has changed after the latest backup then what you recover all data that way but you can try to recover that by reading transaction log.
If your database was in full recovery mode than transaction log has enough details to recover updates to your data after the latest backup.
You might want to try with DBCC LOG, fn_log functions or with third party log reader such as ApexSQL Log
Unfortunately there is no easy way to read transaction log because MS doesn’t provide documentation for this and stores the data in its proprietary format.
Since you have a FULL backup, you can restore the backup to a different server as a database of the same name or to the same server with a different name.
Then you can just review the contents pre-update and write a SQL script to do the update.
If you can catch this in time and you don't have the ability to ROLLBACK or use the transaction log, you can take a backup immediately and use a tool like Redgate's SQL Data Compare to generate a script to "restore" the affected data. This worked like a charm for me. :)
I have a good way to undo or recovery databases using SQL Server Manager. by following the below points :
1 - Go to Database from left side -> Right-click -> Task-> Restore -> Database.
2- Go to Timeline then select your time.

Sql Server 2005 Restore Failing

Running sql server 2005 I have database A. I am trying to restore from a backup of A to database B. I want to retain the database A and create a new testing database B from a previous set of data.
I tried to create B and restore from the .bak AND restore database to B from management studio.
The error is...
TITLE: Microsoft SQL Server Management
Studio
Restore failed for Server
'195448-APP2'.
(Microsoft.SqlServer.Smo)
For help, click:
http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.1399.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Restore+Server&LinkId=20476
------------------------------ ADDITIONAL INFORMATION:
System.Data.SqlClient.SqlError: The
backup set holds a backup of a
database other than the existing 'B'
database. (Microsoft.SqlServer.Smo)
For help, click:
http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.1399.00&LinkId=20476
------------------------------ BUTTONS:
OK
I found this snippet which I am hesitant to use and want to ask if it would solve my problem of changing the location of the mdf and ldf during the process of restoring the database or does it replace database A's items altogether.
ALTER DATABASE AdventureWorks
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE
RESTORE DATABASE AdventureWorks
FROM DISK = 'C\:BackupAdventureworks.bak'
WITH MOVE 'AdventureWorks_Data' TO 'C:\Data\datafile.mdf',
MOVE 'AdventureWorks_Log' TO 'C:\Data\logfile.ldf',
REPLACE
[http://blog.sqlauthority.com/2007/04/30/sql-server-fix-error-msg-3159-level-16-state-1-line-1-msg-3013-level-16-state-1-line-1/][1]
and for me I would make it...
RESTORE DATABASE B
FROM DISK = 'C:\backupofA.bak'
WITH
MOVE 'B' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B.mdf',
MOVE 'B_log' to 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B_log.ldf',
REPLACE
What I don't know is if it will affect database A at all. I am hoping the replace refers files associated with B.
or if it should be
RESTORE DATABASE B
FROM DISK = 'C:\backupofA.bak'
WITH
MOVE 'A' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B.mdf',
MOVE 'A_log' to 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\B_log.ldf',
REPLACE
If anyone could help me with the error and/or confirm this fix I would be very grateful as it is not my database I'm playing with.
Thanks.
You could simply use the Copy Database Wizard.
If you wanna do it like pros and use T-SQL the RESTORE .. MOVE ... REPLACE will do what you expected: move the two files at the locations you intend and replace database B with content from the backup. A will be unaffected.
I would use the wizard if I were you: In Sql Server Management Studio right click on "Databases" and select "Restore Database...". This dialog / wizard will do exactly what you are asking - simply select the source .bak file(s) / Database that you want to restore from, enter the name of the database you want to restore to and hit "Ok".
Some notes - if you enter the name of a database that doesnt yet exist (it sounds like this is what you want to do), it will create that database for you. If you enter the name of an existing database it will attempt to restore to that database. If you attempt to restore to an existing database from a backup made of a different database it will fail, however you can force the Sql Server to overwrite the existing database by going to "Options" and checking the "Overwrite the existing database" checkbox.
Also, if you are restoring a backup of an existing database to create a new second copy of that database you may find that the wizard fails as its attempting to create a database using the same database file paths as the ones currently in use by the source database. To fix this you need to click on "Options" and change all of the "Restore As" file paths to files that dont yet exist.
You can also get this wizard to generate an SQL script instead of actually performing the actual restore (click on the "script" button at the top), which is handy if you want learn how to do this sort of thing in raw SQL instead.
It is possible to restore a database from sql server 7, sql server 2000 to sql server 2005.
It can be achieved by using restore and with replace command
use master
restore database mydatabase from disk ='c:\mybackup.bak' with replace
It is easy to take a backup in sql server using this script without any external tools.
Check out Restore with Example.
drop database b first. then do the restore.

Find out when a database backup was made

Folks,
Assume you receive a disconnected backup of a SQL Server database (2005 or 2008) and you restore that to your SQL Server instance.
Is there a way, is there a system catalog or something, to find out when the last write operation occured on that particular database? I'd like to be able to find out what day a particular database backup was from - unfortunately, that's not really being recorded explicitly anywhere, and checking all dozens of data table for the highest date/time stamp isn't really an option either....
Any ideas? Sure - I can look at the date/time stamp of the *.bak file - but can I find out more precisely from within SQL Server (Management Studio) ??
Thanks!
Marc
If you have access to the SQL Server instance where the backup was originally run, you should be able to query msdb:
SELECT backup_set_id, backup_start_date, backup_finish_date
FROM msdb.dbo.backupset
WHERE database_name = 'MyDBname' AND type = 'D'
There are several table relating to backup sets:
backupfile -- contains one row for each data file or log file backed up
backupmediafamily -- contains one row for each media family
backupmediaset -- contains one row for each backup media set
backupset -- contains one row for each backup set
By querying these tables you can determine when the last backups occurred, what type of backups occurred and where the files were written to.
You can try RESTORE HEADERONLY on your backup file, as described here
that should give you the information you're looking for.
A bit late, but should be what you want.
Each write to the database is an entry in the log file. Which has an LSN.
This must be stored in the backup for log restores at least.
So, how to match LSN to a datetime?
SELECT TOP 5 [End Time] AS BringFirst, *
FROM ::fn_dblog (NULL, NULL)
WHERE [End Time] IS NOT NULL
ORDER BY BringFirst DESC
I've never used this before (just had a play for this answer). Some writes are very likely part of the backup itself, but you should be able to distinguish them with some poking around.
as far as I know in the master database there exists a Log-table where every write is stored with detailed information. BUT I'm unsure if you need to enable the Log-mechanism - so that the default is not to log and you have to enable it.
In Oracle for example it is the way around there exists a system-database table Log that you can query.
If that is not the case - you could still write yourself a trigger and apply that on every table/column needed and do the logging yourself.