Backup Database and Other Attachments in ROR

本文介绍如何使用Rubygem 'backup' 和 'whenever' 实现网站数据的自动化备份,并通过rsync与crontab同步到远程服务器。

Related Resources

  1. rsync:http://rsync.samba.org/
  2. Crontab:http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5
  3. "Linux Crontab 定时任务 命令详解":http://blog.youkuaiyun.com/tianlesoftware/article/details/5315039
  4. [rubygem]"backup":https://github.com/meskyanichi/backup
  5. [rubygem]"whenever":https://github.com/javan/whenever

Automatically backup on the remote server:

Let's firstly assumpt:

  1. You have a site named "example.com";
  2. You can login to it through a username "deploy", and its password is "password";
  3. You located the contents of your site in /var/www/example/;
  4. Your database server is Mysql, and the database for your site is example_production.

1. SSH login:

$ ssh deploy@example.com

2. Install the backup:

$ gem install backup

3. Prepare directories for backup files:

$ mkdir /var/www/example/backup
$ mkdir /var/www/example/backup/db

4. Generate and edit backup script:

$ cd ~/
$ mkdir Backup/
$ cd Backup
$ backup generate:model -t example_db --databases=mysql --compressors=gzip --storages=local
$ cd models
$ vim example_db.rb

Input the below content:

# encoding: utf-8

Backup::Model.new(:example_db, 'Dumping example Production Database') do
  split_into_chunks_of 250

  database MySQL do |db|
    db.name               = "example_production"
    db.username           = ""                      # Replace the blank string with your real username
    db.password           = ""                      # Replace the blank string with your real password
    db.host               = "localhost"
    db.port               = 3306
    db.socket             = "/var/run/mysqld/mysqld.sock"
    db.additional_options = ["--quick", "--single-transaction"]
  end

  store_with Local do |local|
    local.path       = "/var/www/example/backup/db"
    local.keep       = 10
  end

  compress_with Gzip
end

Run backup command to confirm that your config works:

backup perform -t example_db

Attention: Remember to update the config file if you have changed the username and/or password of the database.

5. Install whenever:

$ gem install whenever

6. Generate a schedule.rb file with whenever:

$ cd /var/www/example/
$ mkdir config
$ wheneverize .

The last command will create a new file named "schedule.rb" under config/ directory.

7. Add a daily task:

Open the file to edit:

vim config/schedule.rb

Contents:

set :output, "~/Backup/example_whenever.log"           # This is important, it can help you to find the reason when failed to backup the database
every :day do
  command "cd ~/Backup && backup perform -t example_db"
end

8. Update whenever task into crontab:

$ whenever --update-crontab

9. Check whether the task is updated successfully or not:

$ crontab -e

If successed, you should see your new task shown at the bottom of the file.

Synchronize remote backups every day automatically by rsync and crontab:

Attention: Below operations are played on a different server, which is used to sync files. We assumpt its domain is another.com and the username is deploy.

1. SSH login:

$ ssh deploy@another.com

2. Write a synchronize shell script:

$ mkdir ~/backup/example.com
$ cd ~/backup/example.com
$ vim sync.sh

Input the below contents into the new file named 'sync.sh':

#! /bin/bash
cd /home/deploy/backup/example.com/

rsync -av -e "ssh" deploy@example.com:/var/www/example/backup/db/example_db/ db/

rsync -av -e "ssh" deploy@example.com:/var/www/example/shared/public/uploads .

rsync -av -e "ssh" deploy@example.com:/var/www/example/shared/log .

and then, assign the scripe execution authority:

$ chmod 775 sync.sh

Run the script to ensure everything works well:

$ ./sync.sh

Attention: Because it is required to be authorized to access the targer server, you should remember to copy your ssh public key to the target server(such as, example.com) before you can sync files.

3. Open user's crontab config file:

$ crontab -e

Append the below task at the end:

10 0 * * * /home/deploy/backup/example.com/sync.sh

Ok, that's all. The rest work is that remember to check if there is any backups generated.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值