Quero baixar meu backup de banco de dados através do script bash

Quero baixar meu backup de banco de dados através do script bash

Quero despejar meu banco de dados em uma data específica em um diretório específico, despejar o banco de dados normalmente com script, mas ele não será despejado no diretório mencionado - despejar apenas no diretório "/" e não no diretório espacial. Aqui meu roteiro-

#! /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

Responder1

Suspeito do problema com ORa operadora que você está usando.

SINTAXE:

if [ condition1 ] || [ condition2 ]

Experimente o código abaixo:

#! /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

....
....

Não sei por que você está pulando alguns dias como 2,3,5,...

Sugiro que você use a opção abaixo se estiver tentando com o mês da semana em relação à calandra.

 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
 ...
 ...

ou se você gostaria de ter literalmente 7 dias por semana, tente abaixo:

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

informação relacionada