I have a script:
Code:
#!/bin/bash
#create /root/.my.cnf and add:
#[mysqldump]
#user=root
#password=PASSWORD
#remove the password and user for mysqldump from script
#Wtedy nie będzie warningów w konsoli
DATE=$(date +"%d-%m-%Y")
DIRECTORY="/var/www/biosfera.it/home/biosfera_sh/backups"
MYSQLDUMP=/usr/bin/mysqldump
#need create .password file in proper directory contains data in form -> user,password
MYSQL_USER="`awk 'BEGIN {FS=","} {print $1}' /root/.credentials`"
MYSQL_PASSWORD="`awk 'BEGIN {FS=","} {print $2}' /root/.credentials`"
#file will be stored in this place
cd $DIRECTORY
#backup the database, each in own file
databases=$(mysql --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -E 'biosfera_base1|biosfera_base2')
for database in $databases
do
echo -e "\e[38;5;40mI am doing backup of $database.\e[39m"
$MYSQLDUMP --force --log-error=error_file.log --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $database > $database.sql
done
#here need to archive and compress each file
echo -e "\e[38;5;45mNow I am going to create zipped archives to save some disk space and remove not necessary files. Please, give me a moment. :)\e[39m"
db="*.sql"
for archive in $db
do
tar -czvf $archive.tar.gz $archive >/dev/null 2>&1
/bin/mv $archive.tar.gz $archive"_"$DATE.tar.gz
done
#make the backup files readable only by root
/bin/chmod 600 *.tar.gz
#delete unnecessary dumped db files
rm *.sql
echo
echo "Open path $DIRECTORY to see details."
#mail admin that all went properly
echo "All databases from server $(hostname -f) became backuped. You can check the log file attached to message." | mutt -s "Report - backup from day $(date +%y-%m-%d-%H:%M:%S)" backupdb@pol.it -a "$DIRECTORY/error_file.log"
echo
echo -e "\e[38;5;45mFinished doing databases backup.\e[39m"
In line
Code:
$MYSQLDUMP --force --log-error=error_file.log --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $database > $database.sql
after add $database"-"$DATE.sql (except finishing $database.sql) files are not created. I tried few ways like $database$DATE.sql, ${database}-${DATE} and few more. Nothing works (for testing purposes I commented out archiving loop and removing .sql files). I just want create filename using the name of specific database and date. Of course I tried this same in archive loop. Without success.
As you can see in above script I finally added -->
Code:
/bin/mv $archive.tar.gz $archive"_"$DATE.tar.gz
in loop which does this job. But I am curious why above ways don't work.
PS
Is it possible to suppress showing mysql warning about user/pass in above script?
Bookmarks