How To Use AWK To Simplify Scripting: Multi-Database Backup Example

By Angsuman Chakraborty, Gaea News Network
Sunday, April 20, 2008

AWK is an extremely versatile and powerful language for processing files and text data. Often you can use awk to perform complex tasks on Unix / Linux / Mac OS command line which would otherwise take a full-fledged programming effort. AWK is great for log processing, creating custom scripts which will then be executed and more. For example I wanted to create a backup of all of my MySQL databases, each to a separate file after compressing them with gzip (maximum compression). I already created a list of databases on my MySQL server is a file named databases (how original!). Here is the awk script which processes the file and creates another file (named database_backup) which contains commands to backup all the databases as specified above.

awk '{print "mysqldump -u root -p --opt",$1,"|gzip -9 >",$1 ".sql.gz"}' < databases > database_backup

I actually use a slightly modified script where I specify the password after -p so I don’t have to type it every time and also enables me to run this as a cron job.

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :