# How do I back up the database for Rancher?

**URL:** <https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144>\
**Category:** Rancher 1.x\
**Created:** [July 23, 2015, 11:31am UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144 "2015-07-23T11:31:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![deitch](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/deitch/32/40_2.png) [@deitch](https://forums.suse.com/u/deitch)\
**Post date:** [July 23, 2015, 11:31am UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/1 "2015-07-23T11:31:49Z")

</div>

The Rancher docs explain how to use an external database. But how do I just back up the one in the container? Is there some way I can use -v to mount the data externally? Or some other method?

---

<div class="post-metadata">

**Author:** ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)\
**Post date:** [July 23, 2015, 3:48pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/2 "2015-07-23T15:48:16Z")

</div>

Yes, it’s in the standard location so you can bind-mount `/var/lib/mysql`.

---

<div class="post-metadata">

**Author:** ![deitch](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/deitch/32/40_2.png) [@deitch](https://forums.suse.com/u/deitch)\
**Post date:** [July 23, 2015, 4:07pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/3 "2015-07-23T16:07:49Z")

</div>

Brilliant. So if I start the rancher server `-v /some/place/I/will/back/up:/var/lib/mysql` it will work fine?

---

<div class="post-metadata">

**Author:** ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)\
**Post date:** [July 23, 2015, 9:29pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/4 "2015-07-23T21:29:35Z")

</div>

Yes, and using `-v` to make it a volume is a good way to ensure you don’t accidentally lose it. As far as backing it up, I’m not sure what guarantees MySQL makes about using a copy of their data folder made while it was running. So it would probably be safer to do something like

`docker exec -it rancher-server-container-id mysqldump -A`

or

`docker run --link rancher-server-container-id:db mysql mysqldump -A -h 'db' -u cattle -pcattle`

To get a proper atomic dump of the database content.

---

<div class="post-metadata">

