centos7 每天定時備份 mysql資料庫

2022-06-17 21:42:17 字數 4137 閱讀 1401

#!/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 > $\/$_$.sql.gz

#just

backup the latest 5

days

find $

-mtime +

5-name "$_*.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 > $\/$_$.sql.gz

/usr/bin/mysqldump -u$db_username -p$db_password -h 192.168.10.122 -r --opt $database_hnocean | gzip > $\/$_$.sql.gz

/usr/bin/mysqldump -u$db_username -p$db_password -h 192.168.10.122 -r --opt $database_syocean | gzip > $\/$_$.sql.gz

/usr/bin/mysqldump -u$db_username -p$db_password -h 192.168.10.122 -r --opt $database_whocean | gzip > $\/$_$.sql.gz

/usr/bin/mysqldump -u$db_username -p$db_password -h 192.168.10.122 -r --opt $database_bhocean | gzip > $\/$_$.sql.gz

/usr/bin/mysqldump -u$db_username -p$db_password -h 192.168.10.122 -r --opt $database_yljocean | gzip > $\/$_$.sql.gz

/usr/bin/mysqldump -u$db_username -p$db_password -h 192.168.10.122 -r --opt $database_ytsocean | gzip > $\/$_$.sql.gz

/usr/bin/mysqldump -u$db_username -p$db_password -h 192.168.10.122 -r --opt $database_dyocean | gzip > $\/$_$.sql.gz

/usr/bin/mysqldump -u$db_username -p$db_password -h 192.168.10.122 -r --opt $database_dlocean | gzip > $\/$_$.sql.gz

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

find $ -mtime +30 -name "$_*.sql.gz" -exec -rf \;

find $ -mtime +30 -name "$_*.sql.gz" -exec -rf \;

find $ -mtime +30 -name "$_*.sql.gz" -exec -rf \;

find $ -mtime +30 -name "$_*.sql.gz" -exec -rf \;

find $ -mtime +30 -name "$_*.sql.gz" -exec -rf \;

find $ -mtime +30 -name "$_*.sql.gz" -exec -rf \;

find $ -mtime +30 -name "$_*.sql.gz" -exec -rf \;

find $ -mtime +30 -name "$_*.sql.gz" -exec -rf \;

find $ -mtime +30 -name "$_*.sql.gz" -exec -rf \;

centos7下新增mysql定時備份

1.建立要執行的備份任務 建立mysql的備份檔案的儲存路徑,我的是在 home下建立了mysql data bak資料夾 給該資料夾分配744的許可權 chmod 744 mysql data bak 建立要執行備份的指令碼檔案mysql.sh,我的是在 home bin目錄下建的 touch m...

centos7設定mysql每日定時備份

建立mysql的備份檔案的儲存路徑,我的是在 home下建立了mysql data bak資料夾 給該資料夾分配744的許可權 chmod 744 mysql data bak 建立要執行備份的指令碼檔案mysql.sh,我的是在 home目錄下建的 touch mysql.sh 給該檔案分配744...

CentOS 7解除安裝mariadb安裝mysql

centos 7已經將預設整合mariadb而不是mysql,這對於多數還是依賴於mysql的應用來說,需要手動的進行更新。可能會遇到這樣錯誤,換成mysql就好了。error 2002 hy000 mysql.sock var lib mysql mysql.sock 首先檢視都安裝了哪些的mar...