20seven

Devigner blog about development and design

Backing Up Org-Mode Files

As many already know, I live in org-mode and keep my org files in a private bitbucket repository for safe keeping. One thing I don’t (or didn’t) do is push those files to the repository on a regular basis. The other morning I ate my wheaties and gained enough super power to rectify the issue.
h3. The bash script

I keep all my dotfiles in this uber-private repository, not just my org files. To continue with this approach I added a bash script within my dot files directory which will do my grunt work.
#!/bin/sh

  1. Add dot file changes to bitbucket – to include my org files
    REPOS=“hgfiles”

for REPO in $REPOS
do
echo “Repository: $REPO
cd ~/$REPO

  1. Add new files
    hg add .
    hg commit -m “$(date)”
    hg push
    done

    The variable “REPOS” holds the directories that I want pushed to bitbucket. If you have more than one, add them to this variable. Basically, the script just loops through the REPOS and adds any new files, commits them with a message of the current date and time and pushes it. If there are no new files it just fails silently and my world doesn’t implode on me.

If you are using github or a similar service, just change out the hg commands with the git commands. If you’re using svn… I’m so sorry to hear that.

The plist

Now since I’m horrible at pushing these on a regular basis and don’t have the money to buy a trained monkey to push them for me, I added them to Mac OS X’s launchctl. To do that it’s fairly simple.

I created a plist file which is also stored in my dot files directory.
cd to/the/directory touch greg.dotfiles.orghg.plist
Open this plist file in your favorite editor and add the following code.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>greg.dotfiles.orghg</string> <key>ProgramArguments</key> <array> <string>/Users/greg/hgfiles/management/hg.commit</string>

StartInterval 3600


ProgramArguments is the path to the bash script I created earlier so Mac knows where to find it. The StartInterval is the interval to run the script. In my case I have it push every hour. 3600 seconds divided by 60 equals 60 minutes. You get the picture.

Schedule it!

Now that this is saved it’s time to hook it up and test it. For testing purposes, I changed the StartInterval to 2 minutes (180) so I could watch the console for errors in the log.

The plist file needs to be copied to the proper directory and added to the daemons.
cp greg.dotfiles.orghg.plist /Library/LaunchAgents/greg.dotfiles.orghg.plist launchctl load /Library/LaunchAgents/greg.dotfiles.orghg.plist
All done and I can once again sleep knowing my org files are safe and sound within my repositories.

If you’re a linux user, you won’t need the plist parts, just create a cron job that will run the bash script every hour (or whatever your desired interval).