diff --git a/.gitignore b/.gitignore index 4b784703..2e971155 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,8 @@ # Never commit database backups to the codebase. /db/backups/* !/db/backups/.keep +/db/dumps/* +!/db/dumps/.keep # Ignore all the Puma related files given they are ephemeral and production only. /puma/* diff --git a/Gemfile b/Gemfile index 0f1f5f5f..b6be0fd2 100644 --- a/Gemfile +++ b/Gemfile @@ -39,9 +39,6 @@ gem 'byebug', groups: %i[development test] # For seeding the database with modular control and dependencies gem 'seedbank' -# For dumping database data into a YAML file (great for maintenance backups) -gem 'yaml_db' - # So we can upload files to Digital Ocean Spaces (S3-compatible) gem 'aws-sdk-s3' diff --git a/Gemfile.lock b/Gemfile.lock index 40ea2e6e..2c54828c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -211,9 +211,6 @@ GEM websocket-driver (0.7.3) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - yaml_db (0.7.0) - rails (>= 3.0) - rake (>= 0.8.7) zeitwerk (2.4.2) PLATFORMS @@ -241,7 +238,6 @@ DEPENDENCIES rubocop rubocop-rails seedbank - yaml_db RUBY VERSION ruby 3.0.0p0 diff --git a/app/controllers/concerns/json_api_controller.rb b/app/controllers/concerns/json_api_controller.rb index 6abcad8a..45376348 100644 --- a/app/controllers/concerns/json_api_controller.rb +++ b/app/controllers/concerns/json_api_controller.rb @@ -6,10 +6,6 @@ module JsonApiController extend ActiveSupport::Concern - included do - before_action :check_content_type - end - # CRUD # # In order to follow the JSON API specs, unsported request endpoints should @@ -547,15 +543,6 @@ def user_has_ability?(_action) true end - # VALIDATION - - # Check for the JSON API Content Type header. Throw 415 error if not present. - # Firefox has a known bug where they append "; charset=UTF-8" to the end, which breaks. - # https://josef.codes/firefox-adds-charset-utf-8-when-doing-post-requests-with-ajax/ - def check_content_type - wrong_content_type unless request.content_type.present? && request.content_type.split(';').first == 'application/vnd.api+json' - end - # ERRORS def unauthorized(meta = nil) @@ -633,14 +620,6 @@ def route_not_found(meta = nil) ) end - def wrong_content_type - render_error( - 415, - 'unsupported-media-type', - 'This API follows the JSON API standards and can therefor only accepts request with Content-Type "application/vnd.api+json".' - ) - end - def record_not_found(meta = nil) render_error( 404, diff --git a/db/dumps/.keep b/db/dumps/.keep new file mode 100644 index 00000000..e69de29b diff --git a/scripts/backup-remote.sh b/scripts/backup-remote.sh deleted file mode 100755 index ab525e8b..00000000 --- a/scripts/backup-remote.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash - -set -e -set -o pipefail - -timestamp=$1 - -( - set -x - cd /var/www/rails.api.interflux.com/ - bin/rails db:data:dump_dir dir=backups/$timestamp - tar --create --gzip --verbose --file db/backups/$timestamp.tar.gz db/backups/$timestamp/ -) diff --git a/scripts/backup.sh b/scripts/backup.sh index c954dbd1..0bf4c37b 100755 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -3,34 +3,28 @@ set -e set -o pipefail -# url=admin@frankfurt.server.interflux.com -server=frankfurt # Configured in ~/.ssh/config timestamp="$(date -u +"%Y-%m-%d-%H%M%S")-UTC" +server=frankfurt # See ~/.ssh/config +db_prod=interflux_production +db_dev=interflux_development echo "----------" -echo "Backing up Interflux database" +echo "Backing up database" echo "Timestamp: $timestamp" +echo "DB source: $db_prod @ $server" +echo "DB target: $db_dev @ localhost" echo "----------" - -( - set -x - scp scripts/backup-remote.sh $server:~/ - ssh $server "~/backup-remote.sh $timestamp; rm -f ~/backup-remote.sh" -) - +ssh $server "pg_dump -Fc --no-owner --no-privileges --verbose $db_prod" > db/dumps/$db_prod-$timestamp.dump echo "----------" -echo "Remote database backup complete." -echo "Copying database to local..." +echo "✅ Downloaded production database" echo "----------" - -( - set -x - scp $server:/var/www/rails.api.interflux.com/db/backups/$timestamp.tar.gz ./db/backups - tar --extract --file db/backups/$timestamp.tar.gz - bin/rails db:data:load_dir dir=backups/$timestamp - rm db/backups/$timestamp.tar.gz -) - +ls -la db/dumps +echo "----------" +RAILS_ENV=development rails db:drop db:create +echo "----------" +echo "✅ Reset local database" echo "----------" -echo "Done!" +pg_restore --clean --if-exists --no-owner --no-privileges --dbname=$db_dev db/dumps/$db_prod-$timestamp.dump echo "----------" +echo "✅ Synced local database with production" +echo "----------" \ No newline at end of file diff --git a/scripts/restore-backup-remote.sh b/scripts/restore-backup-remote.sh index d3357af9..9a1bea13 100755 --- a/scripts/restore-backup-remote.sh +++ b/scripts/restore-backup-remote.sh @@ -1,14 +1,16 @@ #!/usr/bin/env bash -set -e -set -o pipefail - -backup=$1 - -( - set -x - cd /var/www/api.interflux.com/builds/production/latest/ - cp -r "/var/www/api.interflux.com/db/$backup" ./db/ - export RAILS_ENV=production - bin/rails db:data:load_dir dir=$backup -) +# TODO: use pg_restore + +# set -e +# set -o pipefail + +# backup=$1 + +# ( +# set -x +# cd /var/www/api.interflux.com/builds/production/latest/ +# cp -r "/var/www/api.interflux.com/db/$backup" ./db/ +# export RAILS_ENV=production +# bin/rails db:data:load_dir dir=$backup +# ) diff --git a/scripts/restore-backup.sh b/scripts/restore-backup.sh index ea16db37..82809eff 100755 --- a/scripts/restore-backup.sh +++ b/scripts/restore-backup.sh @@ -1,22 +1,24 @@ #!/usr/bin/env bash -set -e -set -o pipefail +# TODO: use pg_restore -user=bot -server=server.interflux.com -backup=$1 +# set -e +# set -o pipefail -echo "----------" -echo "Restoring database backup $backup" -echo "----------" +# user=bot +# server=server.interflux.com +# backup=$1 -( - set -x - scp -i ~/.ssh/$user@$server remote/restore-backup-remote.sh $user@$server:~/ - ssh -i ~/.ssh/$user@$server $user@$server "~/restore-backup-remote.sh $backup; rm -f ~/backup-remote.sh" -) +# echo "----------" +# echo "Restoring database backup $backup" +# echo "----------" -echo "----------" -echo "Done!" -echo "----------" +# ( +# set -x +# scp -i ~/.ssh/$user@$server remote/restore-backup-remote.sh $user@$server:~/ +# ssh -i ~/.ssh/$user@$server $user@$server "~/restore-backup-remote.sh $backup; rm -f ~/backup-remote.sh" +# ) + +# echo "----------" +# echo "Done!" +# echo "----------" diff --git a/wiki/production.sh b/wiki/production.sh index fa173164..c4faf9c6 100644 --- a/wiki/production.sh +++ b/wiki/production.sh @@ -4,11 +4,11 @@ # Back up the production database -remote/backup.sh +script/backup.sh # Restore a production database -remote/restore-backup.sh "production-2022-03-09-013916-UTC" +script/restore-backup.sh "production-2022-03-09-013916-UTC" # Seed production database