backup revision f9e467b8fe6fef705eec2989b20e92eaa9d917e1
3387N/A# You can find more extensive documentation of this script at 3387N/A# This backup script creates and restores backups of ontohub data. It includes: 3387N/A# First note: Run this as the ontohub user, *not* as root. 3387N/A# To create a backup, run this script with the argument `create`: 3387N/A# Then a backup named with the current date and time is created in the 3387N/A# backup directory (see below). 3387N/A# To restore a backup, run this script with the argument `restore <backup name>` 3387N/A# Then the selected backup is fully restored 3387N/A# For development machines, the backup directory is: 3387N/A# And for production machines, the backup directory is: 3387N/A# To create and restore, we need root privileges. Otherwise file modes are not 3387N/A# preserved. This script will call `sudo` when needed and inform you about the 3387N/A# reason for calling `sudo`. If you don't allow sudo, a backup will be created 3387N/A# or restored anyway, but the file modes and ownership are not preserved. 3387N/A# Then, you need to adjust them manually. 3387N/A# While backing up and restoring the data, the maintenance mode is activated. 3387N/A# This way we guarantee data consistency of the backup. 3387N/A # Amount of backups that have to be there at least 3387N/A # Backups are kept for at least 365 days 3387N/A # We needed to create the directory for the script to continue later on. 3387N/A puts "Created backup in #{backup_instance_dir}" 3387N/A puts "Restored backup from #{backup_instance_dir}" 3387N/A # Create directory even in dry run to let the script continue. 3387N/A "Error: Backup '#{backup_name}' does not exist in #{backup_root}.") 3387N/A exec('pg_restore', '-n', 'public', 3387N/AAn error occured while restoring the repositories: 3387N/AYou can find the pre-restore repositories at #{tmpdir} 3387N/A def move_data_dirs_to_tmpdir(tmpdir) 3387N/A puts "FileUtils.mv(#{@data_dirs}, #{tmpdir})" if verbose 3387N/A FileUtils.mv(@data_dirs, tmpdir) unless dry_run 3387N/AAs the current user I have no access to move the repository data 3387N/Adirectories #{@data_dirs.join(' ')} to a temporary directory #{tmpdir}. 3387N/AThis is used as a backup for the case of an error while restoring. 3387N/ATo continue, I try the command again using sudo. 3387N/A exec('sudo', 'mv', *@data_dirs, tmpdir) 3387N/A archive_file = backup_instance_dir.join(REPOSITORY_FILE) 3387N/ASuper user privileges are needed to reset the file permissions as 3387N/Athey were before the backup. If you refuse to enter the password 3387N/A(Ctl-C) or enter a wrong password, only the permissions will not be 3387N/Arestored and all restored files will belong to the current user/group. 3387N/A try_as_sudo_with_fallback('tar', verbose ? '-v' : '', '-xf', 3387N/A archive_file.to_s, *@data_dirs) 3387N/A puts "FileUtils.remove_entry(#{tmpdir})" if verbose 3387N/A FileUtils.remove_entry(tmpdir) # even do this in dry run 3387N/AAs the current user I have no access to remove the temporary 3387N/ATo continue, I try the command again using sudo. 3387N/A exec('sudo', 'rm', '-r', tmpdir) 3387N/A def enable_maintenance_mode 3387N/A puts 'Enabling maintenance mode...' 3387N/A if File.exist?(maintenance_file) 3387N/A $stderr.puts 'Maintenance mode was already enabled.' 3387N/A $stderr.puts "Please check the file #{maintenance_file}" 3387N/A puts "FileUtils.touch #{maintenance_file}" if verbose 3387N/A FileUtils.touch maintenance_file unless dry_run 3387N/A def disable_maintenance_mode 3387N/A puts 'Disabling maintenance mode...' puts "FileUtils.rm #{maintenance_file}" if verbose FileUtils.rm maintenance_file unless dry_run puts "[executing next command in #{Dir.getwd}]" if verbose system(*args) unless dry_run 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 raise e unless e.is_a?(Interrupt) # Ctrl-C when asked for password sudo_not_given_fallback(*args) def sudo_not_given_fallback(*args) puts 'Super user privileges not granted. Trying as normal user.' data_root.join(MAINTENANCE_FILE) sql_dump_as_db_user ? %w(-U ontohub) : [] def self.backup_dirs_allowed_to_delete(entries) entries.reject{ |entry| %w(. ..).include?(entry) }[0..-(BACKUPS_COUNT+1)] def data_root(rails_root) if on_development_system?(rails_root) File.realpath(rails_root.join('data')) ENV['DATA_ROOT'] ||'/data/git' def on_development_system?(rails_root) data_path = rails_root.join('data') File.exist?(data_path) && !File.symlink?(data_path) # Don't allow this to be run as the root user. puts 'Running this script as the root user is disabled.' puts 'Please run it as a normal user that has sudo privileges, e.g. ontohub.' # We assume, this script runs in "RAILS_ROOT/script/". RAILS_ROOT = Pathname.new(__FILE__).dirname.join('..') BACKUP_ROOT_PRODUCTION = '/local/home/ontohub/ontohub_data_backup' if on_development_system?(RAILS_ROOT) if on_development_system?(RAILS_ROOT) RAILS_ROOT.join('tmp', 'backup') File.realpath(BACKUP_ROOT_PRODUCTION) backup = Backup::Backup.new(DATABASE, data_root(RAILS_ROOT), BACKUP_ROOT, sql_dump_as_db_user: true, dry_run: false, verbose: true) 'To restore a backup, you need to specify one with the arguments') $stderr.puts('"restore backup_name"') backup.restore(backup_name) Backup::Backup.prune(BACKUP_ROOT) $stderr.puts 'unknown or missing parameter' $stderr.puts 'use parameter "create" or "restore <backup_name>" or "prune"'