Last active
August 29, 2015 14:10
-
-
Save dfang/a58bc901c7b584cdbafc to your computer and use it in GitHub Desktop.
use backup and whenever with capistrano to backup database on vps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1. install bakcup and whenever gem | |
| ``` | |
| gem 'backup' | |
| gem 'whenever' | |
| ``` | |
| 2. capistrano task(for uploading config file, put it to lib/capistrano/tasks/backup.rake) | |
| ``` | |
| namespace :backup do | |
| desc "Upload backup config files." | |
| task :upload_config do | |
| on roles(:app) do | |
| execute "mkdir -p #{fetch(:backup_path)}/models" | |
| upload! StringIO.new(File.read("config/deploy/backup/config.rb")), "#{fetch(:backup_path)}/config.rb" | |
| upload! StringIO.new(File.read("config/deploy/backup/models/db_backup.rb")), "#{fetch(:backup_path)}/models/db_backup.rb" | |
| end | |
| end | |
| desc "Upload whenever cron schedule file." | |
| task :upload_cron do | |
| on roles(:app) do | |
| execute "mkdir -p #{fetch(:backup_path)}/config" | |
| execute "touch #{fetch(:backup_path)}/config/cron.log" | |
| upload! StringIO.new(File.read("config/deploy/backup/schedule.rb")), "#{fetch(:backup_path)}/config/schedule.rb" | |
| within "#{fetch(:backup_path)}" do | |
| # capistrano was unable to find the executable for whenever | |
| # without the path to rbenv shims set | |
| with path: "/home/#{fetch(:deploy_user)}/.rbenv/shims:$PATH" do | |
| puts capture :whenever | |
| puts capture :whenever, '--update-crontab' | |
| end | |
| end | |
| end | |
| end | |
| end | |
| ``` | |
| 3. generate config file | |
| ``` | |
| bakcup -h | |
| backup generate:model -t, --trigger=TRIGGER | |
| backup help backup generate:model | |
| eg. | |
| backup generate:model --trigger noah_seiko --databases postgresql --archives --storages=local --compressors='gzip' | |
| backup perform -t noah_seiko | |
| cd ~/Backup | |
| mkdir config | |
| wheneverize . | |
| copy files generatedin ~/backup to your projects's folder config/deploy/backup, use cap task to upload it to server, test on server, then use whenever generate a cronjob file ......... | |
| ``` | |
| links: | |
| https://ruby-china.org/topics/4710 | |
| http://vladigleba.com/blog/2014/06/30/backup-a-rails-database-with-the-backup-and-whenever-gems/ | |
| http://meskyanichi.github.io/backup/v4/getting-started/ | |
| noah_seiko project on bitbucket.org |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment