backup revision bacf95fddc53ab3107f380c4c816fa8072358bd9
a466f24ecf94b453750f9791ad1140556114711bvboxsync# Description
a466f24ecf94b453750f9791ad1140556114711bvboxsync# This backup script creates and restores backups of ontohub data. It includes:
a466f24ecf94b453750f9791ad1140556114711bvboxsync# * bare git repositories (data/repositories)
c97989161fbe75bc14cea477a5443bbf474dd3advboxsync# * named symlinks to git repositories (data/git_daemon)
a466f24ecf94b453750f9791ad1140556114711bvboxsync# * the postgres database
a466f24ecf94b453750f9791ad1140556114711bvboxsync# First note: Run this as the ontohub user, *not* as root.
a466f24ecf94b453750f9791ad1140556114711bvboxsync# To create a backup, run this script with the argument `create`:
a466f24ecf94b453750f9791ad1140556114711bvboxsync# Then a backup named with the current date and time is created in the
a466f24ecf94b453750f9791ad1140556114711bvboxsync# backup directory (see below).
a466f24ecf94b453750f9791ad1140556114711bvboxsync# To restore a backup, run this script with the argument `restore <backup name>`
a466f24ecf94b453750f9791ad1140556114711bvboxsync# $ script/backup restore 2015-01-01_00-00
a466f24ecf94b453750f9791ad1140556114711bvboxsync# Then the selected backup is fully restored
a466f24ecf94b453750f9791ad1140556114711bvboxsync# Backup directory
a466f24ecf94b453750f9791ad1140556114711bvboxsync# For development machines, the backup directory is:
a466f24ecf94b453750f9791ad1140556114711bvboxsync# <rails root>/tmp/backup/
a466f24ecf94b453750f9791ad1140556114711bvboxsync# And for production machines, the backup directory is:
a466f24ecf94b453750f9791ad1140556114711bvboxsync# Super user privileges
a466f24ecf94b453750f9791ad1140556114711bvboxsync# To create and restore, we need root privileges. Otherwise file modes are not
a466f24ecf94b453750f9791ad1140556114711bvboxsync# preserved. This script will call `sudo` when needed and inform you about the
a466f24ecf94b453750f9791ad1140556114711bvboxsync# reason for calling `sudo`. If you don't allow sudo, a backup will be created
a466f24ecf94b453750f9791ad1140556114711bvboxsync# or restored anyway, but the file modes and ownership are not preserved.
a466f24ecf94b453750f9791ad1140556114711bvboxsync# Then, you need to adjust them manually.
a466f24ecf94b453750f9791ad1140556114711bvboxsync# Maintenance mode
a466f24ecf94b453750f9791ad1140556114711bvboxsync# While backing up and restoring the data, the maintenance mode is activated.
a466f24ecf94b453750f9791ad1140556114711bvboxsync# This way we guarantee data consistency of the backup.
a466f24ecf94b453750f9791ad1140556114711bvboxsyncrequire 'fileutils'
a466f24ecf94b453750f9791ad1140556114711bvboxsyncrequire 'pathname'
a466f24ecf94b453750f9791ad1140556114711bvboxsyncrequire 'open3'
a466f24ecf94b453750f9791ad1140556114711bvboxsync # Amount of backups that have to be there at least
a466f24ecf94b453750f9791ad1140556114711bvboxsync # Backups are kept for at least 365 days
a466f24ecf94b453750f9791ad1140556114711bvboxsync attr_reader :db_name, :data_root, :backup_root, :backup_instance_dir
a466f24ecf94b453750f9791ad1140556114711bvboxsync attr_reader :dry_run, :verbose, :sql_dump_as_postgres_user
a466f24ecf94b453750f9791ad1140556114711bvboxsync verbose: false, dry_run: true, sql_dump_as_postgres_user: false)
a466f24ecf94b453750f9791ad1140556114711bvboxsync @sql_dump_as_postgres_user = sql_dump_as_postgres_user
eb270e8b46c0ad30cdffc6b75fc8dffef9738fbdvboxsync # We needed to create the directory for the script to continue later on.
a466f24ecf94b453750f9791ad1140556114711bvboxsync $stderr.puts "Nothing to prune: There is no backup directory."
a466f24ecf94b453750f9791ad1140556114711bvboxsync backup_dirs_allowed_to_delete(Dir.new(backup_root).entries).each do |dir|
a466f24ecf94b453750f9791ad1140556114711bvboxsync if now - File.new(backup).ctime > BACKUPS_VALIDITY_TIME
a466f24ecf94b453750f9791ad1140556114711bvboxsync @backup_instance_dir = backup_root.join(new_backup_name)
a466f24ecf94b453750f9791ad1140556114711bvboxsync puts "FileUtils.mkdir_p #{backup_instance_dir}" if verbose
a466f24ecf94b453750f9791ad1140556114711bvboxsync # Create directory even in dry run to let the script continue.
a466f24ecf94b453750f9791ad1140556114711bvboxsync exec('pg_dump', *pg_user_switch, '-Fc', db_name, '-f', SQL_DUMP_FILE)
fec6782953105e154c193c1be4a761da1c148d6fvboxsync archive_file = backup_instance_dir.join(REPOSITORY_FILE)
11ab86db9b44d06e180fbf806c9d1915be136d1evboxsync exec('tar', verbose ? '-v' : '', '-cf', archive_file.to_s,
a466f24ecf94b453750f9791ad1140556114711bvboxsync @backup_instance_dir = backup_root.join(backup_name)
a466f24ecf94b453750f9791ad1140556114711bvboxsync "Error: Backup '#{backup_name}' does not exist in #{backup_root}.")
a466f24ecf94b453750f9791ad1140556114711bvboxsync 'Restoring SQL dump...'
a466f24ecf94b453750f9791ad1140556114711bvboxsync exec('pg_restore', '-c', *pg_user_switch, '-d', db_name, SQL_DUMP_FILE)
rescue => e
puts <<-MSG