rss.png profile for ebal on Stack Exchange, a network of free, community-driven Q&A sites
Jan
22
2010
Another bash script for backup with a snapshot logic.

In the last days i lost some (non critical) data cause i had forgot to install rsnapshot in my laptop. I was without network for a couple of hours and there was nothing on TV. So … i’ve played with bash and made a bash script to backup some directories with a snapshot logic.

abs4Snap

The bash script is really simple and straight-forward without any difficult parts. So you can easily configure it, to serve your needs.


#!/bin/bash
# Evaggelos Balaskas, <ebalaskas AT ebalaskas DOT gr>, 20090122
DIR="/var/cache/pacman/pkglist"
PATHS="/etc /usr/local/etc /boot"
BACKUP="backup"
PACMAN="/usr/bin/pacman -Qqe"
RSYNC="/usr/bin/rsync -ra"
MKDIR="/bin/mkdir -p"
DATE="/bin/date"
CP="/bin/cp -al "
MV="/bin/mv -f"
RM="/bin/rm -rf"
TOUCH="/bin/touch"
DAYS="10"

# Make Directory Structure
$MKDIR $DIR/`$DATE +%Y/%m/%d`

#Package List
$PACMAN > $DIR/`$DATE +%Y/%m/%d/pkglist_%H_%M`

#Rotation
if [ -d "$DIR/$BACKUP.$DAYS" ] ; then 
    $RM $DIR/$BACKUP.$DAYS
fi

for i in `seq $(expr $DAYS - 1 ) -1 1` ;do 
    if [ -d "$DIR/$BACKUP.$i" ]; then
        $MV $DIR/$BACKUP.$i $DIR/$BACKUP.$(expr $i + 1)
    fi
done

# Create Hard Link
if [ -d "$DIR/$BACKUP.0" ] ; then
    $CP $DIR/$BACKUP.0 $DIR/$BACKUP.1
fi

# Sychronization
for k in $PATHS; do
    $MKDIR $DIR/$BACKUP.0$k/
    $RSYNC --delete $k/ $DIR/$BACKUP.0$k
done

# TimeStamp
$TOUCH $DIR/$BACKUP.0