Linux rsync backup script

HOW TO USE:

  1. edit following file and set DIR as the directory you wish to backup.
  2. save as backup.sh
  3. make exceutable with ‘chmod +x backup.sh’
  4. Run with ./backup.sh /mnt/mymedia    (where /mnt/mymedia is the location you wish to backup to
#!/bin/sh

# Enter absolute path of directory you wish to backup
# DO NOT FORGET TRIALING SLASH!!
DIR=/root/importantdir/

# ---------------------- DO NOT EDIT BELOW THIS LINE-------------------------- #

if [ $# -lt 1 ]; then
    echo "No destination defined. Usage: $0 destination" >&2
    exit 1
elif [ $# -gt 1 ]; then
    echo "Too many arguments. Usage: $0 destination" >&2
    exit 1
fi

START=$(date +%s)

DAY=`date +%d`
MONTH=`date +%m`
YEAR=`date +%Y`
TIME=$(date '+%A, %d %B %Y, %T')

TODAY="$YEAR""_""$MONTH""_""$DAY"

BKDIR="$1/$TODAY"
LOG=$BKDIR/backup_$TODAY.log

if [ ! -d "$BKDIR" ]; then
        mkdir -p $BKDIR
fi

echo "Backup on: $TIME n" > $LOG
rsync -aAXvh $DIR* $BKDIR | tee -a $LOG

FINISH=$(date +%s)

echo "total time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds" >> $LOG

 

You may also like...

Leave a Reply