Backup procedure: Difference between revisions

From Newroco Tech Docs
Jump to navigationJump to search
(Created page with "This page will describe how a backup procedure would look. ==On backup server== Install rsync <pre>apt-get install rsync</pre> Switch to root and create a private/public k...")
 
Line 26: Line 26:
if [ $# -ne 2 ] ; then
if [ $# -ne 2 ] ; then
     echo "Error: you need to provide 2 parameters, $# given."
     echo "Error: you need to provide 2 parameters, $# given."
     echo "Syntax: replication-nc.sh remote local"
     echo "Syntax: rsyncbackup.sh remote local"
     echo "Example: replication-nc.sh rsyncbackup@10.0.10.108:/home/yhamon/ /srv/yhamon-dev/ (mind the slashes)"
     echo "Example: rsyncbackup.sh backup-user@ip.add.re.ss:/path/to/source/ /path/to/destination/ (mind the slashes)"
     echo
     echo
     exit 1
     exit 1
Line 36: Line 36:


SOURCE=`echo $1/`
SOURCE=`echo $1/`
#SOURCE=${1}current
DEST=$2
DEST=$2


Line 45: Line 44:


if [ -d $INPROGRESS ] ; then
if [ -d $INPROGRESS ] ; then
     echo `date +"%F %T"` " : Directory $INPROGRESS already exists - is a previous replication still running?"
     echo `date +"%F %T"` " : Directory $INPROGRESS already exists - is a previous backup still running?"
     echo `date +"%F %T"` " : Directory $INPROGRESS already exists - is a previous replication still running?" | /usr/bin/mail -s "Replication of $SOURCE failed. Directory $INPROGRESS already exists - is a previous replication still running?" intsystems@newro.co
     echo `date +"%F %T"` " : Directory $INPROGRESS already exists - is a previous backup still running?" | /usr/bin/mail -s "Backup of $SOURCE failed. Directory $INPROGRESS already exists - is a previous backup still running?" example@email.com
     exit 1
     exit 1
fi
fi
Line 61: Line 60:
if [ $RSYNCERROR -ne 0 ] ; then
if [ $RSYNCERROR -ne 0 ] ; then
     echo `date +"%F %T"` " : Rsync to ${DEST}inprogress exited with non-null status. Exiting..."
     echo `date +"%F %T"` " : Rsync to ${DEST}inprogress exited with non-null status. Exiting..."
     echo `date +"%F %T"` " : Rsync to ${DEST}inprogress exited with non-null status. Exiting..." | /usr/bin/mail -s "Replication of $SOURCE failed" intsystems@newro.co
     echo `date +"%F %T"` " : Rsync to ${DEST}inprogress exited with non-null status. Exiting..." | /usr/bin/mail -s "Backup of $SOURCE failed" example@email.com
     exit 1
     exit 1
fi
fi

Revision as of 11:55, 23 January 2018

This page will describe how a backup procedure would look.

On backup server

Install rsync

apt-get install rsync

Switch to root and create a private/public key pair

sudo su -
ssh-keygen

On target server(s)

Install rsync

apt-get install rsync

Create a user for the backup script with the public key created before, more info here

Add entry in /etc/sudoers for the created user

visudo
backup-user ALL=NOPASSWD: /usr/bin/rsync

Back on backup server

Copy this script in /opt/bin/rsyncbackup.sh

#!/bin/bash

if [ $# -ne 2 ] ; then
    echo "Error: you need to provide 2 parameters, $# given."
    echo "Syntax: rsyncbackup.sh remote local"
    echo "Example: rsyncbackup.sh backup-user@ip.add.re.ss:/path/to/source/ /path/to/destination/ (mind the slashes)"
    echo
    exit 1
fi

CURRENTDATE=`/bin/date +"%F"`
OLDDATE=`/bin/date --date="2 days ago" +"%F"`

SOURCE=`echo $1/`
DEST=$2

echo "source=$SOURCE"

INPROGRESS=${DEST}inprogress
LATEST=${DEST}latest

if [ -d $INPROGRESS ] ; then
    echo `date +"%F %T"` " : Directory $INPROGRESS already exists - is a previous backup still running?"
    echo `date +"%F %T"` " : Directory $INPROGRESS already exists - is a previous backup still running?" | /usr/bin/mail -s "Backup of $SOURCE failed. Directory $INPROGRESS already exists - is a previous backup still running?" example@email.com
    exit 1
fi

echo `date +"%F %T"` " : Started rsyncing to  ${DEST}${CURRENTDATE}."


echo "source=$SOURCE"


/usr/bin/rsync --old-compress -rlptD --numeric-ids --hard-links --inplace --acls --xattrs --rsync-path "/usr/bin/sudo /usr/bin/rsync" --link-dest "${DEST}latest" --old-compress ${SOURCE} ${DEST}inprogress
RSYNCERROR=$?

if [ $RSYNCERROR -ne 0 ] ; then
    echo `date +"%F %T"` " : Rsync to ${DEST}inprogress exited with non-null status. Exiting..."
    echo `date +"%F %T"` " : Rsync to ${DEST}inprogress exited with non-null status. Exiting..." | /usr/bin/mail -s "Backup of $SOURCE failed" example@email.com
    exit 1
fi

/bin/mv ${DEST}inprogress ${DEST}${CURRENTDATE}
/bin/rm ${DEST}latest
/bin/ln -s ${DEST}${CURRENTDATE} ${DEST}latest
/bin/rm -r ${DEST}${OLDDATE}

echo `date +"%F %T"` " : Finished rsyncing to  ${DEST}${CURRENTDATE}."

exit 0

And make it executable

chmod +x /opt/bin/rsyncbackup.sh

Create a file /opt/bin/target-name.sh for every target server calling rsyncbackup script for the necessary directories

/opt/bin/rsyncbackup.sh backup-user@target-server-ip:/dir/to/backup1 /dir/for/the/backup1/ >> /var/log/rsyncbackup.log
/opt/bin/rsyncbackup.sh backup-user@target-server-ip:/dir/to/backup2 /dir/for/the/backup2/ >> /var/log/rsyncbackup.log

Make it executable

chmod +x /opt/bin/target-name.sh

Create the directories where the backups will be saved

mkdir -p /dir/for/the/backup

Create a file /etc/cron.d/backup and set the backup

# Minute   Hour   Day of Month       Month          Day of Week       User     Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)         

00 00 * * * root /opt/bin/target-name.sh

Make it executable

chmod +x /etc/cron.d/backup