Monday, April 14, 2008

The importance of backups; the dangers of "rm -rf"

One can't truly appreciate the importance of backups until it saves you from certain doom. I can now say that I personally have been rescued by my backups.

It all started innocently enough - I wanted to wipe out my build directory and start clean again. However, in this case, my build directory happened to have the same name as my Git repository. My Git repository is located in /Users/joe/devel/sipx/git_main. Meanwhile, my build directory is located in /Users/joe/devel/sipx/build/git_main.

Some of you are already shaking your heads, knowing what comes next. Without double-checking my current directory, I typed:
$ rm -rf git_main

All right, now it's time to start a new build. I'll just go into my project root directory...
$ cd ~/devel/sipx/git_main
-bash: cd: /Users/joe/devel/sipx/git_main: No such file or directory


My stomach churned.
$ pwd
$ /Users/joe/devel/sipx

I had just trashed my project root!

After about 30 seconds of panic - I don't have permission to push my changes to our official repository, so I had lots and lots of changes that hadn't been submitted - I remembered that just the previous Friday, I had started backing up my Git repository to a WD Passport USB hard drive. This was first thing Monday morning, and I hadn't made any new changes yet.

So, thankfully, I was able to restore my entire Git repository:
cp -R /Volumes/WD\ Passport/Projects/sipx/git_main .

My woes didn't end there, unfortunately. For compatibility across my different computers I use, the Passport was formatted as a FAT32 partition. The file permissions were all messed up, and apparently my sync software was too smart for its own good - it had changed the newline markers so now all the files appeared changed.

So, when I tried to rebase my branch against the master branch, I got hundreds of errors about "suspicious patch lines" and it wouldn't continue. My stomach started to churn again, but before giving up and tendering my resignation, I searched Google for this error message.

I found a blog post with a strangely appropriate domain name: www.dont-panic.cc, entitled Git on Windows: “You have some suspicious patch lines”. The solution was to edit .git/hooks/pre-commit and comment out the following lines:
if (/\s$/) {
bad_line("trailing whitespace", $_);
}


Although I am running a Mac, the trick still fixed my problem. After that, my rebase was successful and I was back in business.

So, remember: Always make backups, and double-check your working directory before you wipe out its contents!

2 comments:

Karrde712 said...

Well, the old adage always holds true. There are only two kinds of people: those who back up regularly and those who haven't lost anything important yet.

On our Subversion repository, I've taken to creating a new branch for every project or bug I'm working on, and I check in my changes (complete or not) at the end of every day. Since the SVN server is backed up daily, weekly and monthly as well as having the ability to roll back bad changes, I don't really have to worry anymore about losing anything critical.

For the record, though, I started doing this because I had a hard drive failure a couple weeks ago. Fortunately, I'd had the foresight to set my working directory up as a RAID-1 mirror, so the other hard drive still had everything, but that was more a lucky accident than anything else (the PC I was given happened to have enough HDs that I could do that)

C R Krieger said...

Way too complicated for me. But, I have taken to making backups of my important projects, either on an external Hard Drive at work or on three memory sticks (two for each of two separate projects and one for general stuff). This also helps me move between my office PC and my MAC. But, backing up is important. I am working on a separate project at my Parish and building a database for the Secretary. Today I updated the Access database (at my level of geekdom I am just bragging there) and forgot to back it up to the memory stick I bought for the purpose and as soon as I left the building I thought--should have backed it up.

Regards -- Cliff