https://github.com/funnyzak/mysql-onekey-backup
A shell script to backup mysql database and send message with pushoo.
https://github.com/funnyzak/mysql-onekey-backup
backup mysql mysql-backup onekey-mysql-backup pushoo-cli shell
Last synced: about 1 month ago
JSON representation
A shell script to backup mysql database and send message with pushoo.
- Host: GitHub
- URL: https://github.com/funnyzak/mysql-onekey-backup
- Owner: funnyzak
- License: mit
- Created: 2023-01-20T14:03:57.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-11-02T04:14:37.000Z (over 2 years ago)
- Last Synced: 2025-08-31T12:44:17.365Z (10 months ago)
- Topics: backup, mysql, mysql-backup, onekey-mysql-backup, pushoo-cli, shell
- Language: Shell
- Homepage: http://dbback.gp.yycc.dev
- Size: 38.1 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MySQL OneKey Backup
[](http://www.apache.org/licenses/LICENSE-2.0)
[](https://github.com/funnyzak/mysql-onekey-backup/stargazers)
[](https://github.com/funnyzak/mysql-onekey-backup/issues)
[](https://github.com/funnyzak/mysql-onekey-backup/network/members)
[](https://github.com/funnyzak/mysql-onekey-backup/releases)
A shell script to backup mysql database and send message with pushoo.
## Features
- Backup all databases or specified databases.
- Push message with pushoo.
- Delete expired dump files.
- Support custom commands before and after the dump.
- Support custom mysqldump options.
- Support on CentOS 7+、Ubuntu 18.04+、Debian 9+.
## Requirements
You need to install the following software before using the script.
- mysqldump
- pushoo-cli
- nodejs 14+
You can install them with the following command.
```bash
# install mysqldump
bash /path/to/mysql_backup.sh do_install_mysql_client
# install pushoo-cli
bash /path/to/mysql_backup.sh do_install_pushoo_cli
# install nodejs 16
bash /path/to/mysql_backup.sh do_install_nodejs
```
Attention: `You need config pushoo-cli before use it.`
## Arguments
The script has 10 variables, you can set them in the script or pass them as arguments. following is the description of each variable.
- `dump_target_dir` - The directory where the dump file will be saved. Default: `/path/to/db_backups`. Required.
- `db_host` - The host of the database. Default: `127.0.0.1`. Required.
- `db_user` - The user of the database. Default: `root`. Required.
- `db_password` - The password of the database. Default is empty. Required.
- `db_names` - The names of the database. If set empty, all databases will be backed up. Required.
- `db_port` - The port of the database. Default: `3306`. Required.
- `dump_opts` - The options of the mysqldump command. Default: `--single-transaction --quick --lock-tables=false`. Optional.
- `expire_hours` - The dump file at the `specified directory` will be deleted after the specified number of hours. Default: `4320`. Optional.
- `before_dump_command` - The command to be executed before the dump. Default is empty. Optional.
- `after_dump_command` - The command to be executed after the dump. Default is empty. Optional.
for example:
```bash
bash mysql_backup.sh "dump_target_dir" "db_host" "db_user" "db_password" "db_names" db_port "dump_opts" expire_hours "before_dump_command" "after_dump_command"
```
## Usage
```bash
# backup "db1" and "db2" to "/path/to/db_backups" and the dump file will be deleted after 4320 hours.
bash /path/to/mysql_backup.sh "/path/to/db_backups" "127.0.0.1" "root" "examplepassword" "db1 db2" 3306 "--single-transaction --quick --lock-tables=false" 4320
# or
bash /path/to/mysql_backup.sh "/path/to/db_backups" "127.0.0.1" "root" "examplepassword" "db1 db2"
# backup "db1" and "db2" to "/path/to/db_backups" and the dump file will be deleted after 4320 hours. and log to /var/log/db_backup.log
bash /path/to/mysql_backup.sh "/path/to/db_backups" "127.0.0.1" "root" "examplepassword" "db1 db2" >> /var/log/db_backup.log 2>&1
```
## CronTab
You can use crontab to schedule the script to run periodically. For example, you can run the script every day at 3:00 AM.
```bash
0 3 * * * /path/to/mysql_backup.sh "/path/to/db_backups" "127.0.0.1" "root" "examplepassword" "db1 db2" >> /var/log/db_backup.log 2>&1
```
## Docker
You can use docker image, [funnyzak/apline-cron](https://github.com/funnyzak/alpine-cron-docker) to schedule the script to run periodically.
## Reference
- [MySQL Command Line Client Installation Instructions](https://dev.mysql.com/doc/refman/8.0/en/mysql-installation.html)
- [pushoo-cli](https://github.com/funnyzak/pushoo-cli) is a command line tool with Pushoo.js pushes multiple platform messages.
- [Pushoo.js](https://github.com/imaegoo/pushoo)
## Contribution
If you have any questions or suggestions, please feel free to submit an issue or pull request.
## License
MIT License © 2023 [funnyzak](https://github.com/funnyzak)