Part 5 of Laravel in Production.
A backup you have never restored is a hope, not a backup. The failure mode is common and quiet: the job runs, files pile up with the right dates, and the owner relaxes. Then the disk fails and the restore starts. The archive is missing the uploaded files. Or the database dump is a few kilobytes, because the export started erroring months ago and nobody opened a file to check. Everything looked backed up until the one hour when it mattered.
This laravel backup tutorial builds the whole routine in one sitting: what a complete backup holds, the commands that make it, a schedule that matches your site, storage in two places, encryption, a restore test, and a short monthly checklist that keeps the routine honest.
What a complete backup actually holds
Four things. Miss one and the restore has a hole in it.
The code. Your application files, the Blade views, the routes, the config. If your code sits in Git, the committed code is covered, and that is all Git covers.
The database. Every record the site has collected: users, orders, appointments, settings. This is the part that changes every day and cannot be rebuilt from anything.
The uploaded files. Photos, documents, invoices, and attachments that users uploaded, usually under storage/app/public or a public uploads folder. Git does not hold them, because they were never code, and for many small sites they are the largest part of the backup and the part most often left out.
The .env file and its keys. The database password, the mail credentials, and the APP_KEY live in .env, and .env stays out of Git on purpose, which is exactly why backup routines forget it. Data the app encrypted with the original APP_KEY cannot be read with a new key, so a backup without .env can restore a database you cannot fully use.
What you do not need to copy: the vendor folder and node_modules. Composer and npm rebuild both from the lock files, and leaving them out keeps the archive small.
Export the database on a schedule
The plain way is mysqldump with a dated file name, so every copy says when it was made:
mysqldump --single-transaction -u site_user -p site_db | gzip > /home/backup/db-$(date +%F).sql.gz
The --single-transaction flag snapshots InnoDB tables consistently without locking the site. The date in the file name makes the monthly checklist below possible: you can see at a glance which nights produced a file.
The Laravel way is the spatie/laravel-backup package: one command backs up the files and the database together, zipped, following its config file:
php artisan backup:run
Either route ends in the same place: a dated archive in a backup folder, made without you. Put it on the scheduler, or in cron:
15 2 * * * cd /var/www/site && /usr/bin/php artisan backup:run --only-db >> /dev/null 2>&1
That line runs the database backup at 2:15 every morning. A plain shell script around the mysqldump line does the same job on the manual route. Pick one route and write down which, because the checklist below needs a single place to look.
Back up the uploaded files
The uploads change more slowly than the database on most sites, so a weekly archive is usually enough:
tar -czf /home/backup/uploads-$(date +%F).tar.gz -C /var/www/site storage/app/public
Run it after the weekly database export, or after a heavy upload day. Code files need a fresh copy only when the code changes, which for a live site means at deployment time, so add that copy to your deploy steps.
Set the schedule by how fast the data changes
The rule is one question: how much work can this site afford to lose? A brochure site that changes once a month loses little with a weekly backup of everything. A site taking bookings or orders every day needs the database backed up daily at least, and every 12 hours costs nothing extra once the command is written. Match the uploads to how often new files arrive, not to the database schedule.
Keep the last 7 daily database copies and the last 4 weekly uploads archives, and delete anything older than 90 days in either place. A folder that grows forever gets ignored, and an ignored folder is where failing exports hide.
Keep the second copy off the server
A backup stored on the same server is lost in the same accident as the site. Disk failure, a suspended account, a wrong path in a cleanup script: the site and its only backup go together. So the routine copies every archive to a second place the moment it is made, from the same script or cron run, so a person forgetting is not part of the design:
rsync -avz /home/backup/ user@second-server:/backups/site/
The second place can be a second server, a home computer that is on at night, or cloud storage. The type matters less than the rule: a different machine, a different account, and the copy lands there on its own.
Encrypt the copies and control who can open them
A backup is the whole site in one file: the full database and, inside the .env copy, the keys to it. Left unencrypted, it is a copy of your user list waiting for the wrong reader. Encrypt before the file leaves the server. With GPG, one command does it:
gpg --symmetric --cipher-algo AES256 uploads-2026-12-01.tar.gz
Keep the passphrase in a password manager, not in a note on the server, because a passphrase stored next to the archive protects nothing.
Two access rules finish this part. The off server folder opens for one account, yours. And the backup folder never sits inside the public web folder: a predictable file name under a public path is a download link to your entire database.
Test the restore, on a copy you can break
This is the step everyone skips, and the only one that proves the rest. Do it once when you set the routine up, onto a local copy of the site or a throwaway subdomain with its own empty database. The steps are the same:
- Take the newest archive set: the database dump, the uploads, and the .env file
- Create a fresh empty database and import the dump:
gunzip < db-2026-12-01.sql.gz | mysql -u root -p restore_test - Copy the uploads back into storage/app/public and put the backed up .env in place, with the database name changed to the test database and nothing else touched
- Open the test copy, log in as a test user, and open three pages: the home page, one record page, and one page with an uploaded image on it
- Write down how long the whole test took
Step 3 holds the trap: do not run key:generate on the restored copy. The original APP_KEY from the backed up .env is what lets the app read its own encrypted data. A new key makes the restored site run and quietly breaks everything that was encrypted.
If the test copy opens on an error page or a 404 instead of your site, the fault is usually the document root and server paths, and my earlier post on a Laravel site returning 404 on every route after deployment covers those checks: https://dev.to/mehedirafi/laravel-returns-404-on-every-route-after-deployment-here-is-the-fix-34p3
Repeat the full restore test every few months, and once after any big change to the site. The time from step 5 is your real recovery time, better learned on a quiet afternoon than during an outage.
The monthly checklist, 20 minutes
Once a month, run these five checks. Twenty minutes in total, and they catch every quiet failure named at the top of this post.
- Open the backup folder on the server and the off server copy. The newest database file should carry last night's date, or this week's for a weekly site. Two minutes.
- Compare file sizes with last month. A dump that suddenly shrinks, or sits at a few kilobytes, means the export is failing quietly. Three minutes.
- Download one archive and open it. Confirm the uploads folder and the .env copy are inside. Five minutes.
- Delete the copies older than 90 days, in both places. Five minutes.
- Check that the off server folder still opens only for your account, and that the encryption passphrase is still in the password manager. Five minutes.
What this routine does not cover
Three things sit outside it, stated plainly: mail stored on the server, server settings such as the PHP version, the extensions, and the web server config, and DNS records. Cover all three with one plain text note in the off server folder: the PHP version and extension list, the cron lines, and the DNS records from the registrar panel. Half an hour, once, and a server move stops being archaeology.
That is the whole routine: the right four things, on a schedule, copied off the server, encrypted, and proven by a restore you have actually run.
I run backups, updates, and small fixes for client sites every month through Upwork. Care plans start at $40 a month. If you would rather hand this routine over, send me your site link and I will quote the monthly cost: https://www.upwork.com/freelancers/~0118ab607e0b4c599b
I am Mehedi Mansur Rafi, a freelance Laravel developer (B.Sc. in Computer Science & Engineering). I build hospital, clinic, and small business websites, and I look after live sites on monthly care plans. For project work, my Fiverr profile is https://www.fiverr.com/mehedi__rafi and my site is https://mehedimansurrafi.online
Комментарии (0)
Пока нет комментариев — будьте первым.