Posts tagged with rsync

Backup with rsync, samba, ruby & ubuntu

Cedric 17/07/2011 08:37 ubuntu rsync samba ruby 0 comments

I wanted to backup the important file from my webserver:

  • website
  • apache configurations
  • sql databases

I did it this way:

First check that ruby is installed on your server:

ruby -v

If you receive an error install ruby by typing

sudo apt-get install ruby

The files will be save on a share drive accessible through samba protocol:

sudo apt-get install smbfs
sudo mkdir /mnt/my_remote_disk

Create an entry in fstab to keep it permanent

//my_remote_disk/ /mnt/my_remote_disk cifs guest,uid=1000,iocharset=utf8,codepage=unicode,unicode  0  0

The script will be stored in /etc/cron.daily/my_backup_script.rb

#!/usr/bin/env ruby
require 'fileutils'
current_date=Time.now
old_date=current_date-(60*60*24*7) #7days before
new_folder="/mnt/my_remote_disk/".concat(current_date.strftime("%m-%d-%Y").to_s)
old_folder="/mnt/my_remote_disk/".concat(old_date.strftime("%m-%d-%Y").to_s)
puts %x{mkdir #{new_folder}}
puts %x{mysqldump -A --user=xxxxxx --password=xxxxxx > /my_directory_to_backup}
puts %x{rsync -a /my_directory_to_backup /etc/apache2 #{new_folder}}
if File.exists?(old_folder)
  puts %x{rm -rf #{old_folder}}
end

The script will delete the old backup from 7 days ago and create a backup of all mysql databases using mysqldump.

Finally add a line to /etc/crontab to have it done daily:

00 2    * * *   root    /etc/cron.daily/my_backup_script.rb

It should work, if you have problem just write a comment.