How to backup your webserver files and mysql database using rsync.net

I am very happy with my provider, but if you don’t have backup server for your account, you need another service to backup your files.

After a little research, i found rsync.net. Their price is acceptable ($0.80-1.4 gb/mounth | static - global redundant) , and the service is really fine.

I want to backup my dbs every night, but files; every two weeks. My model is like this:

  1. Backup every database individually to /mybackups/db
  2. Backup every vhost folder individually /mybackups/vhosts
  3. Than mount rsync.net filesystem to the webserver
  4. Syncronize local backup folder to the mounted filesystem

Here is how i setup my server.

You need to install sshfs to your server first, than

sudo apt-get install sshfs

due to a bug, run below code

sudo modprobe fuse

If rsync is not installed

sudo apt-get install rsync

After you make your purchase from rsync.net, they will send you an email regarding to your account information. I choosed tp pay with paypal and subscribed, so it will be paid monthly.

We are going to use automated scripts, so, it is better to use ssh keys. If you haven’t created your ssh key already, do it:

ssh-keygen -t rsa

Do not enter a pass phrase, just hit enter twice.

Now we need to send it to the rsync.net server. Type something like this:

scp ~/.ssh/id_rsa.pub [email protected]:.ssh/authorized_keys

Of course change [email protected] with your rsync.net user info.

To backup my databases i use this bashscript (autodb.sh)

#!/bin/bash
### MySQL Server Login Info ###
MUSER="yourmysqluser"
MPASS="yourmysqlpass"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
BAK="/your/backup/path/to/db"
GZIP="$(which gzip)"
MOUNT="/mnt/your-rsync-backup-folder" #name whatever you want
 
find $BAK -type d -mtime +30 -exec rm -rf {} \; #delete anything older than 30 days. you can change this one whatever you want.
NOW=$(date +"%d-%m-%Y")
mkdir $BAK/$NOW
 
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
 FILE=$BAK/$NOW/$db.$NOW-$(date +"%T").gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
 
sshfs [email protected]: $MOUNT
 
rsync -az --delete --force --stats /your/backup/path $MOUNT
 
fusermount -u $MOUNT

Now save it wherever you want (not under anywhere web accesible!) and change its writing permissions to 755

To backup my files (autofiles.sh) i use something like this

#!/bin/bash
 
mark_file=/tmp/job-run-marker
# check whether the job runned last week
if [ -e $mark_file ] ; then
  rm -f $mark_file
else
  touch $mark_file
  exit 0
fi
 
#above part is for only to run this script once in two weeks
 
BAK="/your/backup/path/to/vhosts"
VHOSTS="/your/path/to/vhosts"
MOUNT="/mnt/your-rsync-backup-folder" #name same as the above one
 
find $BAK -type d -mtime +46 -exec rm -rf {} \; #delete anything older than 46 days. since we backup our files every two weeks, we will have 4 versions of each
NOW=$(date +"%d-%m-%Y")
mkdir $BAK/$NOW
 
cd $VHOSTS
for files in *
do
set $files
OF=$BAK/$NOW/$1-$NOW-$(date +"%T").tar.gz
#echo $OF
tar -czvf $OF $files
done # exiting for loop
unset $files # un setting set variables
 
sshfs -o nonempty [email protected]: $MOUNT
 
rsync -az --delete --force --stats /your/backup/path $MOUNT
 
fusermount -u $MOUNT
 
exit 0 # exiting shell script

Save this file like the other one and change write permission of course.

Now we need to tell the machine to run these scripts automatically. Type

crontab -e

At the end of the file add these lines and press kntrl+o and enter (i mean save it)

01 02 * * * /your/path/to/autodb.sh
01 03 * * 7 /your/path/to/autofiles.sh 

First line will execute the db bash script every day at 02:01 am, the second one execute files bash script every sunday at 03:01, but the file control in the script cause it to run only once in two weeks.

I think that’s all. If you face any problems, feel free to let me know.