Skip to content

Instantly share code, notes, and snippets.

@wesort
Last active May 6, 2025 12:12
Show Gist options
  • Save wesort/dfbf15e892e8df1708cdd0c0ee73c74a to your computer and use it in GitHub Desktop.
Save wesort/dfbf15e892e8df1708cdd0c0ee73c74a to your computer and use it in GitHub Desktop.
Automatic daily backup from production site to GitHub
# Shell script to automatically backup current branch to GitHub
# NB: artisan commands apply to Laravel projects
git add .
git commit -m "Automatic daily backup via cronjob"
git push
php artisan optimize:clear
php artisan optimize
# Instructions
# 1. SSH into the server
# 2.1 Create this file at root of the repo to backup: $ touch autobackup.sh
# 2.2 Add the commands shown in this file: $ vim autobackup.sh
# 3. Set permission to execute: $ chmod +x autobackup.sh
# 4. Ensure the current branch (likely `main`) is set to track upstream: $ git push -u origin main
# 5.1 Test that the command runs manually: $ sh autobackup.sh
# 5.2 Check Github.com for the commit of the autobackup.sh file
# 6.1 Setup cronjob to run the file on Laravel Forge or via SSH with: $ crontab -e
# 6.2 Create or edit a new line replacing "example.com" accordingly
# 6.3 Example of nightly at 3am: 0 3 * * * cd ~/example.com && ./autobackup.sh > /dev/null
# 6.4 Crontab schedule generator: https://crontab.guru/#0_3_*_*_*
@wesort
Copy link
Author

wesort commented Aug 16, 2019

Example implementation using Scheduler in Laravel Forge

cd ~/example.com/ && ./autobackup.sh > /dev/null

cronjob-example-01

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment