Skip to content

Instantly share code, notes, and snippets.

@wesort
Last active December 8, 2022 15:32
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 wesort/c2711b76956c9a5b5c32d1ec7ed3f036 to your computer and use it in GitHub Desktop.
Save wesort/c2711b76956c9a5b5c32d1ec7ed3f036 to your computer and use it in GitHub Desktop.

s3cmd

A command line tool for syncing with DigitalOcean Spaces or Amazon S3

I'm using this in the context of .gitignore'ing Assets from a Statamic site.

DigitalOcean Spaces with s3cmd

Install & configure

s3cmd setup

  • Install s3cmd to interact with Spaces
  • from root: sudo apt install s3cmd
  • from root: s3cmd --configure (this will initate a config wizard)
  • Accesss Key and Secret Key:
    • Digitalocean > API > Spaces access keys
    • Name token with server name (ie: example-prod-1)
    • Access Key: XXX...XXXX
    • Secret Key: XXX...XXXX
  • Default Region: ams3
  • S3 Endpoint: ams3.digitaloceanspaces.com
  • DNS-style bucket+hostname:port template for accessing a bucket: example-assets-01.ams3.digitaloceanspaces.com
  • Encryption password: (something-random-_-save-it-somewhere)
  • Path to GPG program: (select default) /usr/bin/gpg
  • Use HTTPS protocol: (select default) True
  • HTTP Proxy server name:
  • HTTP Proxy server port: 0
  • Select Yes to test connection and save configuration

Scripts & hooks

Sync Up: s3cmd-spaces-sync-up.sh

#!/bin/bash
s3cmd sync -r --skip-existing --verbose --delete-removed public/assets/ s3://example-assets-01/assets/

Sync down: s3cmd-spaces-sync-down.sh

#!/bin/bash
s3cmd sync -r --skip-existing --verbose --delete-removed s3://example-assets-01/assets/ public/assets/

# If RemoteDisconnected error appears, add --no-check-md5 to command
# ie: https://github.com/s3tools/s3cmd/issues/960

Create backup: s3cmd-spaces-backup.sh

~ cron to run once a week?

#!/bin/bash
s3cmd put -r public/assets/ s3://example-assets-01/backup-`date +%F\-%a`/ --expiry-days=30

Git post-commit hook

  • Create .git/hooks/post-commit with contents of:
#!/bin/bash
git pull --rebase

git push

npm ci

npm run build

sh s3cmd-spaces-sync-down.sh

sh s3cmd-spaces-sync-up.sh
  • Set permissions on the hook: chmod +x .git/hooks/post-commit

Git post-merge hook

  • Create .git/hooks/post-merge with contents of:
#!/bin/bash
composer install

npm ci

npm run build

sh s3cmd-spaces-sync-down.sh
  • Set permissions on the hook: chmod +x .git/hooks/post-commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment