Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
Expand Down
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -241,7 +238,6 @@ DEPENDENCIES
rubocop
rubocop-rails
seedbank
yaml_db

RUBY VERSION
ruby 3.0.0p0
Expand Down
21 changes: 0 additions & 21 deletions app/controllers/concerns/json_api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
Empty file added db/dumps/.keep
Empty file.
13 changes: 0 additions & 13 deletions scripts/backup-remote.sh

This file was deleted.

38 changes: 16 additions & 22 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "----------"
26 changes: 14 additions & 12 deletions scripts/restore-backup-remote.sh
Original file line number Diff line number Diff line change
@@ -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
# )
34 changes: 18 additions & 16 deletions scripts/restore-backup.sh
Original file line number Diff line number Diff line change
@@ -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 "----------"
4 changes: 2 additions & 2 deletions wiki/production.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading