Поиск

5 дек. 2019 г.

backup_for_rsync_in_remote_server

backup_for_rsync_in_remote_server

#!/bin/bash

##############################################



#backup_for_rsync_in_remote_server

#put this script on the server on which the backup made

#If you use a script, use the authorization to ssh key to the remote server

#########################################

###############Change by itself to rsync###############

DAY=$(date +"%d-%m-%y")               #receive date

REMOTESERVER="backupus1@blabla.com"     #address to connect remote serverн

DIRTOBACKUP="/home/blabla/backupus1"          #directory from backup is done

DIRFROMBACKUP="/var/www/"                 #directory where the backup is done

EMAILD="vladbbk@gmail.com"            #email to send mail

######Change mysql connect#############

MUSER="user"

MPASS="pass"

MHOST="localhost"

MYSQL="$(which mysql)"

MYSQLDUMP="$(which mysqldump)"

GZIP="$(which gzip)"

BACKUP="/var/www/mysqlbackup"

##########backup mysql##########

mkdir -p $BACKUP

# Get all databases name

DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"

for db in $DBS

do

FILE=$BACKUP/mysql-$db.$NOW-$DAY.gz

$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE

done

####upload files to server####

rsync -azo $DIRFROMBACKUP $REMOTESERVER:$DIRTOBACKUP/$HOSTNAME-$DAY

#######send to mail result#################

if [ "$?" == "0" ] ; then
T=/tmp/backup.good
echo "Date: $(date)">$T
echo "Hostname: $(hostname)" >>$T
echo "Backup ready" >>$T
mail -s "BACKUP READY" "$EMAILD" <$T
rm -f $T
rm -r $BACKUP
else
T=/tmp/backup.fail
echo "Date: $(date)">$T
echo "Hostname: $(hostname)" >>$T
echo "$?" >>$T
echo "Backup fails" >>$T
mail -s "BACKUP FAILD" "$EMAILD" <$T
rm -f $T
rm -r $BACKUP
fi

Комментариев нет: