项目作者: mittwald

项目描述 :
Easy, incremental and encrypted backup creation / restoration for different backends (file, mongoDB, mysql, postgres, etc.)
高级语言: Go
项目地址: git://github.com/mittwald/brudi.git
创建时间: 2020-03-06T06:43:42Z
项目社区:https://github.com/mittwald/brudi

开源协议:MIT License

下载


brudi

When it comes to backup-creation there are several solutions to use.
In general everybody’s doing some sort of dump or tar and backing up the results incremental with restic or similar programs.

This is why brudi was born. brudi supports several backup-methods and is configurable by a simple yaml file.
The advantage of brudi is, that you can create a backup of a source of your choice and save it with restic afterwards in one step.
Under the hood, brudi uses the given binaries like mysqldump, mongodump, pg_dump, tar or restic.

Using brudi will save you from finding yourself writing bash-scripts to create your backups.

Besides creating backups, brudi can also be used to restore your data from backup in an emergency.

Table of contents

Usage

CLI

In order to use the brudi-binary on your local machine or a remote server of your choice, ensure you have the required tools installed.

  • mongodump (required when running brudi mongodump)
  • mysqldump (required when running brudi mysqldump)
  • tar (required when running brudi tar)
  • redis-cli (required when running brudi redisdump)
  • restic (required when running brudi --restic)
  1. $ brudi --help
  2. Easy, incremental and encrypted backup creation for different backends (file, mongoDB, mysql, etc.)
  3. After creating your desired tar- or dump-file, brudi backs up the result with restic - if you want to
  4. Usage:
  5. brudi [command]
  6. Available Commands:
  7. help Help about any command
  8. mongodump Creates a mongodump of your desired server
  9. mongorestore Restores a server from a mongodump
  10. mysqldump Creates a mysqldump of your desired server
  11. mysqlrestore Restores a database from an sqldump
  12. pgdump Creates a pg_dump of your desired postgresql-server
  13. pgrestore Restores a database from a pgdump using pg_restore
  14. psql Restores a database from a plain-text pgdump using psql
  15. redisdump Creates an rdb dump of your desired server
  16. tar Creates a tar archive of your desired
  17. tarrestore Restores files from a tar archive
  18. version Print the version number of brudi
  19. Flags:
  20. --cleanup cleanup backup files afterwards
  21. -c, --config string config file (default is ${HOME}/.brudi.yaml)
  22. -h, --help help for brudi
  23. --restic backup result with 'restic backup'
  24. --restic-forget executes 'restic forget' after backing up things with restic
  25. --version version for brudi
  26. Use "brudi [command] --help" for more information about a command.

Docker

In case you don’t want to install additional tools, you can also use brudi inside docker:

docker run --rm -v ${HOME}/.brudi.yml:/home/brudi/.brudi.yml quay.io/mittwald/brudi mongodump --restic --cleanup

The docker-image comes with all required binaries.

Configuration

As already mentioned, brudi is configured via .yaml. The default path for this file is ${HOME}/.brudi.yaml, but it’s adjustable via -c or --config.
The config file itself can include environment-variables via go-template:

  1. restic:
  2. global:
  3. flags:
  4. repo: "{{ .Env.RESTIC_REPOSITORY }}"

Since the configuration provided by the .yaml-file is mapped to the corresponding CLI-flags, you can adjust literally every parameter of your source backup.
Therefore you can simply refer to the official documentation for explanations on the available flags:

Every source has a an additionalArgs-key which’s value is an array of strings. The value of this key is appended to the command, generated by brudi.
Even though brudi should support all cli-flags to be configured via the .yaml-file, there may be flags which are not.
In this case, use the additionalArgs-key.

It is also possible to provide more than one configuration file, for example -c mongodump.yaml -c restic.yaml. These configs get merged at runtime.
If available, the default config will always be laoded first and then overwritten with any values from user-specified files.
In case the same config file has been provided more than once, only the first instance will be taken into account.

Sources

Tar
  1. tar:
  2. options:
  3. flags:
  4. create: true
  5. gzip: true
  6. file: /tmp/test.tar.gz
  7. additionalArgs: []
  8. paths:
  9. - /tmp/testfile
  10. hostName: autoGeneratedIfEmpty

Running: brudi tar -c ${HOME}/.brudi.yml --cleanup

Becomes the following command:
tar -c -z -f /tmp/test.tar.gz /tmp/testfile

All available flags to be set in the .yaml-configuration can be found here.

MySQLDump
  1. mysqldump:
  2. options:
  3. flags:
  4. host: 127.0.0.1
  5. port: 3306
  6. password: mysqlroot
  7. user: root
  8. opt: true
  9. allDatabases: true
  10. resultFile: /tmp/test.sqldump
  11. additionalArgs: []

Running: brudi mysqldump -c ${HOME}/.brudi.yml --cleanup

Becomes the following command:
mysqldump --all-databases --host=127.0.0.1 --opt --password=mysqlroot --port=3306 --result-file=/tmp/test.sqldump --user=root

All available flags to be set in the .yaml-configuration can be found here.

MongoDump
  1. mongodump:
  2. options:
  3. flags:
  4. host: 127.0.0.1
  5. port: 27017
  6. username: root
  7. password: mongodbroot
  8. gzip: true
  9. archive: /tmp/dump.tar.gz
  10. additionalArgs: []

Running: brudi mongodump -c ${HOME}/.brudi.yml --cleanup

Becomes the following command:
mongodump --host=127.0.0.1 --port=27017 --username=root --password=mongodbroot --gzip --archive=/tmp/dump.tar.gz

All available flags to be set in the .yaml-configuration can be found here.

PgDump
  1. pgdump:
  2. options:
  3. flags:
  4. host: 127.0.0.1
  5. port: 5432
  6. password: postgresroot
  7. username: postgresuser
  8. dbName: postgres
  9. file: /tmp/postgres.dump
  10. additionalArgs: []

Running: brudi pgdump -c ${HOME}/.brudi.yml --cleanup

Becomes the following command:
pg_dump --file=/tmp/postgres.dump --dbname=postgres --host=127.0.0.1 --port=5432 --username=postgresuser

All available flags to be set in the .yaml-configuration can be found here.

Limitations

Unfortunately PostgreSQL is very strict when it comes to version-compatibility.
Therefore your pg_dump-binary requires the exact same version your PostgreSQL-server is running.

The Docker-image of brudi always has the latest version available for the corresponding alpine-version installed.

Redis
  1. redisdump:
  2. options:
  3. flags:
  4. host: 127.0.0.1
  5. password: redisdb
  6. rdb: /tmp/redisdump.rdb
  7. additionalArgs: []

Running: brudi redisdump -c ${HOME}/.brudi.yml

Becomes the following command:
redis-cli -h 127.0.0.1 -a redisdb --rdb /tmp/redisdump.rdb bgsave

As redis-cli is not a dedicated backup tool but a client for redis, only a limited number of flags are available by default,
as you can see here.

Restic

In case you’re running your backup with the --restic-flag, you need to provide a valid configuration for restic.
You can either configure restic via brudis .yaml-configuration, or via the environment variables used by restic.

If you’re already using restic in your environment, you should have everything set up perfectly to use brudi with --restic.

Forget

It’s also possible to run restic forget-cmd after executing restic backup with brudi by using --restic-forget.
The forget-policy is defined in the configuration .yaml for brudi.

Example .yaml-configuration:

  1. restic:
  2. global:
  3. flags:
  4. # you can provide the repository also via RESTIC_REPOSITORY
  5. repo: "s3:s3.eu-central-1.amazonaws.com/your.s3.bucket/myResticRepo"
  6. backup:
  7. flags:
  8. # in case there is no hostname given, the hostname from source backup is used
  9. hostname: "MyHost"
  10. # these paths are backuped additionally to your given source backup
  11. paths: []
  12. forget:
  13. flags:
  14. keepLast: 48
  15. keepHourly: 24
  16. keepDaily: 7
  17. keepWeekly: 2
  18. keepMonthly: 6
  19. keepYearly: 2
  20. ids: []

Sensitive data: Environment variables

In case you don’t want to provide data directly in the .yaml-file, e.g. sensitive data like passwords, you can use environment-variables.
Each key of the configuration is overwritable via environment-variables. Your variable must specify the whole path to a key, seperated by _.
For example, given this .yaml:

  1. mongodump:
  2. options:
  3. flags:
  4. username: "" # we will override this by env
  5. password: "" # we will override this by env
  6. host: 127.0.0.1
  7. port: 27017
  8. gzip: true
  9. archive: /tmp/dump.tar.gz

Set your env’s:

  1. export MONGODUMP_OPTIONS_FLAGS_USERNAME="root"
  2. export MONGODUMP_OPTIONS_FLAGS_PASSWORD="mongodbroot"

As soon as a variable for a key exists in your environment, the value of this environment-variable is used in favour of your .yaml-config.

Gzip support for binaries without native gzip support

The tools mysqldump, pg_dump and redis-cli don’t natively support gzip. However, if the desired path for the backup file is suffixed with .gz,
brudi will automatically gzip the backup after creation and delete the uncompressed backup file. For restoration, the file will be automatically
uncompressed. Example for mysql:

  1. mysqldump:
  2. options:
  3. flags:
  4. host: 127.0.0.1
  5. port: 3306
  6. password: mysqlroot
  7. user: root
  8. opt: true
  9. allDatabases: true
  10. resultFile: /tmp/test.sqldump.gz
  11. additionalArgs: []
  1. mysqlrestore:
  2. options:
  3. flags:
  4. host: 127.0.0.1
  5. port: 3306
  6. password: mysqlroot
  7. user: root
  8. Database: test
  9. additionalArgs: []
  10. sourceFile: /tmp/test.sqldump.gz

