Production Server Database And File Backups

Install the Backup gem

Install Backup gem on root directory, not in your project directory.

gem install backup

Setup your Backup script

First we want to generate a new backup script:

backup generate:model --trigger production_backup \
  --archives --storages='local' --compressor='gzip' --notifiers='mail'

This will generate a folder in your user’s home directory called Backup It will have a couple files inside:

  • config.rb – This is your main Backup configuration file. You can read through this if you like, but you probably won’t need to change it
  • log – This is the where the backup logs are stored
  • models – This folder contains your scripts, specifically production_backup.rb that we just generated

Now we can edit ~/Backup/models/production_backup.rb to backup our database, files, and notify us.

Be sure to change the parts that are in all CAPS to match your server setup.

Open this file with Vim or Nano:

sudo nano ~/Backup/models/production_backup.rb

Replace the contents with the following config:

# encoding: utf-8

##
# Backup Generated: production_backup
# Once configured, you can run the backup with the following command:
#
# $ backup perform -t production_backup [-c <path_to_configuration_file>]
#
Model.new(:production_backup, 'Production Backup') do
  split_into_chunks_of 250
  compress_with Gzip

  ##
  # PostgreSQL [Database]
  #
  database PostgreSQL do |db|
    # To dump all databases, set `db.name = :all` (or leave blank)
    db.name               = "DATABASE_NAME"
    db.username           = "DATABASE_USERNAME"
    db.password           = "DATABASE_PASSWORD"
    db.host               = "localhost"
    db.port               = 5432
    db.additional_options = ["-xc", "-E=utf8"]
  end

  ## 
  # Archive our app
  #
  archive :app_archive do |archive|
    archive.use_sudo
    archive.add '/home/deploy/MYAPP/'
  end

  ##
  # Store DB Dump locally
  #
  store_with Local do |local|
    local.path       = "~/backups/"
    local.keep       = 30
    # local.keep       = Time.now - 2592000 # Remove all backups older than 1 month.
  end
  
  ##
  # Mail [Notifier]
  #
  notify_by Mail do |mail|
    mail.on_success           = true
    mail.on_warning           = true
    mail.on_failure           = true

    mail.from                 = "no-reply@MYDOMAIN.COM"
    mail.to                   = "YOUR_EMAIL_ADDRESS"
    mail.address              = "smtp.gmail.com"
    mail.port                 = 587
    mail.domain               = "YOUR_DOMAIN"
    mail.user_name            = "YOUR_SMTP_USERNAME"
    mail.password             = "YOUR_SMTP_PASSWORD"
    mail.authentication       = "login"
    mail.encryption           = :starttls
  end

end

What this will do is setup a backup for PostgreSQL to be dumped and archive your Rails app (including any file uploads that are stored locally) and then it will email us if anything in case of success, warning and error.

There’s a lot of Documentation on the Backup gem if you want to make any modifications. They also support different databases like PostgreSQL, Mongo, Redis, many other storage locations and notification methods.

Test your Backup script

This is easy. Just run the following in your terminal. It will automatically detect the script in the models folder and run it for you.

backup perform -t production_backup

You should get some output telling you what the Backup is doing and, if you configured everything correctly, it will succeed and you will have a new file stored in your local directory.

Schedule Your Backups

Now we need to make this script run every hour. We’re going to use Cron to do this.

So first, let’s grab the executable path:

which backup
# /usr/local/bin/backup

Take note of the output of this command because you’re going to use it next.

And now let’s type crontab -eto setup our Cron job. Add a line at the bottom that looks like the following:

0 * * * * /bin/bash -l -c '/usr/local/bin/backup perform -t production_backup'

Be sure to replace /usr/local/bin/backup with the output of the which backup command. This will make sure the Cron job can find the executable for Backup so that it can run your script.

This line basically tells Cron to perform the backup at the top of every hour. The cron format can be kind of hard to read, so if you’d like to learn more about it, take a look here.

Or we can also use the Whenever gem to manage your backup Cron job instead. This does let you save your cron jobs into your Git repo so you can manage them easily. Definitely worth checking out.

Install Whenever Gem with Bundler in your Gemfile.

gem 'whenever', require: false
$ cd /apps/my-great-project
$ wheneverize .

This will create an initial config/schedule.rb file for you (as long as the config folder is already present in your project).
Add Cronjobs in config/schedule.rb file like this
every 1.day do
   command 'backup perform -t production_backup'
end

after updating schedule.rb run following commands to update crontab

$ whenever --update-crontab   //equivalent to crontab -w
$ whenever

Conclusion

With the Backup gem, it’s super simple to backup your application and production data. Since your code is stored in a Git repository (I hope!) you don’t need to worry about it too much. But when it comes to production data, you want to make sure it is backed up regularly, on a schedule, and saved to a remote machine in case anything happens to the server. The Backup gem helps you through a whole lot of this process with relative ease and a great amount of documentation.

Always be sure to test your backups and make sure you can safely restore from them!

Things To Remember

Make sure you install backup gem at your root directory of your system.

 

Reference: https://gorails.com/guides/hourly-production-server-database-and-file-backups

Advertisements

“as_json” Rails

as_json has very flexible way to configure complex object according to model relations

EXAMPLE

Model campaign belongs to shop and has one list
Model list has many list_tasks and each of list_tasks has many comments
We can get one json which combines all those data easily.

@campaign.as_json(
    {
        except: [:created_at, :updated_at],
        include: {
            shop: {
                except: [:created_at, :updated_at, :customer_id],
                include: {customer: {except: [:created_at, :updated_at]}}},
            list: {
                except: [:created_at, :updated_at, :observation_id],
                include: {
                    list_tasks: {
                        except: [:created_at, :updated_at],
                        include: {comments: {except: [:created_at, :updated_at]}}
                    }
                }
            },
        },
        methods: :tags
    })

Notice methods: :tags can help you attach any additional object which doesn’t have relations with others. You just need to define a method with name tags in model campaign. This method should return whatever you need (e.g. Tags.all)

Official documentation for as_json

RVM – Gemsets

Creating gemsets

Gemsets must be created before being used. To create a new gemset for the current ruby, do this:
rvm 2.1.1
rvm gemset create teddy
Gemset 'teddy' created.

Alternatively, if you prefer the shorthand syntax offered by rvm use, employ the –create option like so:
rvm use 2.1.1@teddy --create

Select gemsets according to the Project

There is a new way to do this, without having to allow arbitrary shell script to be executed in a .rvmrc file.

Create a file named .ruby-gemset containing only the gemset name in.
multilingo_cms
Need an up to date version of rvm for this to work.

You can also specify the ruby version by creating a file named .ruby-version containing only the ruby version:
1.9.3


This format also has the advantage of being compatible with rbenv and rbfu.

If you have existing projects using the deprecated .rvmrc, you can convert them to the new format using the command:
rvm rvmrc to .ruby-version

RubyMine Ubuntu

rubymine-ide-banner

Install RubyMine

  1. Download RubyMine from Source
  2. Unpack the downloaded archive:
    1. tar -xzf RubyMine-X.Y.Z.tar.gz
  3. Goto extracted folder, nevigate to ‘bin’ directory
    1. ./rubymine.sh
  4. Open any project, goto (Tools) -> (Create Desktop Entry)

Change Rubymine Theme

  1. File -> Settings -> Appearance and Behavior -> Appearance

Remove RubyMine

  1. locate rubymine
  2. locate RubyMine
  3. rm -rf ~/.RubyMine