0

I'm trying to understand the following commands in /etc/default/automysqlbackup file:

USERNAME=`grep user /etc/mysql/debian.cnf | tail -n 1 | cut -d"=" -f2 | awk '{print $1}'`

PASSWORD=`grep password /etc/mysql/debian.cnf | tail -n 1 | cut -d"=" -f2 | awk '{print $1}'`

DBNAMES=`find /var/lib/mysql -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f5 | grep -v ^mysql\$ | tr \\\r\\\n ,\ `

DBNAMES=`mysql --defaults-file=/etc/mysql/debian.cnf --execute="SHOW DATABASES" | awk '{print $1}' | grep -v ^Database$ | grep -v ^mysql$ | grep -v ^performance_schema$ | grep -v ^information_schema$ | tr \\\r\\\n ,\ `

but I don't get it at all. I would really appreciate if somebody could explain to me what's going on in there step-by-step.

1 Answer 1

3
USERNAME=`grep user /etc/mysql/debian.cnf | tail -n 1 | cut -d"=" -f2 | awk '{print $1}'`

runs a pipeline with the following commands:

  • grep user /etc/mysql/debian.cnf, which extracts all lines containing “user” from /etc/mysql/debian.cnf;
  • tail -n 1, which shows only the last line of its input;
  • cut -d"=" -f2, which extracts the second part of the line delimited by “=”;
  • awk '{print $1}', which removes all the spaces surrounding the first word in the line.

The purpose (not perfectly implemented) of all this is to extract the last value assigned to user in /etc/mysql/debian.cnf. The result is stored in a variable named USERNAME.

The PASSWORD line is similar.

DBNAMES=`find /var/lib/mysql -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f5 | grep -v ^mysql\$ | tr \\\r\\\n ,\ `

runs the following commands:

  • find /var/lib/mysql -mindepth 1 -maxdepth 1 -type d, which lists the directories in /var/lib/mysql, with their full paths;
  • cut -d'/' -f5, which extracts the fifth field delimited by “/”, i.e. the directories’ names;
  • grep -v ^mysql\$, which removes any line matching exactly “mysql”;
  • tr \\\r\\\n ,\ , which replaces all carriage returns with “,” and all newlines with a space.

The purpose is to list all the directories in /var/lib/mysql, separated by spaces, excluding the /var/lib/mysql/mysql directory. The result is stored in a variable named DBNAMES.

DBNAMES=`mysql --defaults-file=/etc/mysql/debian.cnf --execute="SHOW DATABASES" | awk '{print $1}' | grep -v ^Database$ | grep -v ^mysql$ | grep -v ^performance_schema$ | grep -v ^information_schema$ | tr \\\r\\\n ,\ `

has the same purpose, but it parses the output of MySQL’s SHOW DATABASES command. awk '{print $1}' is used to extract the first word from each line, then all the greps filter out various values.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .