Ever came across a scenario where you want to back up your MSMQ messages and restore them back later!! Let me explain how this can be achieved. There is a command line utility which comes out of the box when you install MSMQ on your machine. This utility is Mqbkup. You can find the description about this utility in the below link. Below link also talks about the consideration you would have to take before running this tool on your server. http://technet.microsoft.com/en-us/library/cc771016(v=ws.10).aspx 

Now let me show you how I used this utility on my machine. I would also show some of the limitations you have with this utility.

  1. As you can see below I have two queues present in my MSMQ. 1 Transactional 1 Non-Transactional. And both have few messages present in them.

  2. Now let’s say I ran into some problem related to MSMQ and I fear losing my messages and I need to backup them. That’s when mqbkup comes into picture. I can use the mqbkup utility to back up my messages. It also backs up of your queue related information. I would show that later below.
          The only thing to consider is that mqbkup cannot take the backup of the Non-transactional (Express) messages.  This is just because mqbkup restarts the msmq service before it takes the backup and hence we lose the express messages.

  3. You have to use the below command to take the backup of MSMQ messages. 

                mqbkup -b "Folder location" –y

               -b is the switch to take backup-         

               -y Don't prompt (yes to all questions)

            

          As you can see in the above screen shot, mqbkup utility restarts the MSMQ service during the backup. It basically takes the backup of all the MSMQ Files  and the registry details as well.


           
         The good thing about this is that you can restore your queue related information as well in case you delete them. Mqbkup would restore these queues for you in case you had deleted them.  Let me show you how it works.

         To reproduce this, I purged and deleted my queues from the MSMQ console.

         

       Now when I would restore my MSMQ messages from the backup folder, mqbkup utility would re-create all the queues for me as it has all the MSMQ files backed up with it. I would also restore the transactional messages back which I had previously in my transactional queue.

      You would have to run the below command to restore MSMQ messages.

      Before restoring messages, you must close Control Panel and all Message Queuing applications or any application that holds open a registry key handle to the HKLM\Software\MSMQ\Parameters registry key. Such applications typically include the Computer Management console if the Message Queuing snap-in is loaded, the MSMQ      trigger service, the Message Queuing service, and some third party applications.

          mqbkup -r "Folder Location" –y
         

                  -r Switch for restore
                  -y Don't prompt (yes to all questions)

        

       As you can see in the scree shot above, mqbackup utility stops and starts the MSMQ service over here as well. It also deletes the MSMQ folders present in the MSMQ storage locations before restoring the new folders. And that’s why we would see the older queues getting created for us. You can also see in the below screen shot that I got all of my transactional messages back. You may also notice that we lost out express messages as well.

       

Let me also cover a scenario where mbbkup would not be useful. Let’s say you took a backup of MSMQ and now you added some new queues on this machine. In the above scenario, it would not be possible for you to restore the last backup and still keep the new queues. You would either be able to keep the newer queue or be able to restore the older one. This is just because your backup files don’t have information regarding the newer queues you had created. Now if you do a restore, the current MSMQ files would get replaced with the ones from the backup and hence you would not see the newer queues.

I hope this helps!!

Written By
Yakshit Gohel

Reviewed By
Jainath V R

Microsoft GTSC, India