25 July, 2007

HowTo make backup via tar.bz2

Tar (with bzip2 compression) is the best tool for making archive backups.
I wrote simple script, that creates backups of files or directories.
It creates backups with unique (depended on date) prefix.

E.g.: it creates file Xantorohara-2007.07.25_21.11.19.749614000.tar.bz2
from directory Xantorohara via command xqx_ubackup.sh Xantorohara



xqx_ubackup.sh

#!/bin/bash

if test -z "$1"; then
echo "Create backup archive with unique (depended on date) name."
echo "Usage: $0 <target file or folder> <(optional) path, where to put archive>"
exit
fi

DATE=`date +%Y.%m.%d_%H.%M.%S.%N`
if test -z "$2"; then
tar -cjf ./"$1"-$DATE.tar.bz2 "$1"
else
tar -cjf "$2"/"$1"-$DATE.tar.bz2 "$1"
fi

2 comments:

Anonymous said...

I'm not an expert, but I've heard it might be better to use -cpjf to preserve file permissions.

Clive said...

Thanks for that. another tool in the toolbox.