1

I want to dump my database in a spacific date in a spacific directory,database dump normally with script but it will not dump in mentioned directory - dump under only "/" directory not spacified directory. Here my script-

#! /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
  • we should use || instead of | in if and elif condition – msp9011 Aug 28 '18 at 7:02
0

I suspect the issue with OR operator which you are using.

SYNTAX:

if [ condition1 ] || [ condition2 ]

Try the below code:

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

....
....

I'm not sure why you are skipping a few days like 2,3,5,...

I suggest you to use with the below option if you are triying with month of the week with respect to calander.

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

or if you woud like to have literal 7 days week, try below:

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
  • I want to dump the database in spacific date in spacific directory. With my syntax database dump in mentioned date but it will dump in "/" directory instead of "/backup/database/week1..4" directory. – Aniket Biswas Aug 28 '18 at 7:40
  • Yes I have tried, now the database is dump in mentioned path. Thank u. – Aniket Biswas Aug 28 '18 at 10:50
  • @AniketBiswas you can accept the answer if it works. – msp9011 Aug 28 '18 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.