rsync with BASH scripting for backup


Introduction: I needed to backup copy from my remote server (arkenidar.com personal website) to my local computer, incrementally, 3 important directories, into my Dropbox so to keep them. This does not keep into account database backup so this plus servmask’s “All-in-One WP Migration plugin” enabled me to move my partially WordPress partially “custom HTML” personal web site from Linode to DigitalOcean, after also screwing an Ubuntu’s release upgrade, with ensuing security concerns (php sources were exposed also config php files, because they weren’t processed by misconfiguration after upgrade).

Scripting enable to ease the automating of keeping this.

There are variables for configuring for personal use. The variable RSOPT (rsync options) enable to inject options to the rsync use.

I am publishing because initially it was very basic and simple then it kind of grew, so I am showing it. And pertinent feedback is esteemed valuable by me.

#!/usr/bin/env sh

# env RSOPT="--dry-run" ~/Dropbox/my-www/rsync-backup-www.sh
# env RSOPT="--dry-run --delete" ~/Dropbox/my-www/rsync-backup-www.sh

HOST=arkenidar.com
THIS=backup-www
DEST=~/Dropbox/my-www/$THIS
mkdir --parents $DEST
line="-------------------------------------------------"
echo $line
cd $DEST && RSYNC="rsync $RSOPT -arvz root@$HOST:" && \
echo ">> $RSYNC/root ." && $RSYNC/root . && echo $line && \
echo ">> $RSYNC/etc/apache2 ." && $RSYNC/etc/apache2 . && echo $line && \
echo ">> $RSYNC/var/www ." && $RSYNC/var/www . && echo $line && \
echo ">> backup DONE (BUT databases aren't included)"
echo $line

then became:

It has a FOR loop with BREAK condition, mimicking the “command1 && command2 && command3” semantics.

It prints the command before executing it (ala Makefile perhaps), by custom code.

This is my first more meaningful BASH script so I am a beginner in this. And it spanned Ubuntu Server, KUbuntu, Manjaro, Windows 10 (Cygwin, Mingw, Termius for shells). Also a use of Dropbox is used to keep them somewhere safe but accessible, other options? Someone told me of Deja-Dup but is it for remote servers? Of course one can creatively find other ways, evaluate them together using our skills and experiences, so I am asking and presenting this as clear as I manage to.


HOST=arkenidar.com
THIS=backup-www
DEST=~/Dropbox/my-www/$THIS
mkdir --parents $DEST
line="-------------------------------------------------"
echo $line
cd $DEST && RSYNC="rsync $RSOPT -arvz root@$HOST:" && \

for dir in /root /etc/apache2 /var/www ; do
COMMAND="$RSYNC$dir ."
echo ">> $COMMAND" && $COMMAND

if [ $? -ne 0 ] ; then
    echo $line
    echo ">> Exit status is FAILURE, exiting."
    echo $line
    exit 1
fi

echo $line
done

echo ">> backup DONE (BUT databases aren't included)"
echo $line

Lascia un commento

Il tuo indirizzo email non sarà pubblicato.

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.