I have this SQL Job (in SQL Server 2005) that creates a backup every six(6) hours, the backup's filename is based on the timestamp so that it will create a unique filename(dbname_yyyymmddhhmmss.bak), Now my question is, How would I know using xp_cmdshell if the file is three day old and based on my script I want to delete backup(.bak) that is a three day old. Can someone out there help me, thanks in advance. Cheers!
I agree that xp_cmdshell is not the best alternative for the job. If you're like me and you don't like/trust maintenance plans, you can probably write a C# console application, where file system support is much stronger than what you can do in DOS (or using T-SQL to parse the output of xp_cmdshell 'DIR ...'), and then schedule that in a windows scheduled task so that you don't have to worry about escalation of privileges from the SQL Server service/proxy account. While it's nice to put everything in one package, you don't always want the guy who changes your oil to make you a quiche.
This is not really the answer to your questions, but you could do this directly in SqlServer 2005 with a Maintenance Plan (Object Explorer -> Management -> Maintenance Plans).
I usually create one Maintenance Plan including two tasks: One "Maintenance Cleanup Task" which deletes old backups after x days, followed by a "Back Up Database Task".
This is not really a task that is suited to xp_cmdshell. Enabling this feature within SQL Server also has security implications.
What you are looking to achieve would be much better suited to SQL Server Integration Services (SSIS). There are components available that can be used to manage and perform your backups, as well as File System Task components that can be used to move and delete data.
You could use a combination of a File System Task, variables and expressions in order to retrieve the backup filename, extract the date component and determine how old the file is. You can then take appropriate action on the file.
I hope this helps but please feel free to pose further questions if you require additional information.
Cheers,John
You could write a .NET assembly and call it from within SQL Server. It would be fairly easy to write it so that a table valued function returns all of the files in a certain directory with filename and file datestamp.
Related
I have tried to read up on this topic and I am still a bit unclear how to proceed. This seemed like a fairly basic task but it has been nowhere as simple as I had assumed. I have several SQL queries written and I want to be able to schedule them to run on a certain day each month and then automatically be exported to a .csv file in a selected folder. This will then allow them to be automatically uploaded into a BI and reporting tool that our firm uses (this part I know how to take care of).
I am fairly well versed in the writing of SQL queries, but everything beyond that I am pretty lost on. Right now I am using Microsoft SQL Management Studio 17. I thought that maybe scheduling jobs using the SQL Server Agent would be the solution, but the more I read about that and go down that path, the less I am convinced that it will allow me to export the query results into the .csv file that I need for it to be picked up. It is also important that these results are exported without headers.
Does anyone have any solutions for this? I am happy to answer any follow up questions if I am at all unclear.
You can create a job within the SQL server management studio to handle the whole thing.
http://social.msdn.microsoft.com/Forums/en/transactsql/thread/7d2280cf-3b33-46f7-ba82-4131e8a841c0
I need to have a T-SQL statement that will delete files older than a day in a directory, but only if they match a certain naming convention. There are multipul backup files in this directory, but only one set we need to keep for a day. I'm unable to use a maintenance cleanup task because it will clear out all bak files, instead of only the ones I want.
Will using the dbo.xp_delete allow the user of the wildcard character %?
You should be able to do this through a SQL Server Maintenance Plan. Look at the "Maintenance Cleanup Task". You can clean up files using a variety of criteria, including age and filename.
This task uses the EXEC xp_delete_file statement.
This feature doesn't seem well documented at all. From what you can read in the comments here you get the general idea of the function:
http://sqlblog.com/blogs/andy_leonard/archive/2009/03/11/xp-delete-file.aspx
I would suggest you write a clean-up task outside of the sql server. This can be easily done with a bat-script or a Powershell script. You can execute these scripts with windows tasks or, in case of a Powershell script, even from within SQL Server itself.
Our team was releasing a new version of our system yesterday and we came across some issues with stored procedures. To cut a long story short we had to upload the old stored procedures to fix the issues.
I have now been given the task to automatically back up the stored procedures for our database before we release a build. I have went through a lot of sites and I've looked at generating scripts, making batch files, doing whole backups and scheduling tasks etc but none of these solutions would automatically backup only the stored procedures.
Any help in this case would be greatly appreciated thanks in advance.
Best Regards
Ryan
In Management Studio, right click on your database in the Object Explorer window, go to Tasks -> Generate Scripts... and follow the wizard.
You need to use SMO libraries to create your scripts and use them in command line batch files. Read more in http://msdn.microsoft.com/en-us/library/ms162153.aspx
Before run de script generator, set Option Continue scripting on Error, otherwise script will not be gereated.
If option DROP and Create is chosen, set option Script Object-Levels permission for stored procedures
Is your software source code checked into source control? It might be of benefit if your database is as well. This is the method software has used to manage versions and releases for years, and its about time DB's joined the party.
I suggest you look into a database project (available in the current 2015 free version of SQL Server Data Tools), which is a way of checking your objects in and out of a repository etc. It's a more complete way of managing database objects and fits into the software lifecycle. You can release your database codebase in conjunction with your software codebase and manage it all in one.
I was wondering if there is a way to automatically append to a script file all the changes I am making to my columns, tables, relationships etc...
The thing is I am doing a lot of different changes on a TEST db and the idea will be to apply this change script when I move the test db to production... hence keeping production data but applying all schema and object changes.
Is there an easy way to do this? Can it also migrate database diagram changes?
I have seen how you can create a change script each time I do a change but this means I have to copy and paste into a master file. Actually pretty easy!
I was just wondering if I was missing something?
Do not make changes to the test server using the UI. Write scripts and keep them under source control. You can test your scripts starting from backups of the live data and you can tune yoru scripts untill they achieve the desired result. Then you can check in the scripts for reference and later apply them on the live server. See this article Version Control and Your Database.
BTW, check out the SSMS toolpack, I think it may do what you want (I'm not sure). My advice stand none the less: version your schema, use explicitly created/saved scripts, use source control.
There's no way to directly generate a "delta" script in SSMS.
However, if every time you publish changes, you script out the entire database, including data, to SQL using the SQL Server Database Publishing Wizard you should be able to extract diffs between the versions and get your deltas that way.
If money is no object, you can purchase Visual Studio Team System Database Architect edition and use its fantastic database comparison tools to generate and version control exactly the diffs you want.
Try using TableDiff , that came with SQL Server 2005.
SQL Server 2005 TableDiff Utility
tablediff Utility
We have the process where when a developer gets done with a change, they then script it out and check it into Subversion. In Subversion we have a folder for Tables, Stored Procs, Data, etc. They script it out so it is repeatable (i.e. don’t insert the new data if it is already there.) This is important to do anyway so you keep the history of changes for a given object in the database.
In the past, we would just enter each of the files that we wanted scripted out into a text file (i.e. FileListV102.txt). When we were ready to make a release we would do “get latest” on all of the files (from VSS back then.) We then had a simple utility that would read the “file list” file and open each of those files in turn concatenating them into an output file. That is pretty easy to code.
We outgrew that and now we have a release management tools (which can be found here and will be on sale mid September), that takes all of the files and creates a big SQL script file out of it. It does it in the order that you would expect based on the folder names – so files found in the "Tables" folder are done before those in the "Data" folder, etc.
Either way, once you are done you have a big SQL script file that you can then apply to a fresh copy of production and that is what you test against.
I know I'm way late to the party, but I just wanted to add that there are tens of third party products out there. Some are very good, some are very cheap or free, and some are a mixture. I listed 22 here:
http://bertrandaaron.wordpress.com/2012/04/20/re-blog-the-cost-of-reinventing-the-wheel/
We have been using a relatively new software called Kal Admin.
It has Change Management feature and let distributing selected changes to other databases very easily. We used to do it by comparing two databases but it not satisfy our need for change tracking.
BTW Kal Admin has Metadata and data compare capabilities as well.
I've been looking at the SqlPubWiz.exe command to write a batch file so that I can keep my script up to date in my source control. But what I need is for the command line tool to allow me to pick specific tables to include (and that I can exclude others).
I think SqlPubWiz.exe won't do that for me (let me know if I'm wrong) but if someone can point me to another tool that can do it, that's what I'm looking for.
There are several commercial tools out there that can create database scripts, e.g.
ApexSQL's SQL Script
EMS DB Extract
Here's an article showing off a free tool - however, it will only script ALL objects from your database: Eric Moreau's blog.
If you want to "roll your own", have a look at the Server Management Objects (SMO) - those allow you to inspect your database and create scripts from them.
See info here, here or here.
Marc
Try DBSourceTools. http://dbsourcetools.codeplex.com
Its open source, and specifically designed to script databases - tables, views, procs to disk.
It also allows you to select which tables, views, db-objects to script.