backup revision b51057b860560bf3ee454c03a121af3d5d34f482
# We needed to create the directory for the script to continue later on. puts "created backup in #{backup_instance_dir}" puts "restored backup from #{backup_instance_dir}" puts "Nothing to prune: There is no backup directory." puts "removing old backup: #{dir}" # Create directory even in dry run to let the script continue. exec "pg_dump#{pg_user_switch} -Fc #{db_name} > #{SQL_DUMP_FILE}" puts "Error: Backup '#{backup_name}' does not exist in #{backup_root}." exec "pg_restore -c#{pg_user_switch} -d #{db_name} #{SQL_DUMP_FILE}" An error occured while restoring the repositories: You can find the pre-restore repositories at #{tmpdir} def move_data_dirs_to_tmpdir(tmpdir) puts "FileUtils.mv(#{DATA_DIRS}, #{tmpdir})" if verbose FileUtils.mv(DATA_DIRS, tmpdir) unless dry_run 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. exec('sudo', 'mv', *DATA_DIRS.map(&:to_s), tmpdir) archive_file = backup_instance_dir.join(REPOSITORY_FILE) 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. try_as_sudo_with_fallback('tar', '-xzvf', archive_file.to_s, def remove_tmpdir(tmpdir) puts "FileUtils.remove_entry(#{tmpdir})" if verbose FileUtils.remove_entry(tmpdir) # even do this in dry run As the current user I have no access to remove the temporary To continue, I try the command again using sudo. exec('sudo', 'rm', '-r', tmpdir) def enable_maintenance_mode puts "FileUtils.touch #{maintenance_file}" if verbose FileUtils.touch maintenance_file unless dry_run def disable_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(command) unless dry_run def try_as_sudo_with_fallback(command) unless exec("sudo #{command}") sudo_not_given_fallback(command) # Ctrl-C when asked for password raise e unless e.is_a?(Interrupt) sudo_not_given_fallback(command) def sudo_not_given_fallback(command) puts 'Super user privileges not granted. Trying as normal user.' data_root.join(MAINTENANCE_FILE) sql_dump_as_postgres_user ? ' -U postgres' : '' def self.backup_dirs_allowed_to_delete(entries) entries.reject{ |entry| %w(. ..).include?(entry) }[0..-(BACKUPS_COUNT+1)] def data_root(rails_root) File.realpath(rails_root.join('data')) def on_development_system?(rails_root) !File.symlink?(rails_root.join('data')) # We assume, this script runs in "RAILS_ROOT/script/". RAILS_ROOT = Pathname.new(__FILE__).dirname.join('..') DATABASE = if on_development_system?(RAILS_ROOT) BACKUP_ROOT = if on_development_system?(RAILS_ROOT) RAILS_ROOT.join('tmp', 'backup') File.realpath('/home/ontohub/ontohub_data_backup') 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) puts 'To restore a backup, you need to specify one with the arguments' puts '"restore backup_name"' backup.restore(backup_name) Backup::Backup.prune(BACKUP_ROOT) puts 'unknown or missing parameter' puts 'use parameter "create" or "restore <backup_name>" or "prune"'