花花_新世界
从【程序猿】到【程序员】的进化历程

第一步:编写数据库备份脚本database_mysql_shell.sh

#!/bin/bash
DATE=`date +%Y%m%d%H%M`                #every minute
DATABASE=springboot-admin              #database name
DB_USERNAME=root                       #database username
DB_PASSWORD="mysql"                    #database password
BACKUP_PATH=/backup/mysqldata          #backup path

#backup command

/usr/bin/mysqldump -u$DB_USERNAME -p$DB_PASSWORD -h 127.0.0.1 -R --opt $DATABASE | gzip > ${BACKUP_PATH}\/${DATABASE}_${DATE}.sql.gz

#just backup the latest 5 days

find ${BACKUP_PATH} -mtime +5 -name "${DATABASE}_*.sql.gz" -exec -rf;

第二步:给脚本授权

chmod +x database_backup_shell.sh

第三步:编写定时备份任务

输入如下命令:

crontab -e

在页面中编写如下内容:

00 1 * * * /root/database_backup_shell.sh

可实现每天凌晨一点自动执行第一步编写的脚本,备份mysql数据库到/backup/mysqldata,且只保留最近5天的数据库备份。

最后,如果这篇博客帮到你了,希望你可以关注一下左侧我的店铺。本人一直秉承着认真卖好货的信念,把顾客的体验放在第一位。感兴趣可以扫描左侧二维码进群了解更多详情,另外,如果你观察下来觉得这个平台还不错,也想分享给身边更多的朋友,可以邀请朋友们进群一起享优惠哦。接下来,几千个品牌的商品我会逐一和大家分享,再次感谢大家的支持❤❤❤

完整示例:

#!/bin/bash
DATE=`date +%Y%m%d%H%M`                #every minute
DATABASE_zfhyocean=zfhyocean              #database name
DATABASE_hnocean=hnocean              #database name
DATABASE_syocean=syocean              #database name
DATABASE_whocean=whocean              #database name
DATABASE_bhocean=bhocean              #database name
DATABASE_ytsocean=ytsocean              #database name
DATABASE_yljocean=yljocean              #database name
DATABASE_dyocean=dyocean              #database name
DATABASE_dlocean=dlocean              #database name
DB_USERNAME=root                       #database username
DB_PASSWORD="root"                    #database password
BACKUP_PATH_zfhyocean=/var/mysqlbackup/zfhyocean          #backup path
BACKUP_PATH_hnocean=/var/mysqlbackup/hnocean          #backup path
BACKUP_PATH_syocean=/var/mysqlbackup/syocean          #backup path
BACKUP_PATH_whocean=/var/mysqlbackup/whocean          #backup path
BACKUP_PATH_bhocean=/var/mysqlbackup/bhocean          #backup path
BACKUP_PATH_ytsocean=/var/mysqlbackup/ytsocean          #backup path
BACKUP_PATH_yljocean=/var/mysqlbackup/yljocean          #backup path
BACKUP_PATH_dyocean=/var/mysqlbackup/dyocean          #backup path
BACKUP_PATH_dlocean=/var/mysqlbackup/dlocean          #backup path

#backup command

/usr/bin/mysqldump -u$DB_USERNAME -p$DB_PASSWORD -h 192.168.10.122 -R --opt $DATABASE_zfhyocean | gzip > ${BACKUP_PATH_zfhyocean}\/${DATABASE_zfhyocean}_${DATE}.sql.gz
/usr/bin/mysqldump -u$DB_USERNAME -p$DB_PASSWORD -h 192.168.10.122 -R --opt $DATABASE_hnocean | gzip > ${BACKUP_PATH_hnocean}\/${DATABASE_hnocean}_${DATE}.sql.gz
/usr/bin/mysqldump -u$DB_USERNAME -p$DB_PASSWORD -h 192.168.10.122 -R --opt $DATABASE_syocean | gzip > ${BACKUP_PATH_syocean}\/${DATABASE_syocean}_${DATE}.sql.gz
/usr/bin/mysqldump -u$DB_USERNAME -p$DB_PASSWORD -h 192.168.10.122 -R --opt $DATABASE_whocean | gzip > ${BACKUP_PATH_whocean}\/${DATABASE_whocean}_${DATE}.sql.gz
/usr/bin/mysqldump -u$DB_USERNAME -p$DB_PASSWORD -h 192.168.10.122 -R --opt $DATABASE_bhocean | gzip > ${BACKUP_PATH_bhocean}\/${DATABASE_bhocean}_${DATE}.sql.gz
/usr/bin/mysqldump -u$DB_USERNAME -p$DB_PASSWORD -h 192.168.10.122 -R --opt $DATABASE_yljocean | gzip > ${BACKUP_PATH_yljocean}\/${DATABASE_yljocean}_${DATE}.sql.gz
/usr/bin/mysqldump -u$DB_USERNAME -p$DB_PASSWORD -h 192.168.10.122 -R --opt $DATABASE_ytsocean | gzip > ${BACKUP_PATH_ytsocean}\/${DATABASE_ytsocean}_${DATE}.sql.gz
/usr/bin/mysqldump -u$DB_USERNAME -p$DB_PASSWORD -h 192.168.10.122 -R --opt $DATABASE_dyocean | gzip > ${BACKUP_PATH_dyocean}\/${DATABASE_dyocean}_${DATE}.sql.gz
/usr/bin/mysqldump -u$DB_USERNAME -p$DB_PASSWORD -h 192.168.10.122 -R --opt $DATABASE_dlocean | gzip > ${BACKUP_PATH_dlocean}\/${DATABASE_dlocean}_${DATE}.sql.gz

#just backup the latest 30 days(rm -f {} \改成-rf)

find ${DATABASE_zfhyocean} -mtime +30 -name "${DATABASE_zfhyocean}_*.sql.gz" -exec -rf \;
find ${DATABASE_hnocean} -mtime +30 -name "${DATABASE_hnocean}_*.sql.gz" -exec -rf \;
find ${DATABASE_syocean} -mtime +30 -name "${DATABASE_syocean}_*.sql.gz" -exec -rf \;
find ${DATABASE_whocean} -mtime +30 -name "${DATABASE_whocean}_*.sql.gz" -exec -rf \;
find ${DATABASE_bhocean} -mtime +30 -name "${DATABASE_bhocean}_*.sql.gz" -exec -rf \;
find ${DATABASE_ytsocean} -mtime +30 -name "${DATABASE_ytsocean}_*.sql.gz" -exec -rf \;
find ${DATABASE_yljocean} -mtime +30 -name "${DATABASE_yljocean}_*.sql.gz" -exec -rf \;
find ${DATABASE_dyocean} -mtime +30 -name "${DATABASE_dyocean}_*.sql.gz" -exec -rf \;
find ${DATABASE_dlocean} -mtime +30 -name "${DATABASE_dlocean}_*.sql.gz" -exec -rf \;

 

posted on 2020-07-24 08:46  花花_新世界  阅读(425)  评论(0编辑  收藏  举报