Powershell, get me job status!

March 24th, 2011 jianhou 2 comments

Ever since I heard of Powershell, I have been wanting to learn it and make use of it. Windows’ own powerful command-line scripting, how much more geekier can it get for a geek? When the need to automate some of my daily/weekly tasks became necessary due to the increasing number of instances I have to monitor, I decided to finally learn powershell.

That is a story for another day, I am not going to expound on how to use Windows Powershell, you have better resources than my post; Windows Powershell In Action. Instead, I will show one of the automation project that gave me some difficulties when I first used powershell.

Many sites and books show you how to retrieve job status from multiple servers using powershell. My approach will take on the following :-

1) Retrieve job status from all servers

2) Insert them into a table in a centralized database

3) Create a job or use Reporting Services to export the report to my email every morning

Step 1 and 2 will have to be performed using a powershell-based job in a centralized instance. This instance will have to be using a SQL Server 2008 version since there is no support for powershell jobs in SQL Server 2005. Now, the biggest problem faced by me was the fact that all the servers may be sitting in a different domain.

You can’t use windows authentication to connect to the servers, and so those login credentials will have to be stored in the centralized database itself and retrieved when needed. “Hold on”, some of you may say, “that is a huge security risk!”. While I agree that it is most definitely not best practice to do this, some of you may find that there are no other options and it is really necessary to do this. There may be a better way to do this, if you know of it, please do do contact me and correct me.

Right, now let me show you the script before going in-depth.

# Include Files
. C:\PowershellScripts\OfficialScripts\centralMonitorConfig.ps1
. C:\PowershellScripts\OfficialScripts\fn_get_servers.ps1 

# Load SMO extension
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.Smo”) | Out-Null;

# Get List of sql servers to check
$dt = getServers $centralserver $centraldatabase “Select server_name, login_usr, login_password from server_instance”

