#!/bin/sh
# -------------------------------------------------------------------------------
# Grant's rsync script which backs up your home directory to a backup hard drive
#--------------------------------------------------------------------------------
# ---------------------------IMPORTANT!!!!--------------------------------------
# ---------------------------IMPORTANT!!!!--------------------------------------
#THESE VARIABLES NEED TO BE CHANGED TO FIT YOUR SYSTEM FOR THIS SCRIPT TO WORK!
#Locations:
DATA_BACKUP=/home/; #This is the data you want to be backed up, in my case it is my home directory
MOUNT_DEVICE=/dev/hdb2; #This is the device or folder where you want to back up your data. This is usually another hard drive or network location.
#USERS:
USER=sirgrant; #Change this to the user you want to own the log file and directory backed up.
#THESE VARIABLES NEED TO BE CHANGED TO FIT YOUR SYSTEM FOR THIS SCRIPT TO WORK!
# ---------------------------IMPORTANT!!!!--------------------------------------
# ---------------------------IMPORTANT!!!!--------------------------------------
unset PATH # suggestion from H. Milz: avoid accidental use of $PATH
# ------------- system commands used by this script --------------------
CHOWN=/bin/chown;
ECHO=/bin/echo;
ID=/usr/bin/id;
MKDIR=/bin/mkdir;
MOUNT=/bin/mount;
MV=/bin/mv;
RM=/bin/rm;
RSYNC=/usr/bin/rsync;
TOUCH=/usr/bin/touch;
UMOUNT=/bin/umount;
# ------------------------ file locations -----------------------------------------
MOUNT_LOCATION=/mnt/rsyncbackup; #This is the location where the mount device is mounted to on your system
#Ignore this, it is going to be used in a later version of this script
#SNAPSHOT_RW=/root/snapshot;
# ------------- variables used by this script --------------------
DATE=`/bin/date "+%m_%d_%y"`; #Used to append the date to the created log file
# ---------------------- the actual script ----------------------------------
# make sure we're running as root
if (( `$ID -u` != 0 )); then { $ECHO "Sorry, must be root. Exiting..."; exit; } fi
$ECHO ""
$ECHO "~*~*~*~*~*~*~*~*~*~*~*~*~*"
$ECHO "Backing up home directory, this may take some time depending on how long
it was since you last backed up..."
$ECHO "~*~*~*~*~*~*~*~*~*~*~*~*~*"
#Creates the mount point if it doesn't exist, if it does -p causes no error if existing
$MKDIR -p $MOUNT_LOCATION/
$MOUNT $MOUNT_DEVICE $MOUNT_LOCATION
#Creates directory for log files and data to be backed up if they don't exist
$MKDIR -p $MOUNT_LOCATION/Rsync\ Logs/
$MKDIR -p $MOUNT_LOCATION/Data\ Backup/
#Actual rsync backup command. Uses > to create a log file of what is copied.
#-v, -- verbose - increase verbosity
#-a, -- archive - archive mode (equals -rlptgoD (no -H, -A, -X)
#-h, -- human-readable - output numbers in a human-readable format
#-u, -- update - skip files that are newer on the receiver
#-r, -- recursive - recurse into directories
#-l, -- links - copy symlinks as symlinks
#-p, -- perms - perserve permissions
#-t, -- times - preserve modification times
#-g, -- group - preserve group
#-o, -- owner - preserve owner (super-user only)
#-D, -- same as --devices --specials
#-H, -- hard links - preserve hard links
#-A, -- acls - preserve ACLs (implies -p)
#-X, -- xattrs - preserve extended attributes
# --progress - show progress during transfer
# --delete - delete extraneous files from dest dirs
# --ignore-errors - delete even if there are i/O errors
# --devices - preserve device files (super-user only)
# --specials - preserve special files
$RSYNC -vuahH --progress --delete --ignore-errors $DATA_BACKUP $MOUNT_LOCATION/Data\ Backup/ > $MOUNT_LOCATION/Rsync\ Logs/"$DATE"_rsync.log
#Since we are running this script as root the log file that is created is owned by root, have to change it back to the user
$CHOWN $USER\: $MOUNT_LOCATION/Rsync\ Logs/
$CHOWN $USER\: $MOUNT_LOCATION/Rsync\ Logs/"$DATE"_rsync.log
$CHOWN $USER\: $MOUNT_LOCATION/Data\ Backup/
#Unmount devices
$UMOUNT $MOUNT_DEVICE
#Delete the folder created at the mount point after script completion
$RM -r $MOUNT_LOCATION
$ECHO ""
$ECHO "~*~*~*~*~*~*~*~*~*~*~*~*~*"
$ECHO "All done!"