Jun272008

Simple backup script for Ubuntu

Trying to prevent myself from another home fiasco, I’ve decided to build a simple script to backup some of my directories. There are a lot of scripts / backup tools out there, but I just wanted to build my own for the fun of it, and since what I really wanted was to create a tar.gz of the contents of a specific directory (no root path), including hidden files, and without the need to support incremental backups. Take a look at the script:

#!/bin/bash

function showHelp() {
	if [ "$1" != "" ]; then
		echo "ERROR: $1"
		echo ""
	fi

	echo "Usage: $0 <folder> <file.tar.gz>"
	echo "where:"
	echo "	<folder>	folder to backup"
	echo "	<file.tar.gz>	where to save the backup (.tar.gz file)"
}

if [ $# -ne 2 ]; then
	showHelp
elif [ ! -d "$1" ]; then
	showHelp "Directory $1 does not exist"
	exit 1
else
	echo -n "Creating $2 from $1: "

	if [ -f "$2" ]; then
		mv "$2" "$2.previous"
	fi

	tar -cpzf "$2" --exclude="$2" --ignore-failed-read --transform="s/"`echo "$1" | sed -e 's/^\\///g' | sed -e 's/\\/$//g' | sed s/\\\//'\\\\'\\\//g`"//" "$1" &>/dev/null

	if [ $? -ne 0 ]; then
		echo "ERROR"
		exit 1
	else
		echo "DONE"

		if [ -f "$2.previous" ]; then
			rm -f "$2.previous"
		fi
	fi
fi

The line that probably looks scary is:

tar -cpzf "$2" --exclude="$2" --ignore-failed-read --transform="s/"`echo "$1" | sed -e 's/^\\///g' | sed -e 's/\\/$//g' | sed s/\\\//'\\\\'\\\//g`"//" "$1" &>/dev/null

The complicated part comes from the transform expression, which is used to get rid of the absolute path to the directory we are backing up. The tar –directory option here is not useful since it adds the “.” special path to the packaged file, so let’s break it down:

  • echo "$1": the folder to backup
  • sed -e 's/^\\///g': remove starting slash, if any
  • sed -e 's/\\/$//g': remove trailing slash, if any
  • sed s/\\\//'\\\\'\\\//g`: convert forward slash to escaped slashes (so every / is replaced by \/)


Leave a Comment

1 Comment to "Simple backup script for Ubuntu"

  1. Jun282008 at 8:01 am

    Dieter_be [Visitor] wrote:

    Hi, here are some tips for you.
    You can use other delimiters then ‘/’ in sed. So that if you plan to use slashes in your regex, you don’t need to escape them. I usually delimit with ‘#’ in this case.
    So the command to strip trailing slashes becomes this : sed ‘s#/$##’
    Note that ‘-e’ is not needed, it’s the default for sed. Also you don’t need the ‘g’ pattern modifier.
    Knowing this, you can make your regexes a lot easier ;-)

    Also check ‘man test’, you’ll see that you can rewrite [ "$1" != "" ] into [ -n "$1" ]

    You don’t need to echo “” for a newline, just echo without args, or add “\n” in your previous echo and use the -e flag.

    Also you might want to redirect your error messages to stderr.

    There is more stuff that can be improved… I suggest you go check out the bash tutorials at tldp.org. they are pretty good ;-)

 
Powered by Wordpress and MySQL. Clauz's design for by Cricava