[Solved] error with a db script

Issues related to applications and software problems
Post Reply
Kl!nk
Posts: 3
Joined: 2019/11/22 07:22:39

[Solved] error with a db script

Post by Kl!nk » 2019/12/13 10:14:07

Hello

I try to make an automatic backup from my database. I take a script from my debian server ( It turn very well on debian)

I adapt it from the configuration but I have two error

line 49: Premature end of file character (EOF) when searching for the corresponding " " "
and
line 54: Syntax error: premature end of file

I don't understand why I search on google Itry a lot of thing but nothing..
Here is the script :

Code: Select all

#!/bin/bash
# Script de sauvegarde des bases de données mysql/mariadb
# Les valeurs à paramétrer sont en fin de script dans la fonction main()

get_databases_to_backup() {
    mysql_command=$(command -v mysql)
    databases_list=$($mysql_command --defaults-file=/etc/my.cnf -Bse 'show databases')

    for exclude in $databases_exclude_list
    do
       databases_list=${databases_list//$exclude/}
    done
}
dump_databases () {
    ext=".sql.gz"
    mysqldump_command=$(command -v mysqldump)
    compress_command=$(command -v gzip)
    current_date=$(date +%F_%Hh%M)
    cd "${backup_folder}" || exit
    for database in $databases_list
    do
        $mysqldump_command --defaults-file=/etc/mysql/debian.cnf "$database" | $compress_command  > "
        echo "Backing up database: ${database}..."
    done

    echo "$(date +%c): Backup complete!"
}
delete_old_backups() {
    find_command=$(command -v find)
    cd "${backup_folder}" || exit
    $find_command ./ -mtime +"${delete_backups_older_than_days}" -type f -exec rm -v {}  \;
}

main() {
    # toujours exclure information_schema performance_schema de la sauvegarde sinon le script générera$
    databases_exclude_list="mysql phpmyadmin information_schema performance_schema"

    # choix du dossier de destination des sauvegardes à ajuster à vos besoins
    backup_folder="/home/dumpbdd/matin"
    if [ ! -d "$backup_folder" ] ; then
        mkdir -p "$backup_folder"
    fi

    get_databases_to_backup
    dump_databases

    # nombre de jours pendant lesquels il faut conserver les sauvegardes, -1 si illimité
    delete_backups_older_than_days=1
    if [ $delete_backups_older_than_days != "-1" ] ; then
        delete_old_backups
    fi
}
main
thank in advance
Last edited by Kl!nk on 2019/12/13 12:05:32, edited 1 time in total.

lightman47
Posts: 1521
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

Re: error with a db script

Post by lightman47 » 2019/12/13 11:57:33

$mysqldump_command --defaults-file=/etc/mysql/debian.cnf "$database" | $compress_command > "
I don't really know, but might the mis-matched quotes be a problem?

Kl!nk
Posts: 3
Joined: 2019/11/22 07:22:39

Re: error with a db script

Post by Kl!nk » 2019/12/13 12:03:34

Yes you have wright I add an " before $compress_command and it works.

Code: Select all

$mysqldump_command --defaults-file=/etc/mysql/debian.cnf "$database" |" $compress_command > " 
thank you !

lightman47
Posts: 1521
Joined: 2014/05/21 20:16:00
Location: Central New York, USA

Re: [Solved] error with a db script

Post by lightman47 » 2019/12/13 12:41:27

It was saying there was an ODD number of quotes when the script ended. I just read the script backwards looking for pairs of them.

;)

User avatar
KernelOops
Posts: 428
Joined: 2013/12/18 15:04:03
Location: xfs file system

Re: [Solved] error with a db script

Post by KernelOops » 2019/12/17 15:26:22

just a quick suggestion, when using bash variables within a string, its a good idea to enclose them in brackets, for example:

change

Code: Select all

echo this is my $database
to

Code: Select all

echo this is my ${database}
this method allows you to squeeze the variable within the string without spaces, like:

Code: Select all

echo something${database}else
--
R.I.P. CentOS :cry:
--

Post Reply