backup revision f0264afd33a980b6584747fc8159ee950805d9e3
851N/A# This backup script creates and restores backups of ontohub data. It includes: 851N/A# * the postgres database 919N/A# To create a backup, run this script with the argument `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# To restore a backup, run this script with the argument `restore <backup name>` 919N/A# Then the selected backup is fully restored 919N/A# For development machines, the backup directory is: 919N/A# And for production machines, the backup directory is: 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# While backing up and restoring the data, the maintenance mode is activated. 911N/A# This way we guarantee data consistency of the backup. 851N/A # Amount of backups that have to be there at least 851N/A # Backups are kept for at least 365 days # 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}" $stderr.
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 'Creating SQL dump...' puts 'Creating repository archive...' "Error: Backup '#{backup_name}' does not exist in #{backup_root}.") puts 'Restoring repository archive...' 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', verbose ? '-v' : '', '-xzf', archive_file.to_s, *DATA_DIRS.map(&: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 'Enabling maintenance mode...' puts "FileUtils.touch #{maintenance_file}" if verbose FileUtils.touch maintenance_file unless dry_run def disable_maintenance_mode puts 'Disabling 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) _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_postgres_user ? %w(-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) '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"'