Last active
March 1, 2023 22:06
-
-
Save gilbitron/13a4134fe91cee080ce31cd5199a13c1 to your computer and use it in GitHub Desktop.
Laravel Homestead daily database backup cron provision (after.sh)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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