想要通过 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

相关内容