2016
April
Standard

WordPress backup & rsync to remote Debian host

script:

#!/bin/bash

#this script is: /home/gus/backup.sh
#backup are in: /data/backups

#set date of backup
sysdate=`date '+%Y-%b-%d-%H:%M'`
#set prefix for backup type
prefix='site-to-backup-data'
#make tar.gz archive backup of WP dir....
tar -zcvf /data/backups/${prefix}_$(date +%Y-%b-%d-%H:%M).tar.gz /data/site-to-backup/site-to-backupaz

#set prefix for mysq backup
prefix='site-to-backup-mysql'
#set user of mysql
MUSER='user'
#set passwors of mysl user
MPASS='passwd'
#mysq dump....
mysqldump -u $MUSER -p$MPASS --databases db_name --single-transaction | gzip -9 > /data/backups/${prefix}_$(date +%Y-%b-%d-%H:%M).sql.gz

#ROTATION
#delete old mysql backups (keep always the 10 most recent files)
cd /data/backups
ls -A1t | grep mysql | tail -n +11 | xargs rm -rf

#delete old site backups (keep always the 10 most recent files)
ls -A1t | grep data | tail -n +11 | xargs rm -rf
#echo date
echo "Full Backup Done@"$sysdate
<!--more-->
###RSYNC BACKUP STEPS:

##for backup on remote server was chosen 'gus-backup' user!

# ssh-keygen -f ~/.ssh/id_rsa -q -P ""

# scp -P NNNN /root/.ssh/id_rsa.pub [email protected]:/home/gus-backup/rsync-key.pub

#####On remote machine login via gus-backup!

$ if [ ! -d .ssh ]; then mkdir .ssh ; chmod 700 .ssh ; fi 
$ mv rsync-key.pub .ssh/ 
$ cd .ssh/ 
$ if [ ! -f authorized_keys ]; then touch authorized_keys ; chmod 600 authorized_keys ; fi 
$ cat rsync-key.pub >> authorized_keys 

######Check ssh login via key from source machine passwordless!

# ssh -p NNNN [email protected]

#check rsync relation from source server!

###########
# rsync --progress -e 'ssh -p NNNN' -avzp /data/backups [email protected]:/home/gus-backup/
###########

##############cron job:

# crontab -l
###START AT 01:30 AM EVERY DAY
#30 1 * * * /home/gus/backup.sh
###START AT 02:30 AM EVERY DAY
30 2 * * * rsync --progress -e 'ssh -p NNNN' -avzp /data/backups [email protected]:/home/gus-backup/
####EVERY MIN
*/1 * * * * /home/gus/backup.sh

Continue Reading →

The end