想要透過 bash 腳本下載我的資料庫備份

想要透過 bash 腳本下載我的資料庫備份

我想將我的資料庫轉儲到一個空間目錄中的一個空間日期中,資料庫轉儲通常使用腳本,但它不會轉儲到提到的目錄中- 僅轉儲在“/”目錄下而不是空間目錄下。這是我的腳本-

#! /bin/bash
now=$(date +%d)
if [ "$now" == 1 ] | [ "$now" == 4 ] | [ "$now" == 7 ]
then
BACKUP_DIR="/backup/database/week1"
elif [ "$now" == 10 ] | [ "$now" == 13 ]
then
BACKUP_DIR="/backup/database/week2"
elif [ "$now" == 16 ] | [ "$now" == 19 ]
then
BACKUP_DIR="/backup/database/week3"
elif [ "$now" == 22 ] | [ "$now" == 25 ] | [ "$now" == 28 ] | [ "$now" == 31 ]
then
BACKUP_DIR="/backup/database/week4"
fi

TIMESTAMP=$(date -u +"%d-%m-%Y")
MYSQL_USER="backupuser"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="efeww2"
MYSQLDUMP=/usr/bin/mysqldump
mkdir -p "$BACKUP_DIR/$TIMESTAMP"

databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|mysql|information_schema|performance_schema|phpmyadmin)"`

for db in $databases; do
  $MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db > $BACKUP_DIR/$TIMESTAMP/$db-$(date +%Y-%m-%d-%H.%M.%S).sql
done

答案1

OR我懷疑您正在使用的運營商有問題。

句法:

if [ condition1 ] || [ condition2 ]

嘗試下面的程式碼:

#! /bin/bash
now=$(date +%d)
if [ "$now" == 1 ] || [ "$now" == 4 ] || [ "$now" == 7 ]
then
BACKUP_DIR="/backup/database/week1"
elif [ "$now" == 10 ] || [ "$now" == 13 ]
then
BACKUP_DIR="/backup/database/week2"
elif [ "$now" == 16 ] || [ "$now" == 19 ]
then
BACKUP_DIR="/backup/database/week3"
elif [ "$now" == 22 ] || [ "$now" == 25 ] || [ "$now" == 28 ] || [ "$now" == 31 ]
then
BACKUP_DIR="/backup/database/week4"
fi

....
....

我不知道你為什麼要跳過幾天,像是2、3、5…

如果您正在嘗試使用日曆的一周中的月份,我建議您使用以下選項。

 now=`echo $((($(date +%-d)-1)/7+1))`
 if [ "$now" -eq 1 ]; then
    BACKUP_DIR="/backup/database/week1"
 elif [ "$now" -eq 2 ]; then
    BACKUP_DIR="/backup/database/week2"
 elif [ "$now" -eq 3 ]; then
    BACKUP_DIR="/backup/database/week3"
 else
    BACKUP_DIR="/backup/database/week4"
 fi
 ...
 ...

或者如果您想每週工作 7 天,請嘗試以下操作:

now=$(date +%d)
 if [ "$now" -le 7 ]; then
    BACKUP_DIR="/backup/database/week1"
 elif [ "$now" -le 14 ]; then
    BACKUP_DIR="/backup/database/week2"
 elif [ "$now" -le 21 ]; then
    BACKUP_DIR="/backup/database/week3"
 else
    BACKUP_DIR="/backup/database/week4"
 fi

相關內容