Yeah, here’s some notes.
Individual Objects
Backing up and restoring an entire cluster is simply etcdctl snapshot {save or restore} ./backup.etcd.data
.
Backup all of certain resources in a namespace.
$ kubectl -n my-namespace get deployments,statefulsets,svc -o yaml > k8s-objects.backup
Backup specific objects of different types in one go.
$ kubectl -n my-namespace get statefulset/mysql deployment/wordpress -o yaml > k8s-objects.backup
Then restoring them is just applying the file to the new cluster.
$ kubectl -n my-namespace apply -f k8s-objects.backup
Persistent Files
Copying files from a container can be done with kubectl cp
.
I copied file from a wordpress pod.
$ kubectl cp my-namespace/wordpress-5c799566c6-w9j6h:/var/www/html ./html
Then I pushed them to the new pod. Something to note, this became a few steps with the wordpress container, I think my files were landing in a subdirectory and I was just too lazy to figure out correct pathing.
$ kubectl cp ./html my-namespace/mainsite-745bf74b7f-f6gpv:/tmp/html
4 kubectl -n my-namespace exec -it mainsite-745bf74b7f-f6gpv -- bash -c 'rm -rf /var/www/html/* /var/www/html/.htaccess'
$ kubectl -n my-namespace exec -it mainsite-745bf74b7f-f6gpv -- bash -c 'cp -rfp /tmp/html/.htaccess /var/www/html/.htaccess'
$ kubectl -n my-namespace exec -it mainsite-745bf74b7f-f6gpv -- bash -c 'cp -rfp /tmp/html/* /var/www/html/'
MySQL
If the database is like MariaDB
or MySQL
, you can get a dump by executing a dump command with kubectl exec
and pipe the output to a local path.
This example is one I used to migrate MySQL data for Wordpress.
Backup was fairly straight forward. I had to use bash
and specify auth stuff from env
.
$ kubectl -n my-namespace exec wordpress-mariadb-95cf9bbd-75z9l -- bash -c 'mysqldump ${MYSQL_DATABASE} -u ${MYSQL_USER} -p"${MYSQL_PASSWORD}"' > backup.sql-$(date -u "+%Y%m%d-%H%M%S")
Restoration got janky, because I was using bash
to restore data piped from stdin
, I had to pull the input from /dev/stdin
, because it was getting lost somewhere.
$ cat backup.sql-20210514-190206 | kubectl -n my-namespace exec mainsite-mariadb-5894c8c4f5-gf872 -i -- bash -c 'mysql ${MYSQL_DATABASE} -u ${MYSQL_USER} -p"${MYSQL_PASSWORD}" < /dev/stdin'