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

おすすめ記事