
特定の日付の特定のディレクトリにデータベースをダンプしたいのですが、データベースは通常スクリプトでダンプされますが、指定されたディレクトリにはダンプされません。指定されたディレクトリではなく、「/」ディレクトリの下にのみダンプされます。これが私のスクリプトです。
#! /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