How to backup your webserver files 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

 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. Type

 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 123@tv-s009.rsync.net:.ssh/authorized_keys

Of course change 123@tv-s009.rsync.net with your rsync.net user info.

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

  1. #!/bin/bash
  2. ### MySQL Server Login Info ###
  3. MUSER="yourmysqluser"
  4. MPASS="yourmysqlpass"
  5. MHOST="localhost"
  6. MYSQL="$(which mysql)"
  7. MYSQLDUMP="$(which mysqldump)"
  8. BAK="/your/backup/path/to/db"
  9. GZIP="$(which gzip)"
  10. MOUNT="/mnt/your-rsync-backup-folder" #name whatever you want
  11.  
  12. find $BAK -type d -mtime +30 -exec rm -rf {} \; #delete anything older than 30 days. you can change this one whatever you want.
  13. NOW=$(date +"%d-%m-%Y")
  14. mkdir $BAK/$NOW
  15.  
  16. DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
  17. for db in $DBS
  18. do
  19. FILE=$BAK/$NOW/$db.$NOW-$(date +"%T").gz
  20. $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
  21. done
  22.  
  23. sshfs your-rsync-net-username@your-rsync-net-server: $MOUNT
  24.  
  25. rsync -az --delete --force --stats /your/backup/path $MOUNT
  26.  
  27. 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

  1. #!/bin/bash
  2.  
  3. mark_file=/tmp/job-run-marker
  4. # check whether the job runned last week
  5. if [ -e $mark_file ] ; then
  6. rm -f $mark_file
  7. else
  8. touch $mark_file
  9. exit 0
  10. fi
  11.  
  12. #above part is for only to run this script once in two weeks
  13.  
  14. BAK="/your/backup/path/to/vhosts"
  15. VHOSTS="/your/path/to/vhosts"
  16. MOUNT="/mnt/your-rsync-backup-folder" #name same as the above one
  17.  
  18. 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
  19. NOW=$(date +"%d-%m-%Y")
  20. mkdir $BAK/$NOW
  21.  
  22. cd $VHOSTS
  23. for files in *
  24. do
  25. set $files
  26. OF=$BAK/$NOW/$1-$NOW-$(date +"%T").tar.gz
  27. #echo $OF
  28. tar -czvf $OF $files
  29. done # exiting for loop
  30. unset $files # un setting set variables
  31.  
  32. sshfs -o nonempty your-rsync-net-username@your-rsync-net-server: $MOUNT
  33.  
  34. rsync -az --delete --force --stats /your/backup/path $MOUNT
  35.  
  36. fusermount -u $MOUNT
  37.  
  38. 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)

  1. 01 02 * * * /your/path/to/autodb.sh
  2. 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.

Cevapla

Bu alanın içeriği gizli tutulacak ve açıkta gösterilmeyecektir.
 .