#!/bin/bash # Script to backup a MySQL database via command-line through the phpMyAdmin script # 09.09.2009 # http://pierre.fritsch.free.fr/ # ADAPT THESE VALUES TO YOUR CONFIGURATION / BEGIN Site=www.myserver.com Database=mydatabase Username=myusername Password=mypassword ServerNo=1 PathToPhpMyAdmin=http://$Site/phpMyadmin # ADAPT THESE VALUES TO YOUR CONFIGURATION / END PathToLogin=$PathToPhpMyAdmin/index.php PathToExport=$PathToPhpMyAdmin/export.php Date=`date "+%Y-%m-%d"` OutputDocument=$Site-$Database-$Date.sql.gz # Temporary files INDEXPHP=/tmp/index.php COOKIESFILE=/tmp/cookies.txt DBEXPORTPHP=/tmp/db_export.php echo echo CLEAN UP echo ======== echo echo rm $COOKIESFILE rm $COOKIESFILE echo rm $INDEXPHP rm $INDEXPHP echo rm $DBEXPORTPHP rm $DBEXPORTPHP echo echo LOG IN AND GET COOKIE echo ===================== echo PostData="pma_username=$Username&pma_password=$Password&server=$ServerNo" echo wget $PathToLogin --post-data=$PostData --keep-session-cookies --save-cookies=$COOKIESFILE --output-document=$INDEXPHP --save-headers wget $PathToLogin --post-data=$PostData --keep-session-cookies --save-cookies=$COOKIESFILE --output-document=$INDEXPHP --save-headers #cat $INDEXPHP echo echo GET TOKEN echo ========= echo #src="navigation.php?lang=en-utf-8&server=6&convcharset=iso-8859-1&collation_connection=utf8_unicode_ci&token=25d6cd72c85bf8a172fb477f930b6682&phpMyAdmin=75ff46662c290ff3f3e57d486ead299f" # ==> we extract the line containing 'navigation.php' # ==> we keep only the part between 'token=' and '&' Token=$(cat $INDEXPHP | grep navigation.php | sed 's/.*token=//' | sed 's/\&.*//') echo Token: $Token echo echo GET DB_EXPORT echo ============= echo PathToDbExport="$PathToPhpMyAdmin/db_export.php?db=$Database&server=$ServerNo&token=$Token&goto=db_structure.php" echo wget $PathToDbExport --load-cookies=$COOKIESFILE --output-document=$DBEXPORTPHP wget $PathToDbExport --load-cookies=$COOKIESFILE --output-document=$DBEXPORTPHP echo echo BUILD REQUEST echo ============= echo #Get target (expected: export.php) #
DbName=$(cat $DBEXPORTPHP | grep " Server=$(cat $DBEXPORTPHP | grep " #Token=$(cat $DBEXPORTPHP | grep "my_db_table for Table in $(cat $DBEXPORTPHP | grep "option value" | sed 's/.*selected\">//' | sed 's/<.*//') do PostParam="$PostParam&table_select%5B%5D=$Table" done #specify export type = SQL PostParam="$PostParam&export_type=database&what=sql" #Add SQL default parameters PostParam="$PostParam&sql_header_comment=&sql_compatibility=NONE&sql_structure=something&sql_if_not_exists=something&sql_auto_increment=something&sql_backquotes=something&sql_data=something&sql_columns=something&sql_extended=something&sql_max_query_size=50000&sql_hex_for_blob=something&sql_type=INSERT" #Add parameter to ask transmission as a gzipped file PostParam="$PostParam&asfile=sendit&filename_template=__DB__&remember_template=on&compression=gzip" echo Post params: $PostParam echo echo PERFORM EXPORT echo ============== echo echo wget $PathToExport --post-data=$PostParam --load-cookies=$COOKIESFILE --output-document=$OutputDocument wget $PathToExport --post-data=$PostParam --load-cookies=$COOKIESFILE --output-document=$OutputDocument echo echo DONE echo ==== echo echo File name: $OutputDocument