Skip to content

Instantly share code, notes, and snippets.

@cygnan
Last active May 17, 2020 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cygnan/66526070d8771e99f96b385fa8614fc5 to your computer and use it in GitHub Desktop.
Save cygnan/66526070d8771e99f96b385fa8614fc5 to your computer and use it in GitHub Desktop.
#!/bin/bash

set -eu

if [ "${EUID}" -ne 0 ]; then
  echo "Error: Please run as root"
  exit 1
fi

set -x

rm -rf ~/live/public/packs

sudo -u mastodon bash <<'_EOS_'
set -eux

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

DATE="$(date '+%Y%m%d%H%M%S')"
pg_dump --format=custom --no-privileges --schema=public --blobs --dbname=mastodon_production --file ~/db_backup/db-"${DATE}".dump

RUBY_VERSION="$(cat ~/live/.ruby-version)"

if [ "${RUBY_VERSION}" != "$(cat ~/.rbenv/version)" ]; then
  git -C ~/.rbenv pull
  git -C ~/.rbenv/plugins/ruby-build pull
  RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install "${RUBY_VERSION}"
  rbenv global "${RUBY_VERSION}"
  gem update --system --no-document
  BUNDLER_VERSION="$(cat Gemfile.lock | grep -A 1 'BUNDLED WITH' | tail -n 1 | sed 's/^ *//;s/ *$//')"
  gem install --force bundler -v "${BUNDLER_VERSION}" --no-document
fi

bundle install -j$(getconf _NPROCESSORS_ONLN) --deployment --without development test
yarn install --pure-lockfile
RAILS_ENV=production bundle exec rails assets:precompile

for d in packs assets emoji; do
  gsutil -m -h "Cache-Control:public, max-age=31536000, immutable" rsync -d -r -c -a public-read ~/live/public/"${d}" gs://mstdn-cygnan-com/"${d}"
done
_EOS_

systemctl stop mastodon-{web,sidekiq,streaming}.service

sleep 10

systemctl stop pgbouncer.service

sudo -u mastodon bash <<'_EOS_'
set -eux

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

RAILS_ENV=production DB_PORT=5432 bundle exec rails db:migrate
_EOS_

systemctl start {mastodon-{web,sidekiq,streaming},pgbouncer}.service

echo 'Update successfully completed.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment