2012-06-03

backup script (NO, not data-backups, small configuration file backups)

Hello again,

after some time finally something usefull for more of you: a small bash script that came out of configuration work. The scenario is, you want to test a new configuration file, but want to backup the old or standard one. Sometimes it's a hole directory you want to exchange. And ofter some testing one has a couple of backup files, all with different names and naming conventions, somtimes maybe a date, sometimes a tag...
So I wrote the following script. Probably most admins do something like this and probably there are already a quadrillion tools out there, that do this or can do this. But I like to have a simple command that does exactly what I want without the need to supply the same options everytime, that I can't remember. So if you have a tool already, just make an alias. But here's the script, for everyone who wants to use it:



#!/bin/bash
# put it here for example: /usr/local/bin/bak.sh
# don't forget to chmod 755 it
if [[ (($# -ne 1) && ($# -ne 2)) || ($1 == "-h") ]]; then
        echo "usage: bak.sh <file or dir> <optional tag>"
        echo "files will just be copied"
        echo "directories will be archieved as .tgz"
        exit
fi
APPEND="bak-`date +'%Y-%M-%d_%H%M%S'`"
if [ $# -eq 2 ]; then
        APPEND="${APPEND}-${2}"
fi
if [ -d $1 ]; then
        APPEND="${APPEND}.tgz"
        echo "backing up ${1} to ${1}.${APPEND}"
        tar czf $1.$APPEND $1
else
        echo "backing up $1 to ${1}.${APPEND}"
        cp -p $1 $1.$APPEND
fi

No comments:

Post a Comment