backup revision 7e19fab3af21714e410863295a27b859111ba40d
168N/A# The first line is for deployment machines only. For local machines, use: 168N/A# You can find more extensive documentation of this script at 168N/A# This backup script creates and restores backups of ontohub data. It includes: 168N/A# * the postgres database 168N/A# First note: Run this as the root user, e.g. with sudo. 168N/A# To create a backup, run this script with the argument `create`: 168N/A# Then a backup named with the current date and time is created in the 168N/A# backup directory (see below). 168N/A# To restore a backup, run this script with the argument `restore <backup name>` 168N/A# Then the selected backup is fully restored 168N/A# For development machines, the backup directory is: 168N/A# And for production machines, the backup directory is: 168N/A# To create and restore, we need root privileges. Otherwise file modes are not 168N/A# preserved. This script will call `sudo` when needed and inform you about the 168N/A# reason for calling `sudo`. If you don't allow sudo, a backup will be created 168N/A# or restored anyway, but the file modes and ownership are not preserved. 168N/A# Then, you need to adjust them manually. 168N/A# While backing up and restoring the data, the maintenance mode is activated. 168N/A# This way we guarantee data consistency of the backup. 168N/A # Amount of backups that have to be there at least 168N/A # Backups are kept for at least 365 days 168N/A # Use 'sudo' on most systems 168N/A # We needed to create the directory for the script to continue later on. 168N/A 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}.") exec('pg_restore', '-n', 'public', 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('mv', *@data_dirs, tmpdir, user: 'root') 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) 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('rm', '-r', tmpdir, user: 'root') 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 # Execute a command as the given user. def exec(*args, user: nil) print "[executing next command in #{Dir.getwd}" if verbose print " as user #{user}" if verbose && user exec_system(*[sudo, *args]) # This looks strange because of the double sudo. # It is needed on our deployment machines to get the environment right. exec_system(*['+', 'sudo', '-u', user, 'bash', '-c', "cd #{Dir.getwd} && #{escape_arguments(args)}"]) # puts args.join(' ') # For debugging def escape_arguments(args) rest = args[1..-1].map do |arg| if arg.to_s.include?(' ') %("#{arg.gsub('"', '\"')}") ([args[0]] + rest).join(' ') data_root.join(MAINTENANCE_FILE) sql_dump_as_db_user ? %W(-U #{sql_dump_as_db_user}) : [] 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 a normal user is disabled.' puts 'Please run it as root.' # 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: on_development_system?(RAILS_ROOT) ? 'postgres' : 'ontohub', user: USER, group: GROUP, 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"'