Backing Up Data To Git Securely: Part-1 - Create the Database User
In my backup script I previously posted, we begin by dumping the entire contents of the database to a file. This is not necessarily a good idea and there may be better ways to backup your data. However, the total size of my database is only a few MBs so it isn’t worth the hassle of creating a master-slave setup.
The goal is to dump the database to the filesystem, add it to the tar file, encrypt it and then delete the clear text version of the file.
Security considerations abound. In a nutshell however, it is important not to publish any unencrypted database dump anywhere. Dump files can be searched on easily by Google and if you house even a morsel of private user data, you’ll be in trouble very quickly if it’s not encrypted.
Getting started, the MySQL command to dump a database is:
mysqldump -u user -psupersecret –all-databases > /some/dir/backupsql.sql
-p will prompt for a password
-all-databases will dump every database you have running on the mysql instance. If you want a specific database, replace –all-databases with your_db_name
This command pushes the output of the mysqldump command to a file on the filesystem.
Since I don’t want my cron job to be prompted for a user’s password when dumping the db, I have created a special backup user with only enough permissions to read the tables, and write their contents to a file. You can create such a user by running the following command inside a mysql prompt:
GRANT SELECT, LOCK TABLES ON *.* TO ‘backup’@'localhost’ IDENTIFIED BY ’supersecret’
Make sure to flush privileges after creating users. There’s not much more frusterating than trying to figure out why that script didn’t work.
One security consideration is that if you were to run the ps command while the dump script is running, it will expose the username and password of the backup user potentially allowing other users of the system to make select statements to databases they otherwise shouldn’t be on.
By limiting the backup user access only from the localhost, this eliminates the possibility of someone else dumping your database from some other host; the user must be logged into the box the database resides on.
At the conclusion of this script, we will have all the database data and structure backed up to a file on the filesystem. In the next post, I’ll explain how to add this data to a tar archive and then encrypt the whole mess.


After the login succeeds, the Login Status will show “Connected” and you should be connected to the Internet.