Backup on Bash Login from other server using SSHFS and RSYNC

Dear All,

Quick little post about a small script I made for backing up some files from a webserver (hosting apache+mysql).

I wanted a script which would remind me to backup whenever I would log onto the specific machine.

I would have preferred automated but I’m a little paranoid about security. Why?
I don’t want to automatically compromise the security of a public webserver if another computer is hacked…so even if the backup server is hijacked no certificate/password will be stored.

Hence running the backup script required an actual user to type in the password.

A colleague of mine noted RSYNC by itself can be used to sync with SFTP (so the mounting using SSHFS is overkill).

Anyway, below the steps:

1) Add the following to the bottom of the .bashrc file in your ~ directory.

while true; do
read -p "Do you wish to backup webserver www and mysql files?" yn
case $yn in
[Yy]* ) bash ~/Desktop/webserver_backup_script; break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done

2) Add the actual webserver_backup_script to the location specified above (e.g. Desktop or somewhere more secure).
What this essentially does is mount folders through Secure FTP on a remote machine using SSHFS.
The specific folders are mounted as READ ONLY  (the -o ro ).
RSYNC is then run to backup/synchronize between a local folder on a RAID6 partition, and the remote mounted folders.

#!/bin/bash
mkdir ~/Desktop/webserver_sftp

mkdir ~/Desktop/webserver_sftp/mysql
sshfs -o ro backup_user@webserver.ait.gr:/var/backup_folder_name/db ~/Desktop/webserver_sftp/mysql$
rsync -vru ~/Desktop/webserver_sftp/mysql /media/RAID6/websever_backup/

mkdir ~/Desktop/webserver_sftp/www
sshfs -o ro backup_user@webserver.ait.gr:/var/www ~/Desktop/webserver_sftp/www
rsync -vru ~/Desktop/webserver_sftp/www /media/RAID6/webserver_backup/

#REMOVE THE MOUNTS
fusermount -u ~/Desktop/webserver_sftp/www
fusermount -u ~/Desktop/webserver_sftp/mysql

3) Finally, on the actual webserver a cron job is required to generate the mysql backup dumps. I recommend automysqlbackup or a simple dump.

Enjoy!



Menelaos Bakopoulos

Mr. Menelaos Bakopoulos is currently pursuing his PhD both at Center for TeleInFrastruktur (CTiF) at Aalborg University (AAU) in Denmark and Athens Information Technology (AIT) in Athens, Greece. He received a Master in Information Technology and Telecommunications Systems from Athens Information Technology and a B.Sc. in Computer Science & Management Information Systems from the American College of Thessaloniki. Since April 2008 he has been a member of the Multimedia, Knowledge, and Web Technologies Group.

More Posts