Skip to content

Instantly share code, notes, and snippets.

@mikebronner
Created December 7, 2017 21:55
Show Gist options
  • Save mikebronner/72c254796eec0fa89521fc3c6e6ce062 to your computer and use it in GitHub Desktop.
Save mikebronner/72c254796eec0fa89521fc3c6e6ce062 to your computer and use it in GitHub Desktop.
Homestead Database Backups via cron
#!/bin/sh
# If you would like to do some extra provisioning you may
# add any commands you wish to this file and they will
# be run after the Homestead machine is provisioned.
sudo rm /etc/cron.daily/database-backup
sudo bash -c "cat > /etc/cron.daily/database-backup" <<EOL
#!/bin/bash
# Database credentials
user="homestead"
password="secret"
# Other options
backup_path="/home/vagrant/Sites/database-backups"
date=`date +%Y-%m-%d`
export PGPASSWORD="$password"
export MYSQL_PWD="$password"
mkdir -p $backup_path
# Dump database into SQL file
echo "Backing up MySQL databases..."
mysqldump --force --opt -u $user --all-databases > "${backup_path}/mysql_dump-${date}.sql"
echo "Backing up PostgreSQL databases..."
pg_dumpall --username=homestead --host=localhost > "${backup_path}/pgsql_dump-${date}.sql"
# Delete files older than 7 days
find $backup_path/* -mtime +30 -exec rm {} \;
EOL
sudo chmod +x /etc/cron.daily/database-backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment