Quantcast
Channel: samsalisbury.net » Sam
Viewing all articles
Browse latest Browse all 3

RepositoryHosting.com backup script

$
0
0

We all know the importance of backups, and backups of backups, but even some of the most respected developers occasionally fail to keep their stuff backed up, as we recently learned… So here’s my tiny contribution to the subject, specifically for anyone using the amazingly low-cost RepositoryHosting.com for their SCM (it supports Git, Mercurial and SVN) and project management (by way of Trac).

Whilst RepositoryHosting.com already offers automatic copying of backups to an Amazon S3 bucket, it’s always best to not rely on a single backup location, as many stories confirm. In this light, I whizzed up the following script which you can use to pull your backups down to your local workstation or office file server. It’s a bit rough around the edges, and doesn’t handle errors, so you will have to check the results after each run, but it sure beats logging in and manually downloading the files through your browser each day… (Thanks to Matt White at RepositoryHosting.com for changing the backup URLs to make them more predictable, and suggesting the initial idea that spawned this script.)

This script relies on a Bash shell with cURL available, like the one that comes with msysgit if you’re using Windows. You’ll also need to make sure you set up daily backups in your RepositoryHosting.com project’s settings.

I’m no expert on Bash shell scripting, so if you can think of any improvements (to make the script more concise or handle errors etc.) then please feel free to educate us all in the comments!

Here’s the script… (I named it “backup.sh”). See the comments in-line for configuration instructions.

#!/bin/sh
# RepositoryHosting.com backup download script
#
# This script downloads your daily RepositoryHosting.com backups.

##################################################################
##
### Configuration
##

# Subdomain of your repository
RepoSubdomain=mysubdomain

# Local backup directory
BackupDir=~/Backups/RepositoryHosting.com

# RepositoryHosting.com administrator credentials
Username=myadminusername
Password=mypassword

# List of project names (for naming the backups, the order of these
# equate to the project IDs in your RepositoryHosting.com account.)
ProjectNames=(
	MyProject
	CoolProject
	OtherProject
)

# Backup date formats (e.g. "+%Y%m%d" for YYYYMMDD, or "+%d" for DD)
# NOTE: This affects the number of backups that will be kept, e.g.
# "+%d" will store a month's worth of backups, each numbered as the
# day of the month when it was created, "+%Y%m%d will keep an infinite
# number of backups, as each one will be uniquely dated.
#
# NOTE: This is used only if you specify @Date in the filename format.
#
FileDateFormat="+%d"

# Subdirectory date format (same as above, used only if you specify
# @Date in your SubDirFormat).
#
#SubDirDateFormat="+%m"

# Backup subdirectory format. If specified will place your backups into
# a separate subdirectory for each project.
#
SubDirFormat="@ProjectName"

# Backup file name format (e.g. "@ProjectName.@Date").
# This will automatically be prepended with the $BackupDir/$SubDir/ and
# appended with ".tar.gz".
#
FileNameFormat="@ProjectName.@Date"

##
### End configuration (no need to edit past here)
##
##################################################################

# Resolve URLs
RepoBaseUrl="https://$RepoSubdomain.repositoryhosting.com"
SessionStartUrl="$RepoBaseUrl/session"
ProjectsBaseUrl="$RepoBaseUrl/projects"

# Messages
echo -e "\nDownloading today's backups \n\tFrom:\t$RepoBaseUrl"\
"\n\tTo:\t$BackupDir"

# Cookie file name
CookieFileName="cookies.txt"

# Create backup directory if it doesn't exist
mkdir -p "$BackupDir"
# Go to backup directory
cd "$BackupDir"

# Login
echo -e "\nLogging in..."
curl -sS -X POST $SessionStartUrl -d \
"username=$Username&password=$Password" -c $CookieFileName -o /dev/null

# Copy project backups
ProjectNumber=0
while [ "x${ProjectNames[ProjectNumber]}" != "x" ]
do
# Specify & create backup subdirectory for this project
ProjectBackupSubdir=\
"${SubDirFormat//@ProjectName/${ProjectNames[ProjectNumber]}}"
ProjectBackupSubdir=\
"${ProjectBackupSubdir//@Date/`date $SubDirDateFormat`}"
test "x$ProjectBackupSubdir" != "x" && mkdir -p $ProjectBackupSubdir && \
ProjectBackupSubdir="$ProjectBackupSubdir/"
# Resolve backup filename
ProjectBackupFile=\
"${FileNameFormat//@ProjectName/${ProjectNames[ProjectNumber]}}"
ProjectBackupFile=\
"${ProjectBackupFile//@Date/`date $FileDateFormat`}.tar.gz"
# Copy project backup
echo -e "\nCopying ${ProjectNames[ProjectNumber]} backup to "\
"\n\t$ProjectBackupSubdir$ProjectBackupFile"
curl -L "$ProjectsBaseUrl/$[ProjectNumber+1]/backups/`date +%Y/%m/%d/00`" \
-b $CookieFileName -# \
-o "$BackupDir/$ProjectBackupSubdir$ProjectBackupFile"
ProjectNumber=$[$ProjectNumber+1]
done

# Delete cookies.txt
rm $CookieFileName

echo -e "\nFinished downloading backups!"

If you want to launch this easily in Windows, you can use a simple batch file…

REM Start the RepositoryHosting.com backup download script
C:
"\Program Files (x86)\Git\bin\sh.exe" -login -c /c/Users/Sam/backup.sh

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images