Skip to content

Commit

Permalink
shop shelling out for backup
Browse files Browse the repository at this point in the history
also gzip inside github actions because the server only has one tiny cpu
  • Loading branch information
indirect committed Oct 5, 2024
1 parent 4732032 commit cf712e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/db-backup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ jobs:
id: filename
run: |
CURRENT_DATE=$(date +"%Y-%m-%d_%H-%M-%S")
echo "filename=backup_${CURRENT_DATE}.gz" >> "$GITHUB_OUTPUT"
echo "filename=backup_${CURRENT_DATE}" >> "$GITHUB_OUTPUT"
- name: Setup Fly.io CLI
uses: superfly/flyctl-actions/setup-flyctl@master
- name: Fetch backup from Fly.io
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
run: |
flyctl -a ${{ env.FLY_APP_NAME }} ssh sftp get /rails/storage/production/backup.sqlite3.gzip ${{ steps.filename.outputs.filename }}
flyctl -a ${{ env.FLY_APP_NAME }} ssh sftp get /rails/storage/production/backup.sqlite3 ${{ steps.filename.outputs.filename }}
- name: Compress backup
run: gzip ${{ steps.filename.outputs.filename }}
- name: Upload compressed backup as GitHub artifact
uses: actions/upload-artifact@v4
with:
name: ${{ steps.filename.outputs.filename }}
path: ${{ steps.filename.outputs.filename }}
name: ${{ steps.filename.outputs.filename }}.gz
path: ${{ steps.filename.outputs.filename }}.gz
13 changes: 6 additions & 7 deletions app/lib/database_backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ def self.run!
end

def run
`sqlite3 #{Shellwords.escape db_path} "VACUUM INTO '#{Shellwords.escape backup_path}'"`
`gzip -f9 #{Shellwords.escape backup_path}`

"#{backup_path}.gzip"
FileUtils.rm_rf backup_path
conn.execute "VACUUM INTO #{conn.quote(backup_path.to_s)}"
backup_path
end

private

def db_path
Rails.root.join "storage", Rails.env, "data.sqlite3" # rubocop:disable Lint/Env
def conn
ActiveRecord::Base.connection
end

def backup_path
Rails.root.join "storage", Rails.env, "backup.sqlite3" # rubocop:disable Lint/Env
Rails.root.join("storage", Rails.env, "backup.sqlite3") # rubocop:disable Lint/Env
end
end

0 comments on commit cf712e8

Please sign in to comment.