Bash script: mysql database backup
System administration

Bash script: mysql database backup

February 1, 2015

There is my approach to backup a mysql database. I have put all the required parameters in bash variables, so the code can be easily reusable.

In addition to the core functionality (making backup) there is also the functionality to delete the older files that the specified number of days. For example, we can set to delete the backup files older than one month. The code is located below:

Explanation of the code above:

  1. Setting database parameter (username, password, database name and the host address to the database)
  2. Setting file system parameters:
    1. Folder where the backup will be exported
    2. Prefix of the backup file
    3. And the information how old files you want to delete after the backup is done
  3. Then is taking place the file name generator of the new backup file
  4. Then the backup is finally retrieved
  5. And in the last step is taking place the deleting of older backup files

If this code is usefully for you, just grab it and customize it as you want. You can also fork it from my github repository:





Follow deanpodgornik on

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *