backup revision abc834c020080fe44a1ea4e34278327e99e3e12e
0N/A# You can find more extensive documentation of this script at 0N/A# This backup script creates and restores backups of ontohub data. It includes: 0N/A# * the postgres database 0N/A# First note: Run this as the ontohub user, *not* as root. 0N/A# To create a backup, run this script with the argument `create`: 0N/A# Then a backup named with the current date and time is created in the 0N/A# backup directory (see below). 0N/A# To restore a backup, run this script with the argument `restore <backup name>` # Then the selected backup is fully restored # For development machines, the backup directory is: # And for production machines, the backup directory is: # To create and restore, we need root privileges. Otherwise file modes are not # preserved. This script will call `sudo` when needed and inform you about the # reason for calling `sudo`. If you don't allow sudo, a backup will be created # or restored anyway, but the file modes and ownership are not preserved. # Then, you need to adjust them manually. # While backing up and restoring the data, the maintenance mode is activated. # This way we guarantee data consistency of the backup. # Amount of backups that have to be there at least # Backups are kept for at least 365 days puts 'Creating backup...' # 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 (Ctl-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' : '', '-xf', 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...' if File.exist?(maintenance_file) $stderr.puts 'Maintenance mode was already enabled.' $stderr.puts "Please check the file #{maintenance_file}" 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 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_postgres_user ? %w(-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')) # 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 = '/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_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"'