backup revision f0264afd33a980b6584747fc8159ee950805d9e3
851N/A#!/usr/bin/env ruby
851N/A
1370N/A# Description
851N/A# This backup script creates and restores backups of ontohub data. It includes:
1445N/A# * bare git repositories (data/repositories)
851N/A# * named symlinks to git repositories (data/git_daemon)
851N/A# * the postgres database
919N/A#
919N/A# Usage
919N/A# To create a backup, run this script with the argument `create`:
919N/A# $ script/backup create
919N/A# Then a backup named with the current date and time is created in the
919N/A# backup directory (see below).
919N/A#
919N/A# To restore a backup, run this script with the argument `restore <backup name>`
919N/A# $ script/backup restore 2015-01-01_00-00
919N/A# Then the selected backup is fully restored
919N/A#
919N/A# Backup directory
919N/A# For development machines, the backup directory is:
919N/A# <rails root>/tmp/backup/
919N/A# And for production machines, the backup directory is:
919N/A# /home/ontohub/ontohub_data_backup
919N/A#
851N/A# Super user privileges
851N/A# To create and restore, we need root privileges. Otherwise file modes are not
851N/A# preserved. This script will call `sudo` when needed and inform you about the
851N/A# reason for calling `sudo`. If you don't allow sudo, a backup will be created
851N/A# or restored anyway, but the file modes and ownership are not preserved.
851N/A# Then, you need to adjust them manually.
851N/A#
1445N/A# Maintenance mode
851N/A# While backing up and restoring the data, the maintenance mode is activated.
911N/A# This way we guarantee data consistency of the backup.
1445N/A
1445N/A
1445N/Arequire 'tmpdir.rb'
911N/Arequire 'fileutils'
1281N/Arequire 'pathname'
1447N/Arequire 'open3'
1281N/A
851N/Amodule Backup
851N/A class Backup
851N/A # Amount of backups that have to be there at least
851N/A BACKUPS_COUNT = 30
851N/A # Backups are kept for at least 365 days
1179N/A BACKUPS_VALIDITY_TIME = 365 * 60 * 60 * 24
851N/A
1447N/A MAINTENANCE_FILE = 'maintenance.txt'
1447N/A
851N/A SQL_DUMP_FILE = 'ontohub_sql_dump.postgresql'
1088N/A REPOSITORY_FILE = 'ontohub_repositories.tar.gz'
1088N/A
1088N/A DATA_DIRS = %w(data/repositories data/git_daemon)
1088N/A
1088N/A attr_reader :db_name, :data_root, :backup_root, :backup_instance_dir
851N/A attr_reader :dry_run, :verbose, :sql_dump_as_postgres_user
1097N/A
1097N/A def initialize(db_name, data_root, backup_root,
1097N/A verbose: false, dry_run: true, sql_dump_as_postgres_user: false)
1097N/A @db_name = db_name
1097N/A @data_root = Pathname.new(data_root)
851N/A @backup_root = Pathname.new(backup_root)
851N/A
1088N/A @dry_run = dry_run
1088N/A @verbose = verbose
1088N/A @sql_dump_as_postgres_user = sql_dump_as_postgres_user
1088N/A end
1088N/A
1088N/A def create
1088N/A puts 'Creating backup...'
1088N/A enable_maintenance_mode
initialize_backup
create_sql_dump
create_repository_archive
# We needed to create the directory for the script to continue later on.
Dir.rmdir(backup_instance_dir) if dry_run
disable_maintenance_mode
puts "Created backup in #{backup_instance_dir}"
self.class.prune(backup_root)
end
def restore(backup_name)
enable_maintenance_mode
initialize_restore(backup_name)
restore_sql_dump
restore_repository_archive
disable_maintenance_mode
puts "Restored backup from #{backup_instance_dir}"
end
def self.prune(backup_root)
if !Dir.exists?(backup_root)
$stderr.puts "Nothing to prune: There is no backup directory."
return
end
now = Time.now
backup_dirs_allowed_to_delete(Dir.new(backup_root).entries).each do |dir|
backup = backup_root.join(dir)
if now - File.new(backup).ctime > BACKUPS_VALIDITY_TIME
puts "removing old backup: #{dir}"
FileUtils.rm_r(backup)
end
end
end
protected
def new_backup_name
Time.now.strftime("%Y-%m-%d_%H-%M-%S")
end
def initialize_backup
@backup_instance_dir = backup_root.join(new_backup_name)
puts "FileUtils.mkdir_p #{backup_instance_dir}" if verbose
# Create directory even in dry run to let the script continue.
FileUtils.mkdir_p(backup_instance_dir)
end
def create_sql_dump
puts 'Creating SQL dump...'
Dir.chdir(backup_instance_dir) do
exec('pg_dump', *pg_user_switch, '-Fc', db_name,
file_dest: SQL_DUMP_FILE)
end
end
def create_repository_archive
puts 'Creating repository archive...'
Dir.chdir(data_root.join('..')) do
archive_file = backup_instance_dir.join(REPOSITORY_FILE)
exec('tar', verbose ? '-v' : '', '-czf', archive_file.to_s,
*DATA_DIRS.map(&:to_s))
end
end
def initialize_restore(backup_name)
@backup_instance_dir = backup_root.join(backup_name)
unless Dir.exists?(backup_instance_dir)
$stderr.puts (
"Error: Backup '#{backup_name}' does not exist in #{backup_root}.")
exit
end
end
def restore_sql_dump
'Restoring SQL dump...'
Dir.chdir(backup_instance_dir) do
exec('pg_restore', '-c', *pg_user_switch, '-d', db_name, SQL_DUMP_FILE)
end
end
def restore_repository_archive
puts 'Restoring repository archive...'
Dir.chdir(data_root.join('..')) do
tmpdir = Dir.mktmpdir
move_data_dirs_to_tmpdir(tmpdir)
begin
extract_archive
remove_tmpdir(tmpdir)
rescue => e
puts <<-MSG
An error occured while restoring the repositories:
#{e.message}
You can find the pre-restore repositories at #{tmpdir}
Do something about it.
MSG
raise e
end
end
end
def move_data_dirs_to_tmpdir(tmpdir)
puts "FileUtils.mv(#{DATA_DIRS}, #{tmpdir})" if verbose
FileUtils.mv(DATA_DIRS, tmpdir) unless dry_run
rescue Errno::EACCES
puts <<-MSG
As the current user I have no access to move the repository data
directories #{DATA_DIRS.join(' ')} to a temporary directory #{tmpdir}.
This is used as a backup for the case of an error while restoring.
To continue, I try the command again using sudo.
MSG
exec('sudo', 'mv', *DATA_DIRS.map(&:to_s), tmpdir)
end
def extract_archive
archive_file = backup_instance_dir.join(REPOSITORY_FILE)
puts <<-MSG
Super user privileges are needed to reset the file permissions as
they were before the backup. If you refuse to enter the password
(Crtl-C) or enter a wrong password, only the permissions will not be
restored and all restored files will belong to the current user/group.
MSG
try_as_sudo_with_fallback('tar', verbose ? '-v' : '', '-xzf',
archive_file.to_s, *DATA_DIRS.map(&:to_s))
end
def remove_tmpdir(tmpdir)
puts "FileUtils.remove_entry(#{tmpdir})" if verbose
FileUtils.remove_entry(tmpdir) # even do this in dry run
rescue Errno::EACCES
puts <<-MSG
As the current user I have no access to remove the temporary
directory #{tmpdir}.
To continue, I try the command again using sudo.
MSG
exec('sudo', 'rm', '-r', tmpdir)
end
def enable_maintenance_mode
puts 'Enabling maintenance mode...'
puts "FileUtils.touch #{maintenance_file}" if verbose
FileUtils.touch maintenance_file unless dry_run
end
def disable_maintenance_mode
puts 'Disabling maintenance mode...'
puts "FileUtils.rm #{maintenance_file}" if verbose
FileUtils.rm maintenance_file unless dry_run
end
def exec(*args, file_dest: nil)
puts "[executing next command in #{Dir.getwd}]" if verbose
out = args.join(' ')
out << " > #{file_dest}" if file_dest
puts out if verbose
Subprocess.run(*args, file_dest: file_dest) unless dry_run
end
def try_as_sudo_with_fallback(*args)
_out, _err, exit_code = exec('sudo', *args)
unless exit_code.success?
sudo_not_given_fallback(*args) # Wrong sudo password
end
rescue Exception => e
raise e unless e.is_a?(Interrupt) # Ctrl-C when asked for password
sudo_not_given_fallback(*args)
end
def sudo_not_given_fallback(*args)
puts 'Super user privileges not granted. Trying as normal user.'
exec(*args)
end
def maintenance_file
data_root.join(MAINTENANCE_FILE)
end
def pg_user_switch
sql_dump_as_postgres_user ? %w(-U postgres) : []
end
def self.backup_dirs_allowed_to_delete(entries)
entries.reject{ |entry| %w(. ..).include?(entry) }[0..-(BACKUPS_COUNT+1)]
end
end
class Subprocess
def self.run(*args, file_dest: nil)
stdin, stdout, stderr, wait_thr, io_dest = run_streaming(*args,
file_dest: file_dest)
exit_code = wait_thr.value # wait for the process to finish
io_dest.close if io_dest
[stdout.read, stderr.read, exit_code]
end
def self.run_streaming(*args, file_dest: nil)
stdin, stdout, stderr, wait_thr = Open3.popen3(*args)
io_dest = nil
if file_dest
io_dest = File.open(file_dest, 'w')
IO.copy_stream(stdout, io_dest)
end
[stdin, stdout, stderr, wait_thr, io_dest]
end
end
end
def data_root(rails_root)
File.realpath(rails_root.join('data'))
end
def on_development_system?(rails_root)
!File.symlink?(rails_root.join('data'))
end
# We assume, this script runs in "RAILS_ROOT/script/".
RAILS_ROOT = Pathname.new(__FILE__).dirname.join('..')
DATABASE = if on_development_system?(RAILS_ROOT)
'ontohub_development'
else
'ontohub'
end
BACKUP_ROOT = if on_development_system?(RAILS_ROOT)
RAILS_ROOT.join('tmp', 'backup')
else
File.realpath('/home/ontohub/ontohub_data_backup')
end
backup = Backup::Backup.new(DATABASE, data_root(RAILS_ROOT), BACKUP_ROOT,
sql_dump_as_postgres_user: on_development_system?(RAILS_ROOT),
dry_run: false, verbose: true)
case ARGV.first
when 'create'
backup.create
when 'restore'
if ARGV.length == 1
$stderr.puts(
'To restore a backup, you need to specify one with the arguments')
$stderr.puts('"restore backup_name"')
exit
end
backup_name = ARGV[1]
backup.restore(backup_name)
when 'prune'
Backup::Backup.prune(BACKUP_ROOT)
else
$stderr.puts 'unknown or missing parameter'
$stderr.puts 'use parameter "create" or "restore <backup_name>" or "prune"'
exit
end