Godaddy is a great place for starting a small simple project for yourselves.
It has a lot of paid options to backup, but that’s not something we want to do, right? We have 2 hands and we can definitely do it ourselves.
Here are a few commands to get you started backing up your database on a nightly basis:
1) First you will need to setup your .my.cnf file so your cron mysqldump command will run and not need to have a password prompt pop-up.
1 | vi ~/.my.cnf |
or
1 | nano ~/.my.cnf |
2) Then we need to add our mysql user and password to this file:
1 2 3 | [mysqldump] user = mysqluser password = secret |
Save and exit.
3) To secure our file from being accessed by others, we set permissions to owner only:
1 | chmod 600 ~/.my.cnf |
4) Ok, now we need to create a directory to put our backups into. We need to put it outside of /home/user/public_html folder, because we need to keep our backups out of hands of hackers.
1 | mkdir /home/user/backups/ |
and then set restrictions for it to be accessible only by you:
1 | chmod -R 600 /home/user/backups/ |
This will set those permissions on folders, sub-folders and files of /home/user/backups folder.
5) To finish up, we will test our mysqldump command:
1 | /usr/bin/mysqldump -u user database | /usr/bin/zip > /home/user/backups/database_$(date +\%d-\%m-\%Y).sql.zip |
This will create a zip file with your MySQL database dump and a date in it’s name (in case you need to find it later).
6) To make things easier, we’ll go now to our Godaddy host and just create a cron job through CPanel / WHM:
and then paste our command into cron job:
Done!
You should now see your backups run every night at 1am.
In next articles we will write about how to encrypt these backups with GPG to make sure if somebody does get access to them, they cannot use them.