I am making a bash script to make backup copies of db. Basically I want something like:create_backup.sh {database_name} {table_name}
and run mysqldump database_name.table_name > date.sql
something like this.
I want database_name
and to table_name
be optional, that is, if they don't come instead of those vars I will select everything : *.*
.
But it doesn't do it right for me, this is my script:
#!/bin/bash
now=$( date '+%F_%H:%M:%S' )
database_name='*'
table_name='*'
if [ -n "$1" ]; then
$database_name="$1"
fi
if [ -n "$2" ]; then
$table_name="$2"
fi
echo "$database_name y $table_name"
echo "mysqldump $database_name.$table_name > $now.sql"
Clarification: Now I am doing an echo to test that I get what I want.
The script doesn't give me an error, but it doesn't do what it should.
If you need to make one
mysqldump
of all the tables in a DB you don't need to pass*
. Even so, I will answer the question of how to treat the arguments.Examples:
Note: