Skip to content

Instantly share code, notes, and snippets.

@linuxmalaysia
Last active May 15, 2024 23:27
Show Gist options
  • Save linuxmalaysia/575f62f39301aaf6ba1acdd0f08ee074 to your computer and use it in GitHub Desktop.
Save linuxmalaysia/575f62f39301aaf6ba1acdd0f08ee074 to your computer and use it in GitHub Desktop.
Old School Backup Using Rsync - Yeahhh on SCO Unix (BSD Style) - Convert to Linux Bash
#!/bin/bash
# Script to transfer directories from a remote server using rsync over SSH
# Harisfazillah Jamel (LinuxMalaysia)
# Circa 2006 For SCO Unix and BSD
# Adjust for Bash Linux 16 Mei 2024
# Target remote server details
# User need to be create and remote backup dir need to own by the user
# RADD can be local or remote IP
RADD=127.0.0.1
RUSER=backupuser
# Email addresses for notifications
ADMINEMAIL="[email protected]"
###ADMINEMAIL2="[email protected]"
EMAILSUBJECT="Backup Rsync From $RADD"
# Directories to backup (modify as needed) - leave out the /
MDIR="root etc \
data"
# Temporary and working directories
TMPDIR=/tmp
WRKDIR=/tmp
# Target directory on remote host
TDIR="/mnt/backup"
# Control and status files
CONTROLSTATUS=""
CURRENTSTATUS=""
# Get current date and time components
#
# setiap os ada cara masing-masing
# sila ubah
CMASA=`date`
HARI=`date '+%d'`
BULAN=`date '+%m'`
TAHUN=`date '+%Y'`
HARINAMA=`date '+%A'`
SAAT=`date '+%S'`
MINIT=`date '+%M'`
JAM=`date '+%H'`
#
# Paths to system utilities (modify if needed)
SSH="/usr/bin/ssh -l"
SCP="/usr/bin/scp"
SFTP="/usr/bin/sftp"
TAR="/usr/bin/tar"
RSYNC="/usr/bin/rsync"
PINGG="/sbin/ping -qnc 10"
# rsync options
OPT="-aczRrvlogp -e ssh --delete"
OPT2="--stats --progress --timeout=14400"
# PID and status files
PIDF="$TMPDIR/remotedir-backup-u-bsd.pid"
STATUSF="$TMPDIR/remotedir-backup-u-bsd.status"
STATUSF1="remotedir-backup-u-bsd.status"
# Remote dir triger ikut script cron bsd
TRIGERF=$TMPDIR/remotedir.triger
CONTROLSTATUSF=$TMPDIR/remotedir-backup-u-bsd.control
#
#
LOGINFO="logger -t RSCO2[$$] -p local1.info"
LOGALR="logger -t RSCO2[$$] -p local1.alert"
#
echo "Start the script on $CMASA"
# Clear status file before starting
> "$STATUSF"
$LOGINFO "Start the script - START"
# Check for existing PID file
cd "$WRKDIR"
if [[ ! -f "$PIDF" ]]; then
echo $$ > "$PIDF"
if [[ $? -ne 0 ]]; then
echo "Failed to create PID file!"
echo "Please check!"
echo "ERROR: $PIDF NOT created on $(date)" >> "$STATUSF"
$LOGALR "$PIDF NOT created"
exit 1
fi
echo "Start the script on $CMASA" > "$STATUSF"
echo "$PIDF OK created on $(date)" >> "$STATUSF"
else
echo "Script is already running!"
echo "Please check!"
echo "ERROR: Script still running on `date` [$$]" >> "$STATUSF"
$LOGALR "Script still running"
exit 1
fi
# Check if backup is scheduled for today
CURRENTSTATUS="$HARI$BULAN$TAHUN"
CONTROLSTATUS=$(cat "$CONTROLSTATUSF" 2>/dev/null)
if [[ "$CURRENTSTATUS" -eq "$CONTROLSTATUS" ]]; then
echo "$CURRENTSTATUS same as $CONTROLSTATUS"
echo "No backup needed today."
echo "INFO: $CURRENTSTATUS same as $CONTROLSTATUS on `date` [$$]" >> "$STATUSF"
$LOGALR "$CURRENTSTATUS sama dengan $CONTROLSTATUS"
rm "$PIDF"
exit 0
fi
echo "Running backup on $HARINAMA, $HARI/$BULAN/$TAHUN at $JAM:$MINIT:$SAAT. Backing up: $MDIR"
# Check if remote host is reachable
if ! ping -q -c 1 "$RADD" >/dev/null 2>&
# Continue script execution
# Create target directories on remote server (if they don't exist)
for d in $MDIR; do
mkdir -pv "$TDIR/$d" &>/dev/null
done
# Loop through directories to backup
for d in $MDIR; do
cd "$TDIR/$d"
echo "$TDIR/$d"
# Log backup info
echo "Backup $d from $RADD with user $RUSER To $TDIR $(date)" >> "$STATUSF"
echo "Backup $d from $RADD with user $RUSER To $TDIR $(date)" > "$TDIR/$d/$STATUSF1"
# Copy status file to remote server for reference
scp "$TDIR/$d/$STATUSF1" "$RUSER@$RADD:/$d/" &>/dev/null
# Log backup start
$LOGINFO "Backup $d from $RADD with user $RUSER to $TDIR"
# Perform rsync backup
rsync "$OPT" "$OPT2" "$RUSER@$RADD:/$d/*" "$TDIR"
# Check rsync exit status
if [[ $? -ne 0 ]]; then
echo "ERROR: Backup $d from $RADD with user $RUSER To $TDIR $(date)" >> "$STATUSF"
echo "ERROR: Backup $d from $RADD with user $RUSER To $TDIR $(date)" > "$TDIR/$d/$STATUSF1"
$LOGALR "ERROR: Backup $d from $RADD with user $RUSER to $TDIR"
scp "$TDIR/$d/$STATUSF1" "$RUSER@$RADD:/$d/" &>/dev/null
fi
done
# Trigger remote script (modify if needed)
echo "REMOTESTARTBACKUP $(date)" >> "$STATUSF"
echo "REMOTESTARTBACKUP $(date)" > "$TRIGERF"
scp "$TRIGERF" "$RUSER@$RADD:$TDIR" &>/dev/null
cp "$TRIGERF" "$TDIR" # Might need adjustment for remote script trigger
# Cleanup and logging
echo "Backup Completed $(date)" >> "$STATUSF"
$LOGINFO "Habis dah"
scp "$STATUSF" "$RUSER@$RADD:$TDIR" &>/dev/null
rm "$PIDF"
# Update control file with current date for daily check
echo "$CURRENTSTATUS" > "$CONTROLSTATUSF"
# Send email notifications (modify if needed)
# Make sure local postfix install and relay to mail server
cat "$STATUSF" | mailx -s "$EMAILSUBJECT" "$ADMINEMAIL"
###cat "$STATUSF" | mailx -s "$EMAILSUBJECT" "$ADMINEMAIL2"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment