Backup script example, Auto Make directory, Prepare date file name, dump mySQL, Auto rename old backup files with index code, rsync data

#!/bin/bash
basedir=/var/backup/store
sqldir=$basedir/sql
PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH
export LANG=C

for dirs in $basedir
do
        [ ! -d "$dirs" ] && mkdir -p $dirs
done

for dirs in $sqldir
do
        [ ! -d "$dirs" ] && mkdir -p $dirs
done



param1=$(date +%Y-%m-%d-%H-%M-%S)

# Database Backup
sqlusername=root
sqlpassword=Z2PG82SMtQzD
mysqldump=/usr/bin/mysqldump
bwlimit=--bwlimit=100K
#bwlimit=

sqldbname=dbispconfig
sqlsavetarget=$sqldir/$sqldbname.sql
rm -rf $sqlsavetarget.7
mv $sqlsavetarget.6 $sqlsavetarget.7
mv $sqlsavetarget.5 $sqlsavetarget.6
mv $sqlsavetarget.4 $sqlsavetarget.5
mv $sqlsavetarget.3 $sqlsavetarget.4
mv $sqlsavetarget.2 $sqlsavetarget.3
mv $sqlsavetarget.1 $sqlsavetarget.2
mv $sqlsavetarget $sqlsavetarget.1
$mysqldump -u $sqlusername -p$sqlpassword --skip-lock-tables -R $sqldbname > $sqlsavetarget

/usr/bin/rsync -ah $bwlimit --delete /etc $basedir
/usr/bin/rsync -ah $bwlimit --delete /usr/local/ispconfig $basedir

rsync site2site.sh script sample

#!/bin/bash

if pidof -x "rsync" >/dev/null; then
         echo "Process already running"
         exit 0
 fi

rsync -avzhe "ssh -i /root/.ssh/andrew-rsync-key" /volume1/andrew/ andrew@10.1.0.1:/volume1/andrew --delete --exclude=#recycle --chmod=ugo+rw
rsync -avzhe "ssh -i /root/.ssh/andrew-rsync-key" /volume1/peter/ andrew@10.1.0.1:/volume1/peter --delete --exclude=#recycle --chmod=ugo+rw
rsync -avzhe "ssh -i /root/.ssh/andrew-rsync-key" /volume1/may/ andrew@10.1.0.1:/volume1/may --delete --exclude=#recycle --chmod=ugo+rw

Reference

Andreas Koch(2013), Rsync over SSH with key authentication, 
https://andykdocs.de/development/Linux/2013-01-17+Rsync+over+SSH+with+Key+Authentication, Last: 2021-05-31

Rsync over SSH with key authentication

Using rsync and ssh to synchronize folders over the network.

created by Andreas Koch on 2013-01-17

Setup the SSH login with key authentication

Create a new ssh key pair:

ssh-keygen -t rsa -b 2048 -f andy-rsync-key

Move the public(!) key to the remote server:

scp andy-rsync-key.pub andy@example.cloudapp.net:/home/andy/

Append the public key to the “authorized_keys” on your remote server:

ssh -l andy example.cloudapp.net
cat andy-rsync-key.pub >> .ssh/authorized_keys

Test the connection:

ssh -l andy -i ~/.ssh/andy-rsync-key example.cloudapp.net

You should not be prompted for a password.

Setup the folder synchronization

Test the synchronization:

rsync --progress -avz -e "ssh -i /home/dev/.ssh/andy-rsync-key" /home/dev/git-master/ andy@example.cloudapp.net:/home/andy/git-master/

Create a shell script for the synchronization

vi ~/bin/sync-git-master.sh

#!/bin/bash
rsync –progress -avz –delete -e “ssh -i /home/dev/.ssh/andy-rsync-key” /home/dev/git-master/ andy@example.cloudapp.net:/home/andy/git-master/

chmod 700 ~/bin/sync-git-master.sh


Schedule the script execution (every 5 minutes):

```bash
crontab -e
# m h  dom mon dow   command
*/5 * * * * /home/dev/bin/git-master-sync.sh