backup revision 4fbbf628eda08ffdeb98cb41fb7e9001050eec3a
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# You can find more extensive documentation of this script at
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# https://github.com/ontohub/ontohub/blob/staging/doc/backup_and_restore_of_ontohub_data.md
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# Description
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# This backup script creates and restores backups of ontohub data. It includes:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# * bare git repositories (data/repositories)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# * named symlinks to git repositories (data/git_daemon)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# * the postgres database
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# First note: Run this as the root user, e.g. with sudo.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov# To create a backup, run this script with the argument `create`:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# Then a backup named with the current date and time is created in the
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov# backup directory (see below).
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov# To restore a backup, run this script with the argument `restore <backup name>`
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# # script/backup restore 2015-01-01_00-00
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov# Then the selected backup is fully restored
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# Backup directory
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# For development machines, the backup directory is:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# <rails root>/tmp/backup/
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# And for production machines, the backup directory is:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# Super user privileges
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# To create and restore, we need root privileges. Otherwise file modes are not
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# preserved. This script will call `sudo` when needed and inform you about the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# reason for calling `sudo`. If you don't allow sudo, a backup will be created
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# or restored anyway, but the file modes and ownership are not preserved.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# Then, you need to adjust them manually.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# Maintenance mode
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# While backing up and restoring the data, the maintenance mode is activated.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe# This way we guarantee data consistency of the backup.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovrequire 'fileutils'
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowerequire 'pathname'
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowerequire 'open3'
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe # Amount of backups that have to be there at least
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe # Backups are kept for at least 365 days
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe REPOSITORY_FILE = 'ontohub_repositories.tar.gz'
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe # Use 'sudo' on most systems
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe attr_reader :db_name, :data_root, :backup_root, :backup_instance_dir
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe attr_reader :dry_run, :verbose, :sql_dump_as_db_user
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe def initialize(db_name, data_root, backup_root,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe verbose: false, dry_run: true, sql_dump_as_db_user: nil, user: nil)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe @data_dirs = DATA_DIRS.map { |dir| File.join(@data_root_basename, dir) }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov # We needed to create the directory for the script to continue later on.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov puts "Restored backup from #{backup_instance_dir}"
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov $stderr.puts "Nothing to prune: There is no backup directory."
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov backup_dirs_allowed_to_delete(Dir.new(backup_root).entries).each do |dir|
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if now - File.new(backup).ctime > BACKUPS_VALIDITY_TIME
rescue => e
puts <<-MSG