backup revision 0b99be5ced371cca00283694e1bd53a8ac0d7b5d
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksarequire 'fileutils'
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksarequire 'pathname'
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa DATA_DIRS = %w(data/repositories data/git_daemon)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa attr_reader :db_name, :data_root, :backup_root, :backup_instance_dir
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa attr_reader :dry_run, :verbose, :sql_dump_without_su
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa verbose: false, dry_run: true, sql_dump_without_su: false)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa # We needed to create the directory for the script to continue later on.
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa puts "restored backup from #{backup_instance_dir}"
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa @backup_instance_dir = backup_root.join(new_backup_name)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa puts "FileUtils.mkdir_p #{backup_instance_dir}" if verbose
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa # Create directory even in dry run to let the script continue.
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa exec "pg_dump -U postgres -Fc #{db_name} > #{SQL_DUMP_FILE}"
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa exec "sudo -u postgres pg_dump -Fc #{db_name} > #{SQL_DUMP_FILE}"
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa archive_file = backup_instance_dir.join(REPOSITORY_FILE)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa exec(["tar -czvf #{archive_file}", *DATA_DIRS].join(' '))
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa @backup_instance_dir = backup_root.join(backup_name)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa puts "Error: Backup '#{backup_name}' does not exist in #{backup_root}."
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa exec "pg_restore -c -U postgres -d #{db_name} #{SQL_DUMP_FILE}"
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa exec "sudo -u postgres pg_restore -c -d #{db_name} #{SQL_DUMP_FILE}"
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa archive_file = backup_instance_dir.join(REPOSITORY_FILE)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa exec(["tar -xzvf #{archive_file}", *DATA_DIRS].join(' '))
486df98bbf3348cfb96e93c3e499d12435880bb5Eugen Kuksa puts "FileUtils.touch #{maintenance_file}" if verbose
486df98bbf3348cfb96e93c3e499d12435880bb5Eugen Kuksa puts "FileUtils.rm #{maintenance_file}" if verbose
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa puts "[executing next command in #{Dir.getwd}]" if verbose
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa# We assume, this script runs in "RAILS_ROOT/script/".
daf3e28fff47a65b53d6fb65155301763b9f166eEugen KuksaRAILS_ROOT = Pathname.new(__FILE__).dirname.join('..')
daf3e28fff47a65b53d6fb65155301763b9f166eEugen KuksaDATABASE = on_development_system?(RAILS_ROOT) ? 'ontohub_development' : 'ontohub'
0b99be5ced371cca00283694e1bd53a8ac0d7b5dEugen KuksaBACKUP_ROOT = File.realpath('/home/ontohub/ontohub_data_backup')
b1fe9054ad7c7192fe4c474363247dad15963e99Eugen Kuksabackup = Backup::Backup.new(DATABASE, data_root(RAILS_ROOT), BACKUP_ROOT,
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa sql_dump_without_su: on_development_system?(RAILS_ROOT),
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa dry_run: false, verbose: true)
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa puts 'To restore a backup, you need to specify one with the arguments'
daf3e28fff47a65b53d6fb65155301763b9f166eEugen Kuksa puts 'use parameter "create" or "restore <backup_name>"'