Remote Server Rsync Backup Replication Shell Script

in Categories Backup last updated April 24, 2008

You need to setup password less login using ssh keys; refer to following tutorials:
+ Howto Linux / UNIX setup SSH with DSA public key authentication (password less login)

+ SSH Public key based authentication – Howto

#!/bin/bash
# Remote Server Rsync backup Replication Shell Script 
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ------------------------------------------------------------------------- 
# Local dir location
LOCALBAKPOINT=/disk3
LOCALBAKDIR=/remote/home/httpd/
# remote ssh server
# user
SSHUER=brootbeer 
 
# server IP / host
SSHSERVER=10.10.11.12 
 
#remote dir to backup
SSHBACKUPROOT=/disk2.backup/hot/ 
 
rsync --exclude '*access.log*' --exclude '*error.log*' -avz -e 'ssh ' ${SSHUER}@${SSHSERVER}:${SSHBACKUPROOT} ${LOCALBAKPOINT}${LOCALBAKDIR}
 
# log if backup failed or not to /var/log/messages file
[ $? -eq 0 ] && logger 'RSYNC BACKUP : Done' || logger 'RSYNC BACKUP : FAILED!'
 
# Replicate backup to /disk1 and /disk2
# You can also use format user@host:/path
# refer to rsync man page
SRC=${LOCALBAKPOINT}${LOCALBAKDIR}
DST="/disk1/remote /disk2/remote"
for d in $DST
do
 [ ! -d $d ] && mkdir -p $d || :
 rsync -avr $SRC $d
done

Shell script to create list of backup files in ~/.mybackup file. Use with mybackup shell script

in Categories Backup last updated April 9, 2008
#!/bin/bash
# mybackupadd - Add file to ~/.mybackup file, then backup and email all
# file as tar.gz to your email a/c.
#
# Usage   : ./mybackupadd ~/public_html/
#
# Copyright (C) 2004 nixCraft project
# Email   : http://cyberciti.biz/fb/
# Date    : Aug-2004
# -------------------------------------------------------------------------
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
FILE=~/.mybackup
MYH=~
CWD=`pwd`
SRC=$1
 
if [ "$SRC" == "" ]; then
  echo "Must supply dir or file name"
  exit 1
fi  
# if list $FILE does not exist
[ ! -f $FILE ] && touch $FILE || :
 
# make sure that file or dir exists to backup
if [ ! -f $SRC ]; then
   if [ ! -d $SRC ]; then
      echo "$SRC does not exists"
      exit 2
   fi
fi
# make sure we don't do add duplicate stuff
cat $FILE | grep -w $SRC > /dev/null
if [ "$?" == "0" ]; then
   echo "$SRC exists in $FILE"
   exit 3
fi 
# okay now add that to backup list
echo "$SRC" >> $FILE
echo "$SRC added to $FILE"