foreach($row in $dt)
{
#Set server connection parameters
$sqlserver = $row.server_name
$usr = $row.login_usr
$pwd = $row.login_password

# Create an SMO Server object
$srv = New-Object “Microsoft.SqlServer.Management.Smo.Server” $sqlserver;

#This sets the connection to mixed-mode authentication
$srv.ConnectionContext.LoginSecure=$false;

#This sets the login name
$srv.ConnectionContext.set_Login($usr);

#This sets the password
$securePassword = ConvertTo-SecureString $pwd -AsPlainText -Force
$srv.ConnectionContext.set_SecurePassword($securePassword)

# SQL Connection String
$conn = New-Object System.Data.SqlClient.SqlConnection(“Server=$centralServer;Database=$centralDatabase;Integrated Security=SSPI”)
$conn.Open() # Open SQL Connection

# For each jobs on the server
foreach($job in $srv.JobServer.Jobs) {
$jobName = $job.Name;
$jobLastRunOutcome = $job.LastRunOutcome;
$jobLastRunDate = $job.LastRunDate;
$reportDate = Get-Date;

# Set up SQLCommand object
$cmd = $conn.CreateCommand()

# Configure TSQL
$cmd.CommandText =“INSERT INTO [dbo].[Job_Status] ([server_ip], [job_name], [job_last_run_date], [job_last_run_outcome], [report_date]) `
VALUES (‘$sqlserver’, ‘$jobName’, ‘$jobLastRunDate’, ‘$jobLastRunOutcome’, ‘$reportDate’)”

$cmd.ExecuteNonQuery() | out-null
}
# Close SQL Connection
$conn.close();
}

Please bear in my mind, this is just a sample script. If you are going to use this in a production environment, please include all the necessary error-handling and encrypt your stored login credentials.

OK, into the details.

# Include Files
. C:\PowershellScripts\OfficialScripts\centralMonitorConfig.ps1
. C:\PowershellScripts\OfficialScripts\fn_get_servers.ps1

These few lines include some standard configuration values and functions.

CentralMonitorConfig.ps1 – Sets the name of the centralized instance and database that stores the retrieved job status

fn_get_servers.ps1 – Accepts the server name parameter and retrieve the login credentials

# Load SMO extension
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SqlServer.Smo”) | Out-Null;

# Get List of sql servers to check
$dt = getServers $centralserver $centraldatabase “Select server_name, login_usr, login_password from server_instance”

Then we load the necessary SMO object and use the aforementioned function, fn_get_servers to retrieve the server login.

foreach($row in $dt)
{
#Set server connection parameters
$sqlserver = $row.server_name
$usr = $row.login_usr
$pwd = $row.login_password

The server information retrieved from the function will be passed to an object variable, $dt and a loop will run through each server in the list, retrieving the login credentials for each server.

# Create an SMO Server object
$srv = New-Object “Microsoft.SqlServer.Management.Smo.Server” $sqlserver;

#This sets the connection to mixed-mode authentication
$srv.ConnectionContext.LoginSecure=$false;

#This sets the login name
$srv.ConnectionContext.set_Login($usr);

#This sets the password
$securePassword = ConvertTo-SecureString $pwd -AsPlainText -Force
$srv.ConnectionContext.set_SecurePassword($securePassword)

Next, we create the SMO object for the server and pass the login credentials into the ConnectionContext property of the server. Before that, we ensure that the mixed mode authentication is set. We also convert the password to a secure string before passing it into the server object.

# SQL Connection String
$conn = New-Object System.Data.SqlClient.SqlConnection(“Server=$centralServer;Database=$centralDatabase;Integrated Security=SSPI”)
$conn.Open() # Open SQL Connection

Here, we open the connection to the centralized instance for inserting the information.

# For each jobs on the server
foreach($job in $srv.JobServer.Jobs) {
$jobName = $job.Name;
$jobLastRunOutcome = $job.LastRunOutcome;
$jobLastRunDate = $job.LastRunDate;
$reportDate = Get-Date;

We have reached  the meaty part, this is where we loop through all the jobs in each of the server in our list, and retrieved the job status information that we required. We also used a built-in function to retrieve the current date for reporting purpose.

# Set up SQLCommand object
$cmd = $conn.CreateCommand()

# Configure TSQL
$cmd.CommandText =“INSERT INTO [dbo].[Job_Status] ([server_ip], [job_name], [job_last_run_date], [job_last_run_outcome], [report_date]) `
VALUES (‘$sqlserver’, ‘$jobName’, ‘$jobLastRunDate’, ‘$jobLastRunOutcome’, ‘$reportDate’)”

$cmd.ExecuteNonQuery() | out-null

We then create a SQL command object and pass the T-SQL insert query into it. Finally we told it to execute and do not display any message.

}
# Close SQL Connection
$conn.close();
}

At last, we complete the job retrieval loop and server retrieval loop and close the connection.

That’s it! All’s left is to create the job in the centralized instance and it will insert the data into the defined table as required by your schedule. As mentioned earlier, you can even create a custom report and schedule it to be sent to your email. Don’t forget to create a data archival job to clear old records from the table or else those job status will pile up.

How to Restore Copy Only Backup

March 22nd, 2011 jianhou No comments

This can be seen as a continuation of the post How To Perform Ad Hoc Backup. It is also something that came as quite a surprise to me.

It all happened when we were creating a stored procedure for the L1 support guys to perform a copy_only backup of the database. That way, they won’t break up the backup chain when they are required to perform a backup of the database for testing, training, etc.

Few hours back, we had finished setting up the job to run the stored procedure when the junior DBA called me. “The support guys just called me. They said they can’t restore the backup file they created using the job we set up for them. Maybe there is something wrong with the stored procedure”. “What do you mean there’s something wrong.” I barked, “We tested it just fine”. “Well, they said they can’t restore it, they can’t select the backup set in Management Studio.”

That did it for me, and I immediately fired up the stored procedure in my laptop, right click -> Restore Database, select the backup file and found this!

There is no GUI support for restoring backup sets made with copy_only option!!

I was flabbergasted. I checked SQL Server 2008 and yes, I can see the backup set. What this means is that there is no GUI support for copy only backup AND restoring the copy only backup set in SQL Server 2005. Further investigation led me to this link.

