Author: Dirk Watkins

My Subversion Backup Script

In the name of all that’s good – backup your Subversion repository!

Your repo is the history of the code that runs your business.  Make sure you back that beast up.  You can safely back it up while developers are using it by running the dump command.

Take a dump

I like to script my dumps (backups) with a Windows batch script and then compress the output for long term storage. Here’s my batch script:

Requires:

@ECHO OFF
cd e:

set ts=%date:~10,4%%date:~4,2%%date:~7,2%
ECHO %ts%
set repoPath=E:\Repositories\Development
set tempPath=C:\SvnBackupStagingArea\DevRepoDump%ts%.svn
set backupPath=\\networkShareForBackup\DevRepoDump%ts%.svn.7z

ECHO Dumping SVN Repo 
svnadmin dump %repoPath% > %tempPath%

ECHO Zipping
cd "c:\Program Files\7-Zip"

set tempPathZip=%tempPath%.7z

7z a %tempPathZip% %tempPath%

ECHO Moving to network share
move %tempPathZip% %backupPath%

ECHO Cleaning up
del %tempPath%

ECHO Done.
ECHO Presented for you in it's smallest form, your backup: 
ECHO %backupPath%
pause

The script creates a nice timestamp (ts) to append to the backup filename – it’ll end up to be something like DevRepoDemp20131111.svn.7z when it’s done. This depends on how your dates are set up in Windows so you might need to change this or remove it.

It then sets the temporary staging path and the final zipped up dump path for where you want the result to end up. The server that has the repository should be where you stage the backup file, so it can backup and zip as fast as possible. Make sure you have at least 1.5 times more free space available than the size of the repository – it’ll need to create a dump and then zip that dump.

SVNADMIN DUMP creates a hot copy of the repository. This works even if developers are using the repo while you are backing it up – Those particular changes probably won’t be in this backup though. That’s why you should run this on a schedule, preferably at least once a day that people are not using the repo, like around lunch time :)

Note: If you are using TortoiseSVN, make sure you have also installed the command line tools with it. Check your machine for svnadmin.exe – if you do not have it, re-install TortoiseSVN and include the tools.

7-Zip will compress the backup copy. And yes, this makes a big difference in the resulting file size. Our repo was 10 gig and it zipped down to 3.5g.

It’ll then move the zip to a network share, deleting the temp file.

Download the zipped batch file

Let me know if this helped you out.

Rangle Up Variables with JavaScript Closures

Using global variables in JavaScript is bad and can tick off other developers.  Use Closures to group your variables with proper scope and stop watching your back in JavaScript User Group meetings.

Here’s what I mean:

When you make a variable or function in JavaScript by default it’s defined on the global object and it can be accessed from anywhere.  Now we all know global objects are BAD – The names can conflict with other libraries, increase memory usage, make debugging a huge pain, etc. In JavaScript, the answer is to “enclose” your variables in a function.  The cool cats call this a “Closure.”

Here’s what I do:

var DirksApp = DirksApp || {};

(function ()
{
     var privateVariable = "I'm Private!";
     this.publicVariable = "I'm Public to the World!";
}).apply(DirksApp);

console.log(DirksApp.publicVariable);    //prints: I'm Public to the World!
console.log(DirksApp.privateVariable);   //prints: undefined

Instead of creating privateVariable and publicVariable in the global namespace, I’ve enclosed it in an “object” called DirksApp.  By declaring privateVariable with var it is only accessible from the inside of DirksApp.  Declaring publicVariable on this makes it publicly accessible off of DirksApp.

Just remember:

  • var for private
  • this for public

The initialization var DirksApp = DirksApp || {}; and apply(DirksApp) pieces allow the same Closure to be used in multiple files and will combine the objects. You’ll appreciate this when your application gets bigger and you want to split the code up into module files. The JavaScript apply method also sets “this” inside the closure to DirksApp, that’s why applying a new function on “this” assigns it as a public function on DirksApp.

