crontab 删除mysql数据_Linux Crontab实现定时备份和删除Mysql数据库

Loading...

# Linux Crontab实现定时备份和删除Mysql数据库

关于 **crontab** 使用可参考我博客下边的这篇文章:

[Linux创建定时任务](https://blog.dwtowen.com/archives/202/)

## 备份数据库

```

# 备份所有数据库

mysqldump -uroot -p123456 --lock-tables=false --all-databases > /data/mysqlbak/`date +%Y%m%d`.sql

```

```

# 备份不同的数据库到不同的目录

mysqldump -uroot -p123456 baskets > /Backups/mysql-backup/databases/baskets/$(date +%Y%m%d_%H%M%S)_baskets.sql

mysqldump -uroot -p123456 edusoho > /Backups/mysql-backup/databases/edusoho/$(date +%Y%m%d_%H%M%S)_edusoho.sql

mysqldump -uroot -p123456 gt > /Backups/mysql-backup/databases/gt/$(date +%Y%m%d_%H%M%S)_gt.sql

mysqldump -uroot -p123456 typecho > /Backups/mysql-backup/databases/typecho/$(date +%Y%m%d_%H%M%S)_typecho.sql

```

## 删除数据库

```

# 删除所有30天前的数据库

find /data/mysqlbak -mtime +30 -name "*.*" -exec rm -rf {} \;

```

```

# 删除 Local Mysql Databases

find /Backups/mysql-backup/databases/baskets -mtime +30 -name "*.*" -exec rm -rf {} \;

find /Backups/mysql-backup/databases/edusoho -mtime +30 -name "*.*" -exec rm -rf {} \;

find /Backups/mysql-backup/databases/gt -mtime +30 -name "*.*" -exec rm -rf {} \;

find /Backups/mysql-backup/databases/typecho -mtime +30 -name "*.*" -exec rm -rf {} \;

```

```

# 删除 Docker Seafile Mysql Databases

find /opt/docker_seafile/seafile-mysql/db/seafile-backup/databases/ccnet_db -mtime +30 -name "*.*" -exec rm -rf {} \;

find /opt/docker_seafile/seafile-mysql/db/seafile-backup/databases/seafile_db -mtime +30 -name "*.*" -exec rm -rf {} \;

find /opt/docker_seafile/seafile-mysql/db/seafile-backup/databases/seahub_db -mtime +30 -name "*.*" -exec rm -rf {} \;

```

## 加入Crontab定时任务

```

crontab -e

```

```

# m h dom mon dow command

0 0 * * * /Backups/mysql-backup/mysql-backup.py

0 0 * * * /opt/docker_seafile/seafile-mysql/db/seafile-backup/seafile-backup.py

0 0 * * * /Backups/mysql-backup/mysql-backup-del.py

```

以上的 `0 0 * * *` 代表,在每天的凌晨 00:00 分执行脚本。

End Thanks!♈

最后修改:2021 年 01 月 12 日 11 : 55 AM

© 允许规范转载

赞赏

如果觉得我的文章对你有用,请随意赞赏

×Close

赞赏作者

扫一扫支付

png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAABS2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4KPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxMzggNzkuMTU5ODI0LCAyMDE2LzA5LzE0LTAxOjA5OjAxICAgICAgICAiPgogPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIi8+CiA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgo8P3hwYWNrZXQgZW5kPSJyIj8+IEmuOgAAAA1JREFUCJljePfx038ACXMD0ZVlJAYAAAAASUVORK5CYII=

png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAABS2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4KPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxMzggNzkuMTU5ODI0LCAyMDE2LzA5LzE0LTAxOjA5OjAxICAgICAgICAiPgogPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIi8+CiA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgo8P3hwYWNrZXQgZW5kPSJyIj8+IEmuOgAAAA1JREFUCJljePfx038ACXMD0ZVlJAYAAAAASUVORK5CYII=

支付宝支付

微信支付

你可能感兴趣的:(crontab,删除mysql数据)