Skip to content

Instantly share code, notes, and snippets.

@gilbitron
Last active March 1, 2023 22:06
Show Gist options
  • Save gilbitron/13a4134fe91cee080ce31cd5199a13c1 to your computer and use it in GitHub Desktop.
Save gilbitron/13a4134fe91cee080ce31cd5199a13c1 to your computer and use it in GitHub Desktop.
Laravel Homestead daily database backup cron provision (after.sh)
#!/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.
if [ ! -f /etc/cron.daily/database-backup ]; then
sudo bash -c "cat > /etc/cron.daily/database-backup" <<EOL
#!/bin/bash
# Database credentials
user="homestead"
password="secret"
# Other options
backup_path="/home/vagrant/backup"
date=\`date +%Y-%m-%d\`
# Dump database into SQL file
databases=\`mysql -u \$user -p\$password -e "SHOW DATABASES;" | grep -Ev "(mysql|information_schema|performance_schema|Database|sys)"\`
for db in \$databases; do
echo "Backing up database \${db}..."
mysqldump --force --opt -u \$user -p\$password --databases \$db > "\${backup_path}/\${db}_\${date}.sql"
done
# Delete files older than 30 days
find \$backup_path/* -mtime +30 -exec rm {} \;
EOL
sudo chmod +x /etc/cron.daily/database-backup
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment