Rsync záloh

Spouští rsync na připojený vzdálený adresář přes SSHFS. Dále vyhodnotí úspěšnost a pošle report.

rsync-backup.sh
#!/bin/bash
#
# Author: Jiri Blazek (blazek@isn.cz)
# App: RSYNC backup from VPS
#
 
# absolute path home
APH=$(dirname $(readlink -f $0))
 
# sshfs settings
SSHFS_PORT=2244
SSHFS_FROM="backupdown@example.com:/"
SSHFS_TO="/backup/sshfs-store/"
 
# rsync settings
RSYNC_FROM="/backup/sshfs-store/"
RSYNC_TO="/backup/store"
 
# email report content
REPORT_RECIPIENT="blazek@isn.cz"
REPORT_SUBJECT="[INFO] RSYNC BACKUP (VPS) - success"
REPORT_CONTENT_FILE="$APH/report-file.txt"
 
# report file
echo "RSYNC BACKUP (VPS)" > $REPORT_CONTENT_FILE
echo "" >> $REPORT_CONTENT_FILE
 
# check process
IS_ERROR="0"
 
function log() {
 echo $(/bin/date +"%Y-%m-%d %k:%M:%S") $@
}
 
is_not_mounted()
{
  RETVAL="0"
  /bin/mount | /bin/grep $1 1>/dev/null 2>/dev/null
  if [ $? -eq 0 ]; then
    RETVAL="1"
  fi
  return $RETVAL
}
 
mount_sshfs()
{
  is_not_mounted $SSHFS_TO
  if [ $? -eq 0 ]; then
    /usr/bin/sshfs -p $SSHFS_PORT $SSHFS_FROM $SSHFS_TO
    if [ $? -eq 0 ]; then
      log "[INFO] directory $SSHFS_TO is mounted" | tee -a $REPORT_CONTENT_FILE
    else
      log "[ERROR] directory $SSHFS_TO mount failed" | tee -a $REPORT_CONTENT_FILE
      IS_ERROR="1"
    fi
  else
    log "[WARNING] directory $SSHFS_TO is already mounted" | tee -a $REPORT_CONTENT_FILE
  fi
}
 
umount_sshfs()
{
  is_not_mounted $SSHFS_TO
  if [ $? -eq 1 ]; then
    /bin/umount $SSHFS_TO
    if [ $? -eq 0 ]; then
      log "[INFO] directory $SSHFS_TO is umounted" | tee -a $REPORT_CONTENT_FILE
    else
      log "[ERROR] directory $SSHFS_TO umount failed" | tee -a $REPORT_CONTENT_FILE
      IS_ERROR="1"
    fi
  else
    log "[WARNING] directory $SSHFS_TO is already umounted" | tee -a $REPORT_CONTENT_FILE
  fi
}
 
rsync_backup()
{
  #/usr/bin/rsync -ae --delete $RSYNC_FROM $RSYNC_TO
  /usr/bin/rsync -a $RSYNC_FROM $RSYNC_TO
  if [ $? -eq 0 ]; then
    log "[INFO] rsync from $RSYNC_FROM to $RSYNC_TO success" | tee -a $REPORT_CONTENT_FILE
  else
    log "[ERROR] rsync from $RSYNC_FROM to $RSYNC_TO failed" | tee -a $REPORT_CONTENT_FILE
    IS_ERROR="1"
  fi
}
 
# mount sshfs, rsync, umount sshfs
mount_sshfs
if [ $IS_ERROR -eq 0 ]; then
  rsync_backup
fi
umount_sshfs
 
# report
echo "" >> $REPORT_CONTENT_FILE
if [ $IS_ERROR -eq 0 ]; then
  echo "RSYNC BACKUP (VPS) = success" | tee -a $REPORT_CONTENT_FILE
else
  echo "RSYNC BACKUP (VPS) = failed" | tee -a $REPORT_CONTENT_FILE
  REPORT_SUBJECT="[ERROR] RSYNC BACKUP (VPS) - failed"
fi
 
# send report by e-mail
/bin/mail -s "$REPORT_SUBJECT" $REPORT_RECIPIENT < $REPORT_CONTENT_FILE