Bottom line, the trick is to use the T-SQL RESTORE command like a normal restore operation.

Just wanted to share this in case anyone is faced with the same mystery.

Categories: Backup & Recovery Tags:

Anyone looking for a Production DBA?

December 10th, 2010 jianhou 2 comments

No, that is not a rhetorical question, I am seriously asking if anyone in the country is looking for a production DBA. Because, as far I am concerned, there is a lack of awareness on the importance of having a production DBA in medium-sized companies with a substantial investment in computer systems and infrastructure.

Most of the time, this task falls into the responsibilities of the system administrator or the software developer. The system administrators because they are the ones usually handling the database servers and the programmers because they are the one who wrote the stored procedures and indexes.

Can you believe how risky this is? You paid millions for the hardware and thus you get a qualified system engineer to maintain it for you. You wanted a good system that can help your users, thus you paid good money for people to create it. But once the data is stored in the database, you couldn’t care less and assign the job to the first person that you see? We are talking about someone who needs to make sure the data stored is secured, maintained and available 24/7.

Why is there such a lack of respect and recognition for the Production DBAs? Most of the time, headhunters and companies will call me up and enquire if I would be interested in a Software Engineer/Progammer/Developer post. I would then replied I only specialised in maintaining and administering SQL Server databases. At that point they will go, “Oh, we need you to be proficient in SQL Server too, just that we need you to know and program C#, C++, Java, VB as well”. So you need a developer AND DBA at the cost of one, that’s great, but I need my weekends too, thank you very much but no thanks.

Some of the job ads are the culprits as well. For example, I ran into this ad just a few days ago where the following requirements are stated:-

  • Experience in maintaining SQL Server and responsible for backups/restoring.
  • Managing the company’s data warehouse, knowledge in ETL processes helpful.
  • Experience in Open Source – LAMP, Apache, PHP/MySQL
  • Must be Microsoft Certified – MCSE, MCTS, MCDBA

There’s no many things wrong with this job ad that I don’t even know where to begin. You need to be proficient in maintaining SQL Server, and to be an expert in BI. Even in database realm, that is already two different disciplines.

To top that off, you need to handle their Open Source environment and also have the time to get yourself certified as a Microsoft Professional!

The way I see it, production DBAs do not get the respect they deserve just because there is no tangible evidence of their good work. If you are a really good DBA, your datasabase server would be running like a clockwork, with everything automated and set up in tandem with a comprehensive alerting system. It means things are quiet, and people will be wondering what exactly is the production DBA doing and why they are paying so much money for him.

I for one, am not going to give up. I am glad that I chose this career path. It is a most enjoyable career. It is immensely satisfying to be able to tune and maintain the database to make it run smoothly, and to watch your servers performance metrics go through the roof after you applied a well-thought out and tested tweak. It is my passion  Hopefully one day, the industry will learn to accept and appreciate a production DBA as an integral part of their team. And when it does, you know where to look for one.

Categories: Others Tags:

MCTS in the bag

December 10th, 2010 jianhou No comments

Finally! I was able to achieve my MCTS in 70-432:SQL Server 2008, Implementation & Maintenance, passing the exam last week. I have been wanting to take this exam for quite some time, for reasons stated here.

However, I kept delaying it again and again and again…….till last week, I decided enough is enough and before I changed my mind, log on to Prometric, set the appointment and paid the exam fees (Thanks to certification packs, go get ‘em here!)

It is a computerised test so the Prometric website states that no pen, paper or anything else is required except two pieces of your identification. In the case of Malaysians, our MyKad and driving license (or birth certificate for those too young to drive? Wow, a SQL prodigy!)

I had to admit I was a bit disappointed with the testing center I picked. I arrived 30 minutes earlier as advised by Prometric, but had to wait till 15 minutes after the appointed time to start the exam. The computer booth where I sat for the exam was in a small room where my back was facing a glass wall. Just outside the wall is a frequently used passage way and the people walking by were noisy, either talking or stomping too loudly.
To top that off, other people frequently entered the room, presumably for their exams too. I just wished there had been a little more privacy and peace for the exam room. I am not going to mention the testing center’s name, I have already submitted my comments to them, hopefully they will improve. Definitely my next test site will not be here again.

What to expect from the exam? The exam started by first guiding you on how to use the exam software by asking some general questions. Then it displayed the agreement and NDA for the exam, why is why I can’t mention anything else about the exam other than it’s a multiple-choice test ranging from 45 questions to 60.

You can mark the exam questions for review and skip the questions if you want to come back to it later. Frankly speaking, I thought I have failed the exam. There were 8 questions which totally stumped me and 12 more of which I was not sure of the answer.

Imagine my relief when I saw that I passed the exam (the results are calculated on the spot). I just wished that we can go back and review the questions to see which one we answered correctly or wrongly. In the end, they just printed a report that highlighted the areas that you did well, or badly. I scored highest in Performance, hmmmm, I never would have guess it.

So, for those of you still hesitating, go forth and just take the exam. As for me, one’s in the bag and MCM, here I come!

How To Perform Ad Hoc Backup

October 20th, 2010 jianhou No comments

Few months back, I ran into a situation that has completely slipped through everyone’s radar. How often do you encounter an issue so critical that it undermines all your strategy and planning, and yet nobody was aware of that issue? I found the issue hiding amongst my random thoughts (eureka!) and was amazed that it completely slipped my attention. To this day, I am still kicking myself over my complete idiocy and stupidity.

The issue I was referring to was the performing of ad hoc backups of the database. Too often, someone from the Business Analyst team will be requesting backups of the database, for testing patches and troubleshooting software issues. Too often, the people in charge of the requests would be the IT Support Engineers on site. Since most of them only have a basic knowledge on SQL Server, too often they would use the SSMS to perform the backup, right click database, task, backup, bla bla..

Now, what this did to our backup strategy was very damaging, it was breaking up our carefully planned backup cycle. Our strategy includes a mix of full, differential and log backups. A differential backup is a backup consisting of all the changes since the LAST FULL backup. By allowing ad hoc backups like previously described, it is breaking up the chain since the last full backup is not the one scheduled by you in the backup plan, it is the one taken for the business analyst! Hopefully the backup file has not been deleted in the event of a disaster that requires you to restore using the full and differential sets. The person restoring the database will no doubt be seeing this :-

Msg 3136, Level 16, State 1, Line 1
This differential backup cannot be restored because the database has not been restored to the correct earlier state.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

Basically SQL Server is telling you that there is a later full backup set than the one you are trying to restore to. Unfortunately you won’t find the backup set anywhere in your backup tapes or in your disk drive because it is an ad hoc backup. What you can do is to find out what time is the backup taken via this :-

SELECT backup_start_date, type, first_lsn, database_backup_lsn
FROM msdb.dbo.backupset
WHERE database_name = ‘TestRestore’

When you run the above query, you will see the history of the backups done and can determine which is the actual base for your differential backup.

From here, you can see that the actual base for the differential (150000000755300000) is actually the one done at 9.53 AM (the yellow highlighted ones). The LSN highlighted in red is the one you are trying to restore to and it’s giving the error.

So how do we obtain the latest backup for the users who require it? If you want to be strict on this, disallow ad hoc backups of any form. Use the existing backup sets and educate the team that ad hoc backups can be potentially damaging to the recovery plan.

Another method would be to use BACKUP WITH COPY_ONLY, a feature available from SQL Server 2005 onwards. This can be done via Management Studio or using T-SQL.

Using Management Studio. Check the Copy Only Backup option. (Only SOL Server 2008)

Using T-SQL. Specify the COPY_ONLY option in the WITH.

BACKUP DATABASE TestRestore
TO DISK = ‘F:\DB Backup\TestRestoreDB_copyonly.bak’
WITH
COPY_ONLY

Another point worth mentioning here is that we won’t lose anything if we can’t use the differential backups. All we have to do is to restore the last scheduled full backup and all the log backups since then. Yes, that will solve the issue but that will be missing the point of the differential backups. You will have to restore a heck lot of log backups without the differentials. And what if the database is in SIMPLE recovery model?

