You were never here, you didn't see anything..

USB Backup


This is the little script I use to back up my thumb drive every so often. Simple and to the point. Takes the specified mount point and rsync’s, tars the gzips it all. Has a bunch of extra lines for the initializeANSI() function. Makes it simple to add colors to shell scripts.


#!/bin/bash

# /=================================================\
# |   Simple back up script.  Note the comments.    |
# | I know they are encrypted, but you can do it :) |
# \=================================================/

# Get the current time in seconds, for timining numbers:
BEGIN="$(date +%s)"

# ANSI Color -- use these variables to easily have different color
#    and format output. Make sure to output the reset sequence after
#    colors (f = foreground, b = background), and use the 'off'
#    feature for anything you turn on.

initializeANSI()
{
  esc="^["

  blackf="${esc}[30m";   redf="${esc}[31m";    greenf="${esc}[32m"
  yellowf="${esc}[33m"   bluef="${esc}[34m";   purplef="${esc}[35m"
  cyanf="${esc}[36m";    whitef="${esc}[37m"

  blackb="${esc}[40m";   redb="${esc}[41m";    greenb="${esc}[42m"
  yellowb="${esc}[43m"   blueb="${esc}[44m";   purpleb="${esc}[45m"
  cyanb="${esc}[46m";    whiteb="${esc}[47m"

  boldon="${esc}[1m";    boldoff="${esc}[22m"
  italicson="${esc}[3m"; italicsoff="${esc}[23m"
  ulon="${esc}[4m";      uloff="${esc}[24m"
  invon="${esc}[7m";     invoff="${esc}[27m"

  reset="${esc}[0m"
}

# Set up color stuff :) :
initializeANSI

# Source driectory (What to back up),
SRC=/media/usb

# Target location (Where to back it up):
TARGET=$HOME/backup

# Numeric identifier, in case we have multiples:
NUM="0"

# Name for the backup, this will be te dir and then
# file name when completed:
NAME=usb-`date +%F`

# gzip options:
GZOPTS="-9"

# Check for a backup of the current day, if we
# have one increment it:
if [ -e "$TARGET/$NAME.$NUM.tar.gz" ];
then
	# We need to know what the next one should be:
	for i in `ls $TARGET/$NAME.*`;
	do
		if [ `ls $i | cut -d "." -f 2` -ge $NUM ]
		then
			ADD=`ls $i | cut -d "." -f 2`
			NUM=`expr $ADD + 1`
		fi
	done
fi

# Test to see if we are mounted:
if test -w /media/usb;
then
	echo ${greenf}"USB drive is mounted"${reset}
	CODE=1
else
	echo ${yellowf}"Mounting USB drive"${reset}
	mount /media/usb
	CODE=2
fi

# Use rsync to create a copy of the contents,
# If you place a "/" after the $SRC variable
# It gets the contents. If not uses the dir:
echo ${greenf}"Creating backup"${reset}
rsync -a $SRC/ $TARGET/$NAME.$NUM

# tar it all up:
cd $TARGET
echo ${greenf}"Taring $NAME.$NUM"${reset}
tar -cf $NAME.$NUM.tar $NAME.$NUM &> /dev/null

# gzip the tar ball
echo ${greenf}"Compressing $NAME.$NUM.tar"${reset}
gzip $GZOPTS $NAME.$NUM.tar

# Remove the working dir:
echo ${greenf}"Cleaning up"${reset}
rm -rf $NAME.$NUM

# Make sure the USB drive is where we found it:
if test $CODE = 2;
then
	echo ${yellowf}"Unmounting USB drive"${reset}
	umount /media/usb
fi

# Get the resulting file size:
SIZE=`ls -sh "$NAME.$NUM.tar.gz" | cut -d " " -f 1`

# Generate an md5sum of the backup:
md5sum $NAME.$NUM.tar.gz > $NAME.$NUM.md5
MD5=`cat $NAME.$NUM.md5 | cut -d " " -f 1`

# Get the time now since we are finished, again for timing:
FINISH="$(date +%s)"

# Calculate time taken by subtracting begin from finish:
TTIME="$(expr $FINISH - $BEGIN)"
# Fromat the diff to hour:min:sec
YEAR="$(date +%Y-%m-%d)"
RTIME=`date -d "$YEAR $TTIME sec" +"%H:%M:%S"`

# Let us know what was done:
echo "
${yellowf}Info :${reset}
${bluef}*************************************************${reset}
${greenf} File name    :${reset} $NAME.$NUM.tar.gz
${greenf} md5 =        :${reset} $MD5
${greenf} md5sum file  :${reset} $NAME.$NUM.md5
${greenf} Location     :${reset} $TARGET
${greenf} gzip options :${reset} $GZOPTS
${greenf} Size         :${reset} $SIZE
${greenf} Time taken   :${reset} $RTIME
${bluef}*************************************************${reset}
"

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This blog is kept spam free by WP-SpamFree.

Most who fail…

Most who fail have yet to really try!