Skip to content

Commit f6be13c

Browse files
committed
enable retention
Signed-off-by: Avi Deitcher <[email protected]>
1 parent 613f323 commit f6be13c

File tree

3 files changed

+80
-24
lines changed

3 files changed

+80
-24
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Back up mysql databases to... anywhere!
44

55
## Overview
66

7-
mysql-backup is a simple way to do MySQL database backups and restores.
7+
mysql-backup is a simple way to do MySQL database backups and restores, as well as manage your backups.
88

99
It has the following features:
1010

@@ -14,6 +14,7 @@ It has the following features:
1414
* connect to any container running on the same system
1515
* select how often to run a dump
1616
* select when to start the first dump, whether time of day or relative to container start time
17+
* prune backups older than a specific time period or quantity
1718

1819
Please see [CONTRIBUTORS.md](./CONTRIBUTORS.md) for a list of contributors.
1920

docs/configuration.md

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Various sample configuration files are available in the [sample-configs](../samp
5858

5959
## Configuration Options
6060

61-
The following are the environment variables, CLI flags and configuration file options for a backup or a restore.
61+
The following are the environment variables, CLI flags and configuration file options for: backup(B), restore (R), prune (P).
6262

6363
| Purpose | Backup / Restore | CLI Flag | Env Var | Config Key | Default |
6464
| --- | --- | --- | --- | --- | --- |
@@ -70,34 +70,26 @@ The following are the environment variables, CLI flags and configuration file op
7070
| names of databases to exclude from the dump | B | `exclude` | `DB_NAMES_EXCLUDE` | `database.exclude` | |
7171
| do not include `USE <database>;` statement in the dump | B | `no-database-name` | `NO_DATABASE_NAME` | `database.no-database-name` | `false` |
7272
| restore to a specific database | R | `restore --database` | `RESTORE_DATABASE` | `restore.database` | |
73-
| how often to do a dump, in minutes | B | `dump --frequency` | `DB_DUMP_FREQ` | `dump.schedule.frequency` | `1440` (in minutes), i.e. once per day |
74-
| what time to do the first dump | B | `dump --begin` | `DB_DUMP_BEGIN` | `dump.schedule.begin` | `0`, i.e. immediately |
75-
| cron schedule for dumps | B | `dump --cron` | `DB_DUMP_CRON` | `dump.schedule.cron` | |
76-
| run the backup a single time and exit | B | `dump --once` | `RUN_ONCE` | `dump.schedule.once` | `false` |
77-
| enable debug logging | BR | `debug` | `DEBUG` | `logging: debug` | `false` |
78-
| where to put the dump file; see [backup](./backup.md) | B | `dump --target` | `DB_DUMP_TARGET` | `dump.targets` | |
73+
| how often to do a dump or prune, in minutes | BP | `dump --frequency` | `DB_DUMP_FREQ` | `dump.schedule.frequency` | `1440` (in minutes), i.e. once per day |
74+
| what time to do the first dump or prune | BP | `dump --begin` | `DB_DUMP_BEGIN` | `dump.schedule.begin` | `0`, i.e. immediately |
75+
| cron schedule for dumps or prunes | BP | `dump --cron` | `DB_DUMP_CRON` | `dump.schedule.cron` | |
76+
| run the backup or prune a single time and exit | BP | `dump --once` | `RUN_ONCE` | `dump.schedule.once` | `false` |
77+
| enable debug logging | BRP | `debug` | `DEBUG` | `logging: debug` | `false` |
78+
| where to put the dump file; see [backup](./backup.md) | BP | `dump --target` | `DB_DUMP_TARGET` | `dump.targets` | |
7979
| where the restore file exists; see [restore](./restore.md) | R | `restore --target` | `DB_RESTORE_TARGET` | `restore.target` | |
80-
| replace any `:` in the dump filename with `-` | B | `dump --safechars` | `DB_DUMP_SAFECHARS` | `database.safechars` | `false` |
81-
| AWS access key ID, used only if a target does not have one | BR | `aws-access-key-id` | `AWS_ACCESS_KEY_ID` | `dump.targets[s3-target].credentials.access-key-id` | |
82-
| AWS secret access key, used only if a target does not have one | BR | `aws-secret-access-key` | `AWS_SECRET_ACCESS_KEY` | `dump.targets[s3-target].credentials.secret-access-key` | |
83-
| AWS default region, used only if a target does not have one | BR | `aws-region` | `AWS_REGION` | `dump.targets[s3-target].region` | |
80+
| replace any `:` in the dump filename with `-` | BP | `dump --safechars` | `DB_DUMP_SAFECHARS` | `database.safechars` | `false` |
81+
| AWS access key ID, used only if a target does not have one | BRP | `aws-access-key-id` | `AWS_ACCESS_KEY_ID` | `dump.targets[s3-target].credentials.access-key-id` | |
82+
| AWS secret access key, used only if a target does not have one | BRP | `aws-secret-access-key` | `AWS_SECRET_ACCESS_KEY` | `dump.targets[s3-target].credentials.secret-access-key` | |
83+
| AWS default region, used only if a target does not have one | BRP | `aws-region` | `AWS_REGION` | `dump.targets[s3-target].region` | |
8484
| alternative endpoint URL for S3-interoperable systems, used only if a target does not have one | BR | `aws-endpoint-url` | `AWS_ENDPOINT_URL` | `dump.targets[s3-target].endpoint` | |
85-
| SMB username, used only if a target does not have one | BR | `smb-user` | `SMB_USER` | `dump.targets[smb-target].credentials.username` | |
86-
| SMB password, used only if a target does not have one | BR | `smb-pass` | `SMB_PASS` | `dump.targets[smb-target].credentials.password` | |
87-
| compression to use, one of: `bzip2`, `gzip` | B | `compression` | `COMPRESSION` | `dump.compression` | `gzip` |
85+
| SMB username, used only if a target does not have one | BRP | `smb-user` | `SMB_USER` | `dump.targets[smb-target].credentials.username` | |
86+
| SMB password, used only if a target does not have one | BRP | `smb-pass` | `SMB_PASS` | `dump.targets[smb-target].credentials.password` | |
87+
| compression to use, one of: `bzip2`, `gzip` | BP | `compression` | `COMPRESSION` | `dump.compression` | `gzip` |
8888
| when in container, run the dump or restore with `nice`/`ionice` | BR | `` | `NICE` | `` | `false` |
8989
| tmp directory to be used during backup creation and other operations | BR | `tmp` | `TMP_PATH` | `tmp` | system-defined |
9090
| filename to save the target backup file | B | `dump --filename-pattern` | `DB_DUMP_FILENAME_PATTERN` | `dump.filename-pattern` | |
9191
| directory with scripts to execute before backup | B | `dump --pre-backup-scripts` | `DB_DUMP_PRE_BACKUP_SCRIPTS` | `dump.scripts.pre-backup` | in container, `/scripts.d/pre-backup/` |
9292
| directory with scripts to execute after backup | B | `dump --post-backup-scripts` | `DB_DUMP_POST_BACKUP_SCRIPTS` | `dump.scripts.post-backup` | in container, `/scripts.d/post-backup/` |
9393
| directory with scripts to execute before restore | R | `restore --pre-restore-scripts` | `DB_DUMP_PRE_RESTORE_SCRIPTS` | `dump.pre-restore-scripts` | in container, `/scripts.d/pre-restore/` |
9494
| directory with scripts to execute after restore | R | `restore --post-restore-scripts` | `DB_DUMP_POST_RESTORE_SCRIPTS` | `dump.post-restore-scripts` | in container, `/scripts.d/post-restore/` |
95-
96-
97-
## Unsupported Options
98-
99-
Unsupported options from the old version of `mysql-backup`:
100-
101-
* `MYSQLDUMP_OPTS`: A string of options to pass to `mysqldump`, e.g. `MYSQLDUMP_OPTS="--opt abc --param def --max_allowed_packet=123455678"` will run `mysqldump --opt abc --param def --max_allowed_packet=123455678`. These are replaced by individual options.
102-
* `AWS_CLI_OPTS`: Additional arguments to be passed to the `aws` part of the `aws s3 cp` command, click [here](https://docs.aws.amazon.com/cli/latest/reference/#options) for a list. These are replaced by target-specific options.
103-
* `AWS_CLI_S3_CP_OPTS`: Additional arguments to be passed to the `s3 cp` part of the `aws s3 cp` command, click [here](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html#options) for a list. If you are using AWS KMS, `sse`, `sse-kms-key-id`, etc., may be of interest. These are replaced by target-specific options
95+
| retention policy for backups | BP | `dump --retention` | `RETENTION` | `prune.retention` | Infinite |

docs/prune.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Pruning
2+
3+
Pruning is the process of removing backups that no longer are needed.
4+
5+
mysql-backup does **not** do this by default; it is up to you to enable this feature, if you want it.
6+
7+
## Launching Pruning
8+
9+
Pruning happens only in the following scenarios:
10+
11+
* During pruning runs
12+
* During backup runs
13+
14+
It does not occur during restore runs.
15+
16+
### Pruning Runs
17+
18+
You can start `mysql-backup` with the command `prune` to run a pruning operation. It will prune any backups that are no longer needed.
19+
20+
Like backups, it can run once and then exit, or it can run on a schedule.
21+
22+
It uses the same configuration options for scheduling as backups, see the [scheduling](./scheduling.md) documentation for more information,
23+
specifically the section about [Scheduling Options](./scheduling.md#scheduling-options).
24+
25+
### Backup Runs
26+
27+
When running `mysql-backup` in backup mode, it _optionally_ can also prune older backups before each backup run.
28+
When enabled, it will prune any backups that fit the pruning criteria.
29+
30+
## Pruning Criteria
31+
32+
Pruning can be on the basis of the _age_ of a specific backup, or the _number_ of backups. Both are set by the configuration setting:
33+
34+
* Environment variable: `RETENTION=<value>`
35+
* CLI flag: `dump --retention=<value>` or `prune --retention=<value>`
36+
* Config file:
37+
```yaml
38+
prune:
39+
retention: <value>
40+
```
41+
42+
The value of retention always is an integer followed by a letter. The letter can one of:
43+
44+
* `h` - hours, e.g. `2h`
45+
* `d` - days, e.g. `3d`
46+
* `w` - weeks, e.g. `4w`
47+
* `m` - months, e.g. `3m`
48+
* `y` - years, e.g. `5y`
49+
* `c` - count, how many backup to keep, e.g. `10c`; this could have been simply `10`, but was kept as `c` to avoid accidental confusion with the other options.
50+
51+
Most of these are interchangeable, e.g. `3d` is the same as `72h`, and `4w` is the same as `28d` is the same as `672h`.
52+
53+
When calculating whether or not to prune, `mysql-backup` __always__ converts the amount to hours, and then errs on the side of caution.
54+
For example, if provided `7d`, it will convert that to `168h`, and then prune any backups older than 168 full hours. If it is 167 hours and 59 minutes old, it
55+
will not be pruned.
56+
57+
## Determining backup age
58+
59+
Pruning depends on the name of the backup file, rather than the timestamp on the target filesystem, as the latter can be unreliable.
60+
This means that the filename must be of a known pattern.
61+
62+
As of this writing, pruning only work for backup files whose filename uses the default naming scheme, as described in
63+
["Dump File" in backup documentation](./backup.md#dump-file). We hope to support custom filenames in the future.

0 commit comments

Comments
 (0)