Automated Backup of mysqlDB on cpanel

Here we will learn how to backup all databases of cPanel server in specified folder say /home/mysqlbackup

First of all we will list all databases in a file using command

  • echo “show databases” | mysql > /home/DB_list.txt

By default first line of the output is DATABASE and rest of lines are name of databases.

Second command will help to get rid of first line.

  • tail -n +2 /home/DB_list.txt > /home/DB_list2.txt

So we have DB_list2.txt with list of all databases.

Here we will use for loop to backup all databases listed in DB_list2.txt

  • for m in `cat /home/DB_list2.txt`;do mysqldump $m > /home/mysqlbackup/$m.sql;done