Posted on in Development
Laravel Homestead is an awesome tool for building Laravel sites. However, sometimes it would be nice if when you did a vagrant destory you wouldn’t lose all of the information in your databases. Or in my case, if you database crashes and you can’t get it back up, it would be great if you had a database backup to restore from.
Thankfully it’s relatively simple to create a database backup cron on Homestead (v0.5.0 at time of writing). First we need to add a folder mapping as a location to store our backups so they persist on our host machine. To do this edit your ~/.homestead/Homestead.yaml (or your local Homestead.yaml if you use a per-project installation):
folders: - map: "~/HomesteadBackup" to: /home/vagrant/backup
Make sure to vagrant reload — provision to create the new backup location on the VM.
Then we can use Homestead’s after.sh provisioning file to create a daily cron to run our backup script: Edit ~/.homestead/after.sh and add the following script:
This script will create a daily cron that will backup each of your databases, in separate SQL files, in the specified backup location. It will also delete backups older than 30 days to save space.
To install the new backup script you simply need to run vagrant provision.