|
| 1 | +--- |
| 2 | +title: Backup and Restore |
| 3 | +weight: 26 |
| 4 | +--- |
| 5 | + |
| 6 | +The way K3s is backed up and restored depends on which type of datastore is used. |
| 7 | + |
| 8 | +- [Backup and Restore with External Datastore](#backup-and-restore-with-external-datastore) |
| 9 | +- [Backup and Restore with Embedded etcd Datastore (Experimental)](#backup-and-restore-with-embedded-etcd-datastore-experimental) |
| 10 | + |
| 11 | +### Backup and Restore with External Datastore |
| 12 | + |
| 13 | +When an external datastore is used, backup and restore operations are handled outside of K3s. The database administrator will need to back up the external database, or restore it from a snapshot or dump. |
| 14 | + |
| 15 | +We recommend configuring the database to take recurring snapshots. |
| 16 | + |
| 17 | +For details on taking database snapshots and restoring your database from them, refer to the official database documentation: |
| 18 | + |
| 19 | +- [Official MySQL documentation](https://dev.mysql.com/doc/refman/8.0/en/replication-snapshot-method.html) |
| 20 | +- [Official PostgreSQL documentation](https://www.postgresql.org/docs/8.3/backup-dump.html) |
| 21 | +- [Official etcd documentation](https://etcd.io/docs/latest/op-guide/recovery/) |
| 22 | + |
| 23 | +### Backup and Restore with Embedded etcd Datastore (Experimental) |
| 24 | + |
| 25 | +_Available as of v1.19.1+k3s1_ |
| 26 | + |
| 27 | +In this section, you'll learn how to create backups of the K3s cluster data and to restore the cluster from backup. |
| 28 | + |
| 29 | +>**Note on Single-Server with embedded SQLite:** Currently, backups of SQLite are not supported. Instead, make a copy of `/var/lib/rancher/k3s/server` and then delete K3s. |
| 30 | +
|
| 31 | +#### Creating Snapshots |
| 32 | + |
| 33 | +Snapshots are enabled by default. |
| 34 | + |
| 35 | +The snapshot directory defaults to `${data-dir}/server/db/snapshots`. The data-dir value defaults to `/var/lib/rancher/k3s` and can be changed by setting the `--data-dir` flag. |
| 36 | + |
| 37 | +To configure the snapshot interval or the number of retained snapshots, refer to the [options.](#options) |
| 38 | + |
| 39 | +#### Restoring a Cluster from a Snapshot |
| 40 | + |
| 41 | +When K3s is restored from backup, the old data directory will be moved to `${data-dir}/server/db/etcd-old/`. Then K3s will attempt to restore the snapshot by creating a new data directory, then starting etcd with a new K3s cluster with one etcd member. |
| 42 | + |
| 43 | +To restore the cluster from backup, run K3s with the `--cluster-reset` option, with the `--cluster-reset-restore-path` also given: |
| 44 | + |
| 45 | +``` |
| 46 | +./k3s server \ |
| 47 | + --cluster-reset \ |
| 48 | + --cluster-reset-restore-path=<PATH-TO-SNAPSHOT> |
| 49 | +``` |
| 50 | + |
| 51 | +**Result:** A message in the logs says that K3s can be restarted without the flags. Start k3s again and should run successfully and be restored from the specified snapshot. |
| 52 | + |
| 53 | +#### Options |
| 54 | + |
| 55 | +These options can be passed in with the command line, or in the [configuration file,](/installation/install-options/#configuration-file ) which may be easier to use. |
| 56 | + |
| 57 | +| Options | Description | |
| 58 | +| ----------- | --------------- | |
| 59 | +| `--etcd-disable-snapshots` | Disable automatic etcd snapshots | |
| 60 | +| `--etcd-snapshot-schedule-cron` value | Snapshot interval time in cron spec. eg. every 5 hours `0 */5 * * *`(default: `0 */12 * * *`) | |
| 61 | +| `--etcd-snapshot-retention` value | Number of snapshots to retain (default: 5) | |
| 62 | +| `--etcd-snapshot-dir` value | Directory to save db snapshots. (Default location: `${data-dir}/db/snapshots`) | |
| 63 | +| `--cluster-reset` | Forget all peers and become sole member of a new cluster. This can also be set with the environment variable `[$K3S_CLUSTER_RESET]`. |
| 64 | +| `--cluster-reset-restore-path` value | Path to snapshot file to be restored |
| 65 | + |
| 66 | +#### S3 Compatible API Support |
| 67 | + |
| 68 | +K3s supports writing etcd snapshots to and restoring etcd snapshots from systems with S3-compatible APIs. S3 support is available for both on-demand and scheduled snapshots. |
| 69 | + |
| 70 | +The arguments below have been added to the `server` subcommand. These flags exist for the `etcd-snapshot` subcommand as well however the `--etcd-s3` portion is removed to avoid redundancy. |
| 71 | + |
| 72 | +| Options | Description | |
| 73 | +| ----------- | --------------- | |
| 74 | +| `--etcd-s3` | Enable backup to S3 | |
| 75 | +| `--etcd-s3-endpoint` | S3 endpoint url | |
| 76 | +| `--etcd-s3-endpoint-ca` | S3 custom CA cert to connect to S3 endpoint | |
| 77 | +| `--etcd-s3-skip-ssl-verify` | Disables S3 SSL certificate validation | |
| 78 | +| `--etcd-s3-access-key` | S3 access key | |
| 79 | +| `--etcd-s3-secret-key` | S3 secret key" | |
| 80 | +| `--etcd-s3-bucket` | S3 bucket name | |
| 81 | +| `--etcd-s3-region` | S3 region / bucket location (optional). defaults to us-east-1 | |
| 82 | +| `--etcd-s3-folder` | S3 folder | |
| 83 | + |
| 84 | +To perform an on-demand etcd snapshot and save it to S3: |
| 85 | + |
| 86 | +``` |
| 87 | +k3s etcd-snapshot \ |
| 88 | + --s3 \ |
| 89 | + --s3-bucket=<S3-BUCKET-NAME> \ |
| 90 | + --s3-access-key=<S3-ACCESS-KEY> \ |
| 91 | + --s3-secret-key=<S3-SECRET-KEY> |
| 92 | +``` |
| 93 | + |
| 94 | +To perform an on-demand etcd snapshot restore from S3, first make sure that K3s isn't running. Then run the following commands: |
| 95 | + |
| 96 | +``` |
| 97 | +k3s server \ |
| 98 | + --cluster-init \ |
| 99 | + --cluster-reset \ |
| 100 | + --etcd-s3 \ |
| 101 | + --cluster-reset-restore-path=<SNAPSHOT-NAME> \ |
| 102 | + --etcd-s3-bucket=<S3-BUCKET-NAME> \ |
| 103 | + --etcd-s3-access-key=<S3-ACCESS-KEY> \ |
| 104 | + --etcd-s3-secret-key=<S3-SECRET-KEY> |
| 105 | +``` |
| 106 | + |
| 107 | +#### Etcd Snapshot and Restore Subcommands |
| 108 | + |
| 109 | +k3s supports a set of subcommands for working with your etcd snapshots. |
| 110 | + |
| 111 | +| Subcommand | Description | |
| 112 | +| ----------- | --------------- | |
| 113 | +| delete | Delete given snapshot(s) | |
| 114 | +| ls, list, l | List snapshots | |
| 115 | +| prune | Remove snapshots that exceed the configured retention count | |
| 116 | +| save | Trigger an immediate etcd snapshot | |
| 117 | + |
| 118 | +*note* The `save` subcommand is the same as `k3s etcd-snapshot`. The latter will eventually be deprecated in favor of the former. |
| 119 | + |
| 120 | +These commands will perform as expected whether the etcd snapshots are stored locally or in an S3 compatible object store. |
| 121 | + |
| 122 | +For additional information on the etcd snapshot subcommands, run `k3s etcd-snapshot`. |
| 123 | + |
| 124 | +Delete a snapshot from S3. |
| 125 | + |
| 126 | +``` |
| 127 | +k3s etcd-snapshot delete \ |
| 128 | + --s3 \ |
| 129 | + --s3-bucket=<S3-BUCKET-NAME> \ |
| 130 | + --s3-access-key=<S3-ACCESS-KEY> \ |
| 131 | + --s3-secret-key=<S3-SECRET-KEY> \ |
| 132 | + <SNAPSHOT-NAME> |
| 133 | +``` |
| 134 | + |
| 135 | +Prune local snapshots with the default retention policy (5). The `prune` subcommand takes an additional flag `--snapshot-retention` that allows for overriding the default retention policy. |
| 136 | + |
| 137 | +``` |
| 138 | +k3s etcd-snapshot prune |
| 139 | +``` |
| 140 | + |
| 141 | +``` |
| 142 | +k3s etcd-snapshot prune --snapshot-retention 10 |
| 143 | +``` |
0 commit comments