**Author:** ![deitch](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/deitch/32/40_2.png) [@deitch](https://forums.suse.com/u/deitch)\
**Post date:** [July 24, 2015, 4:42am UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/5 "2015-07-24T04:42:50Z")

</div>

This _definitely_ should be part of the docs. Having the ability to attach to the `/var/lib/sql` from outside as a volume - i.e. knowing that is where it is and that there is a `VOLUME` there (I just read the Dockerfile, so now I see) - is valuable to anyone managing the installation.

But equally knowing how to run “proper” backups, i.e. knowing that `mysqldump` would work here is important.

Should also have restore directions.

Thanks.

---

<div class="post-metadata">

**Author:** ![shakisha](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@shakisha](https://forums.suse.com/u/shakisha)\
**Post date:** [August 19, 2015, 5:31pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/6 "2015-08-19T17:31:56Z")

</div>

Was having the same problem as you, just found now the documentation, you can look here ----\>  
[http://docs.rancher.com/rancher/upgrading/](http://docs.rancher.com/rancher/upgrading/)

---

<div class="post-metadata">

**Author:** ![deitch](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/deitch/32/40_2.png) [@deitch](https://forums.suse.com/u/deitch)\
**Post date:** [August 20, 2015, 12:48pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/7 "2015-08-20T12:48:57Z")

</div>

Not quite. We finally got around to trying it, and the server refuses to start. It hangs on

```
Running mysql_install_db...
chown: changing ownership of '/var/lib/mysql': Permission denied
Cannot change ownership of the database directories to the 'mysql'
user. Check that you have the necessary permissions and try again.
```

---

<div class="post-metadata">

**Author:** ![deitch](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/deitch/32/40_2.png) [@deitch](https://forums.suse.com/u/deitch)\
**Post date:** [August 20, 2015, 12:57pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/8 "2015-08-20T12:57:56Z")

</div>

Ah well, selinux again. 🙂

---

<div class="post-metadata">

**Author:** ![shakisha](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@shakisha](https://forums.suse.com/u/shakisha)\
**Post date:** [August 21, 2015, 1:14am UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/9 "2015-08-21T01:14:35Z")

</div>

yes, this selinux is a devil 😄

---

<div class="post-metadata">

**Author:** ![deitch](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/deitch/32/40_2.png) [@deitch](https://forums.suse.com/u/deitch)\
**Post date:** [September 21, 2015, 2:13pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/10 "2015-09-21T14:13:11Z")

</div>

So I just built a simple version of what @vincent suggested. An image that can be run and told to do backups.

`docker run --link rancher-server-container-id:db rancher-backup` (OK, it isn’t called rancher-backup because I haven’t pushed it out yet, and it would have a real namespace)

It takes 4 env var options:

- `DB_DUMP_FREQ`: how often to run the dump (minutes), default to 1440 (1/day)
- `DB_DUMP_BEGIN`: when to start that frequency, in time of day, e.g. 1330, default 2330
- `DB_DUMP_TARGET`: where to dump the backups, default to `/backup`
- `DB_DUMP_DEBUG`: to enable `set -x` on the bash script that does the backups for debugging

So you could map something into `/backup` and have it run the backups.

Each backup is saved as a file in `DB_DUMP_TARGET` directory as a gzipped file with the name `rancher_backup_YYYYMMDDHHMMSS.gz` (as in YYYY=year, etc.)

I am working on getting it to accept a `DB_DUMP_TARGET` of an smb URL, then http and maybe nfs.

It also supports `DB_RESTORE_TARGET` which, if given a file from a previous backup, will restore the database. Works really nicely as a one-time `docker run` command.

Any interest? Should I ask my client for permission to open-source it?

---

<div class="post-metadata">

**Author:** ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)\
**Post date:** [September 21, 2015, 6:29pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/11 "2015-09-21T18:29:33Z")

</div>

Is there anything Rancher-specific about it? Sounds like it could be useful as a generic mysql backup method. If open source I think we’d be happy to link to it in the docs.

---

<div class="post-metadata">

**Author:** ![kiboro](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@kiboro](https://forums.suse.com/u/kiboro)\
**Post date:** [September 24, 2015, 10:29am UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/12 "2015-09-24T10:29:56Z")

</div>

> docker exec -it rancher-server-container-id mysqldump -A

> or

> docker run --link rancher-server-container-id:db mysql mysqldump -A -h ‘db’ -u cattle -pcattle

> To get a proper atomic dump of the database content.

Hi Vincent, this technique works fine for backups, but stuck on restore (glad I’m testing now).

Tried this:

> /usr/bin/docker exec -it cad399e67ffa mysql -pxxxxx keycloak \< /home/kevin/keycloak-2015-09-24.sql  
> cannot enable tty mode on non tty input  
> Nope…  
> /usr/bin/docker exec -i cad399e67ffa mysql -pxxxxx keycloak \< /home/kevin/keycloak-2015-09-24.sql  
> Warning: Using a password on the command line interface can be insecure.  
> ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Warning: Using a password on the command line interface can be insecure.

> /’ at line 1  
> Nope again…

Probably easy if I knew how.

---

<div class="post-metadata">

**Author:** ![kiboro](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@kiboro](https://forums.suse.com/u/kiboro)\
**Post date:** [September 24, 2015, 10:31am UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/13 "2015-09-24T10:31:22Z")

</div>

Bah, forget about it. The dump leaves a warning line about passwords on the command line in the top of the sql dump. Delete that and it’s fine.

---

<div class="post-metadata">

**Author:** ![deitch](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/deitch/32/40_2.png) [@deitch](https://forums.suse.com/u/deitch)\
**Post date:** [September 24, 2015, 11:05am UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/14 "2015-09-24T11:05:50Z")

</div>

It probably could, now that I think about it. The only rancher-specific things are:

- link to rancher db container - but that is defined in `--link` line anyways, so not really rancher-specific
- username and password - but that also can be overridden if I make a minor tweak

I’ll go get signoff.

---

<div class="post-metadata">

**Author:** ![vincent](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/vincent/32/7156_2.png) [@vincent](https://forums.suse.com/u/vincent)\
**Post date:** [September 24, 2015, 1:51pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/15 "2015-09-24T13:51:36Z")

</div>

On my phone so I can’t test it, but `2>/dev/null` should hide the password warning, or even better `>mydump.sql` to write the dump to disk and leave errors to stderr.

---

<div class="post-metadata">

**Author:** ![deitch](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/deitch/32/40_2.png) [@deitch](https://forums.suse.com/u/deitch)\
**Post date:** [October 4, 2015, 6:49am UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/16 "2015-10-04T06:49:57Z")

</div>

OK, they just signed off. I will get this uploaded to github and the docker hub in the coming days.

---

<div class="post-metadata">

**Author:** ![deitch](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/deitch/32/40_2.png) [@deitch](https://forums.suse.com/u/deitch)\
**Post date:** [October 4, 2015, 12:37pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/17 "2015-10-04T12:37:19Z")

</div>

Heh, I got it today. See [https://github.com/deitch/mysql-backup](https://github.com/deitch/mysql-backup) and the image at [https://hub.docker.com/r/deitch/mysql-backup/](https://hub.docker.com/r/deitch/mysql-backup/)

Issues and PRs welcome at the github repo.

---

<div class="post-metadata">

**Author:** ![josedavireges](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@josedavireges](https://forums.suse.com/u/josedavireges)\
**Post date:** [September 4, 2017, 8:09pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/18 "2017-09-04T20:09:46Z")

</div>

The best way is `docker exec -it 711830bda328 mysqldump -A > dump.sql`

---

<div class="post-metadata">

**Author:** ![pwFoo](https://sea2.discourse-cdn.com/flex022/user_avatar/forums.suse.com/pwfoo/32/2884_2.png) [@pwFoo](https://forums.suse.com/u/pwFoo)\
**Post date:** [September 5, 2017, 5:46am UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/19 "2017-09-05T05:46:25Z")

</div>

Rancher backup ist in the catalog and helps to backup databases and stacks.

> **[disaster37/rancher-backup](https://github.com/disaster37/rancher-backup)**
>
> Contribute to rancher-backup development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![georgi](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@georgi](https://forums.suse.com/u/georgi)\
**Post date:** [September 6, 2017, 3:19pm UTC](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144/20 "2017-09-06T15:19:41Z")

</div>

When i try to restore a mysqldump i get  
`ERROR 2013 (HY000) at line 1418: Lost connection to MySQL server during query`  
any ideas?

[Next page](https://forums.suse.com/t/how-do-i-back-up-the-database-for-rancher/144.md?page=2)
