backup revision 28001d576e67ba46ed481c5695f1e0827ff26007
919N/A # Amount of backups that have to be there at least 919N/A # Backups are valid for 7 days 98N/A # We needed to create the directory for the script to continue later on. 98N/A 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. puts "Error: Backup '#{backup_name}' does not exist in #{backup_root}." 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 def exec(*args, file_dest: nil) puts "[executing next command in #{Dir.getwd}]" if verbose out << " > #{file_dest}" if file_dest Subprocess.run(*args, file_dest: file_dest) unless dry_run def try_as_sudo_with_fallback(*args) unless exec('sudo', *args) 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_postgres_user ? ' -U postgres' : '' def self.backup_dirs_allowed_to_delete(entries) entries.reject{ |entry| %w(. ..).include?(entry) }[0..-(BACKUPS_COUNT+1)] def self.run(*args, file_dest: nil) stdin, stdout, stderr, wait_thr, io_dest = run_streaming(*args, exit_code = wait_thr.value # wait for the process to finish [stdout.read, stderr.read, exit_code] def self.run_streaming(*args, file_dest: nil) stdin, stdout, stderr, wait_thr = Open3.popen3(*args) io_dest = File.open(file_dest, 'w') IO.copy_stream(stdout, io_dest) [stdin, stdout, stderr, wait_thr, io_dest] 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"'