You may have to read that last paragraph again. I was confused even writing it.  And there’s definitely more to it, but this is a quick and dirty tutorial.  Look up JavaScript Scope and IIFEs (iffys) for more info.

So, please, stop polluting the global namespace. You can start by using Closures to make your JavaScript code a bit more maintainable and you’ll make more JavaScript friends in the process.

Special thanks to Paul Sanchez, @basicdays, who presented similar content for the Milwaukee JavaScript User Group and gave me the oomph to get this article out the door.

Notifying DBAs of New SharePoint Databases

In our production environment, the Database Administrators like to know when a new database is created or dropped. As well they should.  They normally use a DDL trigger to send out an email when one of these events occurs.  The problem we ran into was that in Windows SharePoint Services 3 (WSS3/SharePoint 2007), the trigger broke SharePoint’s ability to create new content databases!  Perhaps the account that ran the procedure didn’t have enough rights to run sp_send_dbmail, but we don’t know for sure.  If you know why, tell everyone in the comments.

The way we fixed it was to, instead, create a nightly agent job on the SQL Server that just looked for new database records in sys.databases.

SELECT name, create_date
FROM sys.databases
order by create_date desc

Just add a WHERE clause that filters the databases based on how often you run your job. If any databases exist since the last time you ran the job, send out an email.

How to Run SQL Server Management Studio as a Different Windows User

If you’re using Windows Authentication for your SQL Server database you’ll need to run SQL Server Management Studio (SSMS) as that Windows account to test queries.  SSMS normally runs as the user logged in to Windows, but there is a simple way to run it as the database access account.

Just shift-right-click on a shortcut and select “Run as a different user“.  (Windows 7)

Shift-Right-Click a shortcut to run the program as a different Windows User.
Shift-Right-Click a shortcut to run the program as a different Windows User. (Windows 7)

You’ll be presented with a Windows login prompt.  Log in and SSMS will open.  Note that the connection dialog has the new user in the Windows Authentication area

Connect to Server Dialog
Connect to Server Dialog

Note: You’re now running the program as a different user, so it probably won’t have access to the files you normally use.  My fix was to give the account read/write to the folder where I hold my SQL scripts.

This tip will work for other programs too.  Let me know how you used it in the comments below.

Turn Yeast into a Mosquito Killing Army!

Mosquitoes are drawn to CO2.  Yeast create CO2.  So, use yeast to draw mosquitoes into a trap.

I heard about this tip somewhere and thought I’d share it with you.  It cleverly uses yeast’s carbon-dioxide bi-product to trick mosquitoes into going into a bottle.  Since mosquitoes are also drawn to heat, you can paint the bottle black to make it extra temping.  This is a very natural and eco-friendly pest control.  You can just use some old bread yeast, although some spent beer-brewing trub (pronounced TROOBE) will work awesomely too.

Mosquitos Trapped in a 2-Liter Bottle

Items needed

  • 1 2-liter soda bottle
  • scissers
  • 1 cup of water
  • 1/4 cup of sugar (white or brown)
  • 1 teaspoon of yeast

Steps

  1. Cut the plastic bottle in half.
  2. Mix sugar with hot tap water. When cool, pour in the bottom half of the bottle.
  3. Add the yeast when the water is cool. No need to mix. Yeast eat sugar, and then creates carbon dioxide which attracts mosquitoes.
  4. Place the funnel part, upside down, into the other half of the bottle.  You can tape them if you want.
  5. (optional) Paint the bottle, or wrap it with something black, leaving the top uncovered.  The black will absorb more heat during the day.  Both the yeast and mosquitoes like it warm.
  6. Place the trap along the edges of your yard.

Maintenance (Every Few Weeks)

Add some more cooled sugar water every 2 weeks so the yeast has something to eat.  If there are a lot of dead mosquitoes, and or the liquid looks especially nasty, spray it out and start over.