guneysus
12/28/2014 - 5:32 PM

Linux Tested Backup Script. Not Incremental.

Linux Tested Backup Script. Not Incremental.

#!/usr/bin/env bash
# One line to give the program's name and a brief description.
# Copyright (C) 2014 Ahmed Seref Guneysu
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

TMP=$*
if [ "$TMP" = "" ]; then
    TMP="$HOME /etc /root /var/www /var/lib/mysql /home/developer"
fi

for i in $TMP; do
    if [ -d "$i" ]; then
        SOURCE_ROOT="$SOURCE_ROOT $i"
    fi
done

DEST_ROOT="/media/ahmed/2090F2C590F2A088/__BACKUPS"
DEST_ROOT_GZ="/media/ahmed/2090F2C590F2A088/__BACKUPS/tgz/"


# TODO
# Get device id
# blkid
# df -h
# sudo blkid -p /dev/sdc1 -o udev
# sudo blkid -p /dev/sdc1 -o udev | egrep "ID_FS_UUID=" # | xclip

# DEVICE_ID=`sudo blkid -p /dev/sdc1 -o udev | egrep "ID_FS_UUID="|sed "s/ID_FS_UUID=//g"`


# Create mount point for device and mount
# DEST_ROOT="/media/seagate/__BACKUPS"
# DEST_ROOT_GZ="/media/seagate/__BACKUPS/tgz/"

L0DATESTAMP=$DEST_ROOT/.level0_datestamp

LOGDIR="$DEST_ROOT/LOGS"
LOGFILE=$LOGDIR/`date +"%Y-%m-%d_%s"`.log

if [ ! -d ${LOGDIR} ]; then mkdir -p ${LOGDIR} ; fi
if [ ! -d ${DEST_ROOT}  ] ; then mkdir -p ${DEST_ROOT}   ; fi

touch $LOGFILE
alias DATE='date "+[%F %T]"'

COMPRESS=1
COMPRESSLOG=1
COMPRESSLEVEL=-fast

####### Redirect Output to a logfile and screen - Couldnt get tee to work
exec 3>&1                         # create pipe (copy of stdout)
exec 1>$LOGFILE                   # direct stdout to file
exec 2>&1                         # uncomment if you want stderr too
tail -f $LOGFILE >&3 &            # run tail in bg


hr () {
	printf '%*s\n' "${COLUMNS:-80}" '' | tr ' ' -
}

# -------------------------- HOME ETC ROOT -------------------------------------
#
echo ;hr ; echo "`date "+[%F %T]"` BACKUP STARTED"

# Create archive filename.

echo "`date "+[%F %T]"` BACKING UP [$SOURCE_ROOT] -> [$DEST_ROOT]"
for BACKUPFILES in $SOURCE_ROOT
do
      WITHOUTSLASHES=`echo $BACKUPFILES | tr "/" "."`
      WITHOUTLEADINGDOT=`echo $WITHOUTSLASHES | cut -b2-`
      OUTFILENAME="$WITHOUTLEADINGDOT.tar"
      OUTFILE=$DEST_ROOT/$OUTFILENAME

      OUTFILENAME_GZ=$WITHOUTLEADINGDOT.`date +"%Y-%m-%d_%s"`.tar.gz
      OUTFILE_GZ=$DEST_ROOT/$OUTFILENAME_GZ
      tar --preserve-permissions --verbose --create \
          --exclude=/home/*/.local/share/Trash/* \
          --exclude=/home/*/.Trash/* \
          --exclude=/home/.Trash-*/* \
          --exclude=$HOME/.bup/* \
          --exclude=$HOME/.instant/* \
          --exclude=$HOME/.PyCharm30/system/caches/* \
           --exclude=$HOME/Dropbox/.dropbox.cache/* \
           --exclude="$HOME/VirtualBox VMs/*" \
           --exclude=$HOME/opt/* \
           --exclude=$HOME/.cache/chromium/* \
           --exclude=$HOME/.cache/mozilla/* \
           --exclude=$HOME/.cache/MonoDevelop-*/* \
          --exclude-caches \
          --file $OUTFILE \
          $BACKUPFILES

      gzip --stdout -$COMPRESSLEVEL --verbose \
          $OUTFILE > $OUTFILE_GZ
      mv $OUTFILE_GZ $DEST_ROOT_GZ
      rm $OUTFILE
done


echo "`date "+[%F %T]"` BACKUP COMPLETED" ; hr
# ------------------------------------------------------------------------------
gzip --best $LOGFILE  > /dev/null 2>&1

# TODO pip packages list
# TODO apt-get packages