Restoring from backup

TarRestore
  1. tarrestore:
  2. options:
  3. flags:
  4. extract: true
  5. gzip: true
  6. file: /tmp/test.tar.gz
  7. target: "/"
  8. additionalArgs: []
  9. hostName: autoGeneratedIfEmpty

Running: brudi tarrestore -c ${HOME}/.brudi.yml

Becomes the following command:
tar -x -z -f /tmp/test.tar.gz -C /

MongoRestore
  1. mongorestore:
  2. options:
  3. flags:
  4. host: 127.0.0.1
  5. port: 27017
  6. username: root
  7. password: mongodbroot
  8. gzip: true
  9. archive: /tmp/dump.tar.gz
  10. additionalArgs: []

Running: brudi mongorestore -c ${HOME}/.brudi.yml

Becomes the following command:
mongorestore --host=127.0.0.1 --port=27017 --username=root --password=mongodbroot --gzip --archive=/tmp/dump.tar.gz

All available flags to be set in the .yaml-configuration can be found here.

MySQLRestore
  1. mysqlrestore:
  2. options:
  3. flags:
  4. host: 127.0.0.1
  5. port: 3306
  6. password: mysqlroot
  7. user: root
  8. Database: test
  9. additionalArgs: []
  10. sourceFile: /tmp/test.sqldump

Running: brudi mysqlrestore -c ${HOME}/.brudi.yml

Becomes the following command:
mysql --database=test --host=127.0.0.1 --password=mysqlroot --port=3306 --user=root < /tmp/test.sqldump

All available flags to be set in the .yaml-configuration can be found here.

PgRestore

Restoration for PostgreSQL databases is split into two commands, psql and pgrestore. Which one to use depends on the format of the dump created with pg_dump:

psql can be used to restore plain-text dumps, which is the default format.

pgrestore can be used if the format option of pg_dump was set to tar, directory or custom.

Restore using pg_restore
  1. pgrestore:
  2. options:
  3. flags:
  4. host: 127.0.0.1
  5. port: 5432
  6. username: postgresuser
  7. password: postgresroot
  8. dbname: postgres
  9. additionalArgs: []
  10. sourcefile: /tmp/postgres.dump

Running: brudi pgrestore -c ${HOME}/.brudi.yml

Becomes the following command:
pg_restore --host=127.0.0.1 --port=5432 --username=postgresuser --db-name=postgres /tmp/postgress.dump

This command has to be used if the format option was set to tar, directory or custom in pg_dump.

All available flags to be set in the .yaml-configuration can be found here.

Restore using psql
  1. psql:
  2. options:
  3. flags:
  4. host: 127.0.0.1
  5. port: 5432
  6. user: postgresuser
  7. password: postgresroot
  8. dbname: postgres
  9. additionalArgs: []
  10. sourcefile: /tmp/postgres.dump

Running: brudi pgrestore -c ${HOME}/.brudi.yml

Becomes the following command:
psql --host=127.0.0.1 --port=5432 --user=postgresuser --db-name=postgres < /tmp/postgress.dump

This command has to be used if the format option was set to plain in pg_dump, which is the default.

All available flags to be set in the .yaml-configuration can be found here.

Restoring using restic

Backups can be pulled from a restic repository and applied to your server by using the --restic flag in your brudi command.
Example configuration for mongorestore:

  1. mongorestore:
  2. options:
  3. flags:
  4. host: 127.0.0.1
  5. port: 27017
  6. username: root
  7. password: mongodbroot
  8. gzip: true
  9. archive: /tmp/dump.tar.gz
  10. additionalArgs: []
  11. restic:
  12. global:
  13. flags:
  14. repo: "s3:s3.eu-central-1.amazonaws.com/your.s3.bucket/myResticRepo"
  15. restore:
  16. flags:
  17. target: "/"
  18. id: "latest"

This will pull the latest snapshot of /tmp/dump.tar.gz from the repository, which mongorestore then uses to restore the server.
It is also possible to specify concrete snapshot-ids instead of latest.

Featurestate

Source backup methods

  • mysqldump
  • mongodump
  • tar
  • pg_dump
  • redisdump

Restore backup methods

  • mysqlrestore
  • mongorestore
  • tarrestore
  • pgrestore
  • redisrestore

Incremental backup of the source backups

  • restic
    • commands
      • restic backup
      • restic forget
      • restic restore
    • storage
      • s3