The summary here is; don’t allow ad hoc backups and if you must, use the COPY_ONLY option.

Categories: Backup & Recovery Tags:

Dvorak – Better than QWERTY?

October 19th, 2010 jianhou No comments

Whew, it’s been a long time since I posted here. Waayyy too long in fact. What could be the cause? Procrastination? Laziness? It might have been partly due to those factors but the single overriding reason was that shortly after the last post, I switched to Dvorak.

Say what? For the uninitiated, Dvorak is an alternative layout for the keyboard that was purportedly designed to be more efficient, more comfortable and easier to learn than the traditional QWERTY layout.  The creator of this layout, Dr. August Dvorak, achieved this by studying the most common words and letters in the English language and re-arranged the keys so that the more common keys are easier to reach.

For example, he placed the vowels on the left side and the most common consonants on the right, thereby ensuring most words will be an equal balance of left-hand typing vs right hand typing. Try typing ‘minimum’ on a QWERTY keyboard and you will see what I mean.  Supposedly studies have also shown that 70% of most English keystrokes can be typed on the “home row” whereas only 32% of the QWERTY layout can be done on the home row. The home row is the base position of your fingers, the keys you would be resting your fingers on if you are a touch typist.

Personally, I am a sucker for anything that promotes itself to be a more efficient way of doing things compared to the traditional ways.  I thought I would take on this experiment to adopt Dvorak for a month to see if it’s really an improvement over the QWERTY. I did not predict any drastic effects this will have on my life as I used the same laptop for work and personal.  To learn the new layout, I went to this site ABCD : Lesson Overview.

The first week was horrendous! My QWERTY speed was 70++ wpm before switching and it dropped to 20 wpm after I switched. It was painfully slow, like you are in a dream state, where you know you can run faster but somehow you seem to be moving too slowly. Heck, even typing QWERTY is hard now. That is why I have not posted anything since; it will take hours to write the article!

It’s been 5 weeks since I switched and my speed is back to a respectable 50 wpm. What can I say about Dvorak now? Frankly I still can’t pass my judgment on it now, not till I reach back my old typing speed. It does seem to be more comfortable but it could be psychological. My pinkies seem to do more work though, especially the right one, where the S and L are located.

For those of you who are interested, you can always give it a test drive to determine if it’s right for you. You can go to the lessons I linked to above or alternatively go here to learn more.

Oh, one more thing, I just loved it when someone tries to type on my laptop and it comes out all gibberish. I told them my keyboard is encrypted and my fingers are the encryption certificates!

Categories: Others Tags: ,

5 ways to jumpstart your DBA career

September 8th, 2010 jianhou No comments

1. Start reading up

I am an avid reader, so this comes naturally for me. I am always on the lookout for good SQL books. Reading each book only serves to show me how little I know and how deep the subject matter truly is. This leads to a hunger for more knowledge and kept me reading and learning more.

Books are not the only resource as the Internet is full of resources: SQL community websites, blogs, newsgroups and more. Books Online is always a great reference point for anything but it can be a bit of a dry reading. The best online resources are articles by SQL Server experts who have garnered a lot of experience and they are willing to share that knowledge with us.

2. Build a Lab

Reading can only teach you so much. I find learning most effective when the theories are strengthened with real hands-on experience. Of course it would be best if you are given the chance to experiment in real-life scenarios, but I doubt your boss would allow you to test your lessons in the production environment.

This is where the lab comes in. It would be immensely useful to set up your own lab just to test out various SQL Server configurations and 3rd party tools that you were thinking of using in production but were afraid to do so.

It doesn’t have to be an expensive rack setup of blade servers and SAN, you are only testing the features and configurations after all. I am running mine on a desktop running VirtualBox; three virtual machines, one DC server and two cluster nodes, and one laptop as client.

3. Take the Microsoft certification exam

If you are interested in database administration and management, take the 70-432 exam. If you are interested in development, take the related exam and vice versa for Business Intelligence. Sure, some of you may argue that having a Microsoft certification doesn’t reflect accurately on the depth of your knowledge on the subject. But the facts are:-

FACT 1:- Potential employers do look at your certifications. In fact, most of the recruiters would include a necessity for Microsoft Certification in their list of required qualifications.

FACT 2:-I find it very useful as the exam forces you to focus on all aspects of the product and gives you a guideline on what to study to improve your ability as a SQL Server DBA. Besides, it’s a challenge to yourself to acquire higher levels of certification as your knowledge grows.

4. Join the SQL Community

Reaching out to the SQL Community is a great way to learn and to give something back to the community. You could not possibly accumulate all the knowledge you require by yourself, and therefore you seek to share and exchange knowledge with others, it’s a win-win situation.

For those living in the US, they have PASS and it’s a very vibrant community for all SQL Server enthusiasts. For the rest of us, we will have to make do with whatever local SQL community there is in our area. For fellow Malaysians, we do have a local SQL community group here; known as SPAN (SQL Server Practitioners Alliance). It is not as mature as PASS but things seem to be picking up lately with events scheduled every month. They also invite guest speakers from India and Australia for some of these events.

If you could not find a local community nearby, that’s too bad but not all is lost though. You can be just as active in the online communities by joining the forum; contributing ideas and articles, helping people out with their difficulties, posting your own problems and others. Heck, you could even take the initiative to start a local SQL community group if you feel the need to socialize the old-fashioned way.

5. Start a blog

Starting a blog on SQL Server serves many purposes. First, you get to build your own personal brand, and that will be a plus point for you when potential employers are skimming through your CV. It demonstrates that you have a passion for the technology and you are willing to do something about it.

Second, you get to write articles on various SQL server topics that you may have learned in the past. By imparting your knowledge to others, you will actually learn even more, another win-win situation. I realized this when an IT junior came to me for help on some SQL queries. I thought I knew the answer until I started to open my mouth to teach him. It hit me like a truck, “I don’t really know enough about this subject!”. Knowing something is very misleading, how well do you really know the subject? Teaching someone will really demonstrate the extent of your knowledge.

For those who are afraid of starting a blog because of thoughts like “Who am I to teach about SQL Server, there are thousands of experts out there. What can I possibly know that these experts do not know?”, then I have an ancient Chinese saying for you, 一山还宥一山高. Translating this will mean that no matter how much you think you know, there is always something that you don’t know! Even experts will seek out other experts to find out what they do not know and you may be holding that knowledge in your head. And besides, why are you starting blog thinking about them? What about the ones who are just beginning? What about the ones on the same level as you?

No matter what level of expertise you are at, you will always have information that might be useful to someone out there. Why not share it? If people find it useful, good, if not they will move on to another blog, no harm done. With all that said, start a blog now! Brent Ozar (Twitter | Blog) has written a great article on how to get started and he is the one who has inspired me to start a blog. His articles have helped me a lot, not only on blogging but on SQL. One day, you can be like him too, but take that first step now.

Categories: Others Tags:

Hello World!

August 27th, 2010 jianhou No comments

Hello World indeed! No, I have not forgotten to remove the first default post by WordPress. What better greeting to start the blog by Hello World!

What I hoped next is for the world to come into MY world, an imperfect one where I will begin to post down my lessons and mistakes from being a SQL Server DBA in my work. Why? Why go to a new blog by a SQL noob? Why not go to one of the more prominent SQL bloggers? By all means, they are the masters of the SQL World, the rock stars, and even I read their blogs as my daily ritual.

My goal is not to compete with them. My goal is to learn more on SQL Server. By writing on SQL Server, I have to ensure my facts are accurate, and by that, I have to do a lot of research. And by that, I learn.

And if people comes across my blog, and goes ‘OK, that was useful for me’, the blog will have achieve one of it’s purpose. Blogging is one of the things that can help to develop your career as a SQL Server DBA. In my next post, I will talk more about this.

And so, to the world I say, let us begin.

Categories: Others Tags: