Hello everybody,
I have a database backup code, using mysqldump, and I would like to ignore some tables, after getting the variable echo outside the loop, I ve got the last, the first one was overridden:
ignoreTables=('visitors_15012016 visitors_Original')
for line in $ignoreTables
do
echo 'mysqldump --ignore-table='$line
done
echo 'li '$line
mysqldump --log-error=/var/log/mysql-dump/dump.log -u USER -pPASSWD DBName $line | bzip2 > /path2file/file.sql.bz2
I am looking form something like:
mysqldump --log-error=/var/log/mysql-dump/dump.log -u USER -pPASSWD DBName --ignore-table=visitors_15012016 --ignore-table=visitors_Original | bzip2 > /path2file/file.sql.bz2
Thanks in advance
ignoreTables=( visitors_15012016 visitors_Original )
ignoreString=''
for tbl in ${ignoreTables[@]}
do
ignoreString=${ignoreString}"--ignore-table="${tbl}" "
done
mysqldump --log-error=/var/log/mysql-dump/dump.log -u USER -pPASSWD DBName $ignoreString | bzip2 > /path2file/file.sql.bz2
1 Like
Thank you dude, could you explain the code for me please? I am still a newbie
[highlight=bash]
# create array. take care of the syntax .. spaces and parenthesis
ignoreTables=( visitors_15012016 visitors_Original )
# take an initial dummy empty variable for building the required string
# we need --ignore-table=tbl1 --ignore-table=tbl2 and so on...
ignoreString=''
# loop over the array. ${arr_var[@]} simply gives back all the elements of the array
for tbl in ${ignoreTables[@]}
do
# build the string variable. with each pass of the loop, the new table name is appended to the old one along with the mysqldump switch "--ignore-table"
ignoreString=${ignoreString}"--ignore-table="${tbl}" "
done
# now at the end of loop, ignoreString contains
# "--ignore-table=tbl1 --ignore-table=tbl2"
# use it in the below command
mysqldump --log-error=/var/log/mysql-dump/dump.log -u USER -pPASSWD DBName $ignoreString | bzip2 > /path2file/file.sql.bz2
[/highlight]
Oh yeah got it, I use to do it in PHP but not the same concept:
$string=''
for(){
$string .='something else';
} // end for
echo $string;
I think if my array is something like WILL NOT work, will return the last element in the array, I use to set my arrays as below:
ignoreTbl=( 'visitors_15012016' 'visitors_Original' )
There are 2 elements there:
ignoreTables=( 'visitors_15012016 visitors_Original' )
entries1=${#ignoreTables[@]}
echo 'ignoreTables has '$entries1' elements' // WILL RETURN 1
ignoreTbl=( 'visitors_15012016' 'visitors_Original' )
entries2=${#ignoreTbl[@]}
echo 'ignoreTbl has '$entries2' elements' // WILL RETURN 2