[SOLVED] How do I back up a GitLab Mattermost database?

I can’t figure out how to backup the database. Can someone please send me a tutorial or something?

Updated June 28, 2016, here’s the command:

sudo -u mattermost -i bash
/opt/gitlab/embedded/bin/pg_dump -U gitlab_mattermost -h /var/opt/gitlab/postgresql -p 5432 mattermost_production > mattermost_production_backup
2 Likes

Okay, I think I’ve made a bit of progress, but I’m still stuck. I found how to use the pg_dump command, but it’s giving me the following error:

mattermost@git:~$ /opt/gitlab/embedded/bin/pg_dump mattermost_production > mattermost_production.bak
pg_dump: [archiver (db)] connection to database "mattermost_production" failed: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

Okay, I think I’m on to something here. I’ve got this:

sudo /opt/gitlab/embedded/bin/pg_dump -U gitlab_mattermost -h /var/opt/gitlab/postgresql -p 5432 mattermost_production > mattermost_production_backup

But that returns

pg_dump: [archiver (db)] connection to database "mattermost_production" failed: FATAL: Peer authentication failed for user "gitlab_mattermost"

Hmm.

Okay, I’m stuck. Any help would be greatly appreciated.

Got it, here’s what I did:

sudo -u mattermost -i bash
/opt/gitlab/embedded/bin/pg_dump -U gitlab_mattermost -h /var/opt/gitlab/postgresql -p 5432 mattermost_production > mattermost_production_backup
1 Like

thanks for sharing your solution! it worked for me too.

I was trying to import the database with sudo -u mattermost /opt/gitlab/embedded/bin/psql -U gitlab_mattermost -h /var/opt/gitlab/postgresql -p 5432 mattermost_production < mattermost_production_backup, but there were many errors with relations already existing.

What’s a good way to restore the database?

ackerleytng, did you ever figure out how to import the database?

Figured out a nice semi-onliner for this. Once this runs, you still need to move it out of /tmp/ to wherever you want to store it.

sudo -H -u mattermost bash -c '/opt/gitlab/embedded/bin/pg_dump -U gitlab_mattermost -h /var/opt/gitlab/postgresql -p 5432 mattermost_production > /tmp/mattermost_production.sql'

If anyone’s using the old GitLab omnibus Kubernetes helm chart, here’s how:

kubectl exec `kubectl get pod -l "name=gitlab-gitlab" -o jsonpath='{.items[0].metadata.name}'` -- bash -c "PGPASSWORD=\$POSTGRES_PASSWORD /opt/gitlab/embedded/bin/pg_dump -U gitlab -h gitlab-gitlab-postgresql -p 5432 mattermost_production > /var/opt/gitlab/backups/mattermost_production_backup"

This will put it in the same directory as gitlab-rake gitlab:backup:create uses, so if you script it to run after that, you can just copy the whole /var/opt/gitlab/backups directory out to where you need it.