User Tools

Site Tools


eric:computer:bash:shell_scripting:rsync-main.sh.2016.07.06

Title: rsync-main.sh.2016.07.06
Created: 2018-04-29 22:15:54 +0000

rsync-main.sh

# File:  rsync-main.sh
# Main script to do backups, called from other shell scripts
# Requires rsync version 3

echo Started at:
date
echo

RSYNC_PATH="/usr/local/bin/rsync"
ARCHIVE_NAME="$ARCHIVE_PATH`date "+archive-%Y-%m-%d-%H.%M.%S"`"
ERROR_LOG="$SCRIPT_HOME"/logs/"$BACKUP_NAME"-rsync-error.log
VERBOSE_LOG="$SCRIPT_HOME"/logs/"$BACKUP_NAME"-rsync-verbose.log
CURRENT_LOG_FILE="$SCRIPT_HOME/logs/current_log.txt"
COMPLETED_FILE="$DEST_PATH".latest-rsync-date
SYS_INFO="$DEST_PATH".system_info.txt

# remove old logs to avoid confusion (if no log is created, the old one would still appear)
# touch the log files first to create them if they don't exist so we do not get an error from rm
touch "$ERROR_LOG"
if [ "$?" = "0" ]; then
    # touch success
    rm "$ERROR_LOG"
else
    # touch failure
    echo "ERROR: Failed to write to file:  $ERROR_LOG"
    return 1
fi

touch "$VERBOSE_LOG"
if [ "$?" = "0" ]; then
    #touch success
    rm "$VERBOSE_LOG"
else
    # touch failure
    echo "ERROR: Failed to write to file: $VERBOSE_LOG"
    return 1
fi

echo This script requires RSYNC VERSION 3 OR GREATER, please verify below:
echo
if [ -f $RSYNC_PATH ]
then
    $RSYNC_PATH --version | grep version
    echo
else
    echo ** ERROR ** RSYNC DOES NOT EXIST AT STATED PATH: $RSYNC_PATH **
    return 1
fi

# if there is a sourse sparsebundle, let's mount it
if [ $SOURCE_SPARSEBUNDLE != "-" ]
then
    echo -n Mounting source drive: $SOURCE_PATH. . .
    hdiutil attach $SOURCE_SPARSEBUNDLE -verify -readwrite -autofsck
    if [ "$?" = "0" ]; then
        # success
        echo " (success)"
    else
        # failure
        echo
        echo "ERROR: Could not mount $SOURCE_SPARSEBUNDLE"
        return 1
    fi
    echo
else
    echo No source-side sparsebundle image to mount. . .
    echo
fi

# if there is a destination sparse bundle then mount it
if [ $DEST_SPARSEBUNDLE != "-" ]
then
    echo -n Mounting destination drive: $DEST_PATH. . .
    hdiutil attach $DEST_SPARSEBUNDLE -verify -readwrite -autofsck
    if [ "$?" = "0" ]; then
        # success
        echo " (success)"
    else
        # failure
        echo
        echo "ERROR: Could not mount $DEST_SPARSEBUNDLE"
        return 1
    fi
    echo
else
    echo No destination sparse bundle image to mount. . .
    echo
fi

# test writability of destination path
touch "$DEST_PATH/rsync-write.txt"
if [ "$?" = "0" ]; then
    # success
    rm "$DEST_PATH/rsync-write.txt"
else
    # failure
    echo "ERROR: Failed to write to destination:  $DEST_PATH"
    return 1
fi

if [ $ARCHIVE_SPARSEBUNDLE = "-" ]
then
    # -u = update mode, do not overwrite newer files (used when not archiving i.e. not keeping old files)
    ARCHIVE_FLAG="-u"
    echo archive off
    echo
else
    ARCHIVE_FLAG="--archive --delete --backup --backup-dir $ARCHIVE_NAME"
    echo archive on
    echo

    # mount the archive sparse bundle now
    echo -n Mounting rsync archive drive: $ARCHIVE_SPARSEBUNDLE. . .
    hdiutil attach $ARCHIVE_SPARSEBUNDLE -verify -readwrite -autofsck
    if [ "$?" = "0" ]; then
        # success
        echo " (success)"
    else
        # failure
        echo
        echo "ERROR: Failed to mount archive sparsebundle:  $ARCHIVE_SPARSEBUNDLE"
        return 1
    fi
    echo

    # Creates the archive directory so we can write the log file to it before rsync starts
    mkdir -p $ARCHIVE_NAME
    if [ "$?" = "0" ]; then
        # success
        echo "Created archive folder: $ARCHIVE_NAME"
    else
        # fail
        echo "ERROR: Could not create archive directory: $ARCHIVE_NAME"
        return 1
    fi

    # Notify where archives will go
    echo Old versions of changed files will be archived to the following directory: $ARCHIVE_NAME
    echo

fi

# Create tree directory listing
#TREE_FILE="$SCRIPT_HOME/trees/$BACKUP_NAME.txt"
#echo "Creating directory tree to file: $TREE_FILE"
#echo "Directory tree starting at: $SOURCE_PATH">$TREE_FILE
#echo "Created: `date`">>$TREE_FILE
#echo>>$TREE_FILE
#find "$SOURCE_PATH" -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'>>"$TREE_FILE"

# Notify if checksum is turned on
echo "Checksum flag setting is (blank if off):  "$CHECKSUM_FLAG
echo

# Notifiy archive flag
echo "Archive flag is set to: "$ARCHIVE_FLAG
echo

# Create error log file (overwrites previous if more than one backup today) and writes header line
echo "*** RSYNC ERROR LOG "$SOURCE_PATH" TO "$DEST_PATH" ***">"$ERROR_LOG"

echo
echo Rsyncing from $SOURCE_PATH to $DEST_PATH. . .
echo


#Output the log file name of the current verbose log so that the monitoring (tail) shell script will know what log to display
echo "$VERBOSE_LOG">"$CURRENT_LOG_FILE"

# do not use --copy-links because it will recursively copy things inside of apps
# -r = recursive
# -X = preserve extended attributes
# -x =  don't cross file system boundaries
# -l = copy symlinks as symlinks
# -N = --crtimes = preserve creation times on Mac (compile rsync with crtimes.diff patch)
# -H = --hard-links = preserve hard links
# --crtimes = must be patched and compiled in
# --fileflags = must be patched and compiled in
# --protect-decmpfs = does not work in 3.1.2
sudo $RSYNC_PATH \
    $ARCHIVE_FLAG \
    $CHECKSUM_FLAG \
    --links \
    --xattrs \
    --crtimes \
    --fileflags \
    --one-file-system \
    --hard-links \
    --recursive \
    --log-file="$VERBOSE_LOG" \
    --force-change \
    --delete-excluded \
    --protect-args \
    --stats \
    --exclude="/System/Library/CoreServices" \
    --exclude="/usr/bin" \
    --exclude="Application Support:Adobe:Acrobat" \
    --exclude=rsync \
    --exclude=MobileSync \
    --exclude=private/etc/cups/certs \
    --exclude=private/tmp \
    --exclude=private/var \
    --exclude=private/var/vm \
    --exclude=private/var/folders \
    --exclude=private/var/spool \
    --exclude=private/var/run \
    --exclude=private/var/db/Spotlight-V100 \
    --exclude=/etc \
    --exclude=/var \
    --exclude=/tmp \
    --exclude=tmp \
    --exclude=Volumes \
    --exclude=System/Library/DirectoryServices/DefaultLocalDB \
    --exclude='System/Library/User Template' \
    --exclude=*-Tmp- \
    --exclude=.DS_Store \
    --exclude=.hotfiles.btree \
    --exclude=.vol \
    --exclude='Temporary Items' \
    --exclude='.TemporaryItems' \
    --exclude='Entourage Temp' \
    --exclude=*.Trash \
    --exclude=*.Trashes \
    --exclude='Preferences/MS Internet Cache' \
    --exclude=.Spotlight* \
    --exclude=*.fseventsd \
    --exclude=*Feeds \
    --exclude=Network \
    --exclude=cores \
    --exclude=/dev \
    --exclude=/net \
    --exclude=.DocumentRevisions-V100 \
    --exclude='VISE Temp Items' \
       "$SOURCE_PATH" "$DEST_PATH" 2>>"$ERROR_LOG"
if [ "$?" = "0" ]; then
    # no errors
    echo "Rsync completed successfully"
    # tell monitor script to wait
    echo " ">"$CURRENT_LOG_FILE"
else
    # rsync errors
    echo "Rsync exited with errors, check the logs"
    # tell monitor script to wait
    echo " ">"$CURRENT_LOG_FILE"
    return 1
fi
echo

#append end message to error log
echo "*** END OF LOG ***">>"$ERROR_LOG"

touch "$COMPLETED_FILE"
echo "Noting this rsync time and date as the creation time for the invisible file: $COMPLETED_FILE"
echo

# Noting System Information for future reference
echo "Noting system information for future reference in the invisible file: $SYS_INFO"
echo

#system_profiler SPHardwareDataType SPSoftwareDataType | perl -e '@info= <>; $info= join("", @info);if($info=~m@[\r\n]+\s*Computer Name:\s*([^\r\n]+)[\r\n]+@) {print "$1\n";}if($info=~m@[\r\n]+\s*System Version:\s*([^\r\n]+)[\r\n]+@) {print "$1\n";}if($info=~m@[\r\n]+\s*Memory:\s*([^\r\n]+)[\r\n]+@) {print "$1\n";}if($info=~m@[\r\n]+\s*Total Number Of Cores:\s*([^\r\n]+)[\r\n]+@) {print "$1 x ";}if($info=~m@[\r\n]+\s*
#Processor Speed:\s*([^\r\n]+)[\r\n]+@) {print "$1 ";}if($info=~m@[\r\n]+\s*Processor Name:\s*([^\r\n]+)[\r\n]+@) {print "$1\n";}' > "$SYS_INFO"

#cat "$SYS_INFO"
#echo

#show errors (if any)
echo
cat "$ERROR_LOG"
echo

echo "*** Any error messages from the verbose log, including one preceding line and one trailing line of information, will be printed below ***"
grep -A 1 -B 1 rsync: "$VERBOSE_LOG"
echo "*** END OF VERBOSE LOG ERRORS ***"
echo

#show disk space free
df -h
echo

if [ $ARCHIVE_SPARSEBUNDLE != "-" ]
then
    # here if archive sparsebundle exists

    # Find the size of the archive folder
    ARCHIVESIZESPACE=`du -hs $ARCHIVE_NAME | cut -f 1`

    #Remove the leading space
    ARCHIVESIZE=`echo "${ARCHIVESIZESPACE// /}"`

    echo Archive folder size: $ARCHIVESIZE

    NEW_NAME=$ARCHIVE_NAME"_("$ARCHIVESIZE")"

    echo Renaming archive folder to: $NEW_NAME
    mv "$ARCHIVE_NAME" "$NEW_NAME"
    echo

    echo Unmounting rsync archive sparsebundle: $ARCHIVE_IMAGE
    hdiutil detach $ARCHIVE_IMAGE
    echo

    echo Releasing free space from rsync archive drive: $ARCHIVE_SPARSEBUNDLE. . .
    hdiutil compact $ARCHIVE_SPARSEBUNDLE
    echo

fi

# if there is a sourse sparsebundle, let's unmount it
if [ $SOURCE_SPARSEBUNDLE != "-" ]
then
    echo Unmounting Source sparsebundle:  $SOURCE_IMAGE
    hdiutil detach $SOURCE_IMAGE
    echo

    echo Releasing free space from source drive: $SOURCE_SPARSEBUNDLE. . .
    hdiutil compact $SOURCE_SPARSEBUNDLE
    echo

fi

# if there is a destination sparsebundle, let's unmount it
if [ $DEST_SPARSEBUNDLE != "-" ]
then
    echo Unmounting Destination sparsebundle: $DEST_IMAGE
    hdiutil detach $DEST_IMAGE
    echo

    echo Releasing free space from destination drive: $DEST_SPARSEBUNDLE. . .
    hdiutil compact $DEST_SPARSEBUNDLE
    echo
fi

echo "All automatically mounted disk images should be unmounted now, please verify /Volumes below to make sure no directories of the same names were created:"
echo
ls -al /Volumes
echo

echo Completed at:
date
echo
echo "*** END ***"

return 0
eric/computer/bash/shell_scripting/rsync-main.sh.2016.07.06.txt · Last modified: by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki