← /blog

Backing up a Laravel app to a home NAS

I recently set out to back up a Laravel project in production more often. The VPS already took a weekly backup, but for an important project that isn’t enough. To avoid paying extra for daily backups, I figured I could use the NAS I have at home, and got to work.

Here’s how it turned out and, above all, where I tripped up, because that’s where I learned the most.

The idea: the NAS fetches the backups

The first thing I decided was the direction of the connection. The server could send the backups to the NAS, but that means storing credentials for my home network on the server. If someone compromises the server, they can use them to delete the copies too.

So I did it the other way around: the NAS opens the connection and downloads. The server knows nothing about the NAS, and the user the NAS logs in as can only read.

The nightly flow:

Time (local, UTC−3) What happens
02:00 backup:clean deletes server backups older than 2 days
02:30 backup:run --only-db dumps the database into an encrypted zip
04:00 The NAS downloads the zips and the files users uploaded
04:00 backup:monitor sends a mail if the newest backup is older than a day

The dump: spatie/laravel-backup

For the database I used spatie/laravel-backup. It backs up only the database (--only-db), compresses the dump with gzip and stores it in an AES-256 encrypted zip:

// config/backup.php
'database_dump_compressor' => GzipCompressor::class,
'password' => env('BACKUP_ARCHIVE_PASSWORD'),
'encryption' => 'default', // AES-256 when a password is set

And the schedule, in UTC:

$schedule->command('backup:clean')->dailyAt('05:00')->withoutOverlapping()->onOneServer();
$schedule->command('backup:run --only-db')->dailyAt('05:30')->withoutOverlapping()->onOneServer();
$schedule->command('backup:monitor')->dailyAt('07:00')->onOneServer();

The server keeps only 2 days of copies, because its disk is small and the long history lives on the NAS.

Uploaded files don’t go inside the zip, on purpose. rsync copies only what the NAS doesn’t have yet, so pulling the upload folders directly moves each file once, instead of packing it every night.

Pitfall #1: an invalid mail address keeps the app from booting. During development I found that spatie/laravel-backup v10 validates the notification recipient while the app boots. If BACKUP_NOTIFICATION_EMAIL is empty or has a typo, it isn’t just the backup that fails: every request and every artisan command fails. My config uses a fallback:

'to' => env('BACKUP_NOTIFICATION_EMAIL') ?: env('MAIL_FROM_ADDRESS'),

Still, the fallback won’t save you from a typo, so make sure BACKUP_NOTIFICATION_EMAIL holds a single valid address before you deploy.

Important tip

Store the zip password outside the server. Without it, no backup can be opened. And if you lose the server, you lose its environment variables with it.

The read-only user

The NAS (a UGREEN running UGOS) only supports password login over SSH. So I created a dedicated user with password login, while every other account keeps its own setting.

Pitfall #2: --disabled-login breaks ForceCommand. The usual recipe is adduser --disabled-login, but that gives the user the /usr/sbin/nologin shell. sshd runs the ForceCommand through the user’s shell, so every login answers “This account is currently not available”. It needs a real shell:

adduser --disabled-login --shell /bin/sh --gecos "" backup-user

Why not rrsync

The standard tool to restrict an SSH user to read-only rsync is rrsync. I set it up, tested it from the NAS terminal and it worked. From the UGOS task: “connection error”, with no further detail.

Pitfall #3: UGOS always sends -s. To see what the NAS was asking for, I wrapped the forced command in a script that logs SSH_ORIGINAL_COMMAND:

logger -t backup-pull "cmd: ${SSH_ORIGINAL_COMMAND:-<none>}"

The log made it clear: UGOS always sends rsync --server --sender -s.... The -s option (protect-args) sends paths inside the protocol, and Ubuntu 24.04’s rrsync answers “option -s has been disabled on this server”. UGOS turns that into a bare “connection error”.

Important tip

Don’t test the connection from a Mac. Recent macOS versions ship openrsync instead of the usual rsync, and it builds a different command on the server side: rrsync rejects it with invalid rsync-command syntax, even when the server is set up correctly. Test with the real rsync, from the NAS terminal or from the server itself.

The alternative: an rsync daemon per login

Instead of rrsync, every SSH login of the user starts a single-use rsync daemon that serves one read-only module:

[app]
    path = /path/to/storage/app
    read only = yes
    use chroot = no
    munge symlinks = yes
    exclude = backup-temp/ temp/
  • read only = yes refuses any upload or delete.
  • With use chroot = no, rsync strips leading slashes and .., so asking for app/../ returns the same module.
  • munge symlinks = yes keeps symlinks from escaping the module. Without it, a compromised app could create private/x -> /etc/shadow and the NAS would download that host file.

The ForceCommand script runs the daemon as www-data, never as root. Uploaded files have private permissions that backup-user can’t read; www-data is the same uid that writes them inside the container:

#!/bin/sh
logger -t backup-pull "cmd: ${SSH_ORIGINAL_COMMAND:-<none>}"
exec sudo -u www-data /usr/bin/rsync --server --daemon --config=/etc/app-rsyncd.conf .

And sudoers allows exactly that command, nothing else.

In sshd_config, the Match block goes at the end of the file, because a Match swallows every line after it:

Match User backup-user
    PasswordAuthentication yes
    ForceCommand /usr/local/bin/backup-pull
    PermitTTY no
    AllowTcpForwarding no
    AllowAgentForwarding no
    X11Forwarding no
    PermitTunnel no

Pitfall #4: on Ubuntu 24.04, reload isn’t enough. A new ForceCommand didn’t reach new logins until I ran systemctl restart ssh.

Coolify and permissions

The app runs on Coolify, which leaves /data/coolify and /data/coolify/applications at drwx------. www-data can’t reach the storage folder. The fix is a traverse-only ACL (x), which lets it walk through those folders without listing them:

setfacl -m u:www-data:x /data/coolify /data/coolify/applications
sudo -u www-data ls /data/coolify/applications   # must still say "Permission denied"

Important tip

If a Coolify update resets those permissions, the ACL is gone and the NAS task starts failing. The fix is to run the setfacl again.

fail2ban that bans nobody

With a user that accepts passwords, fail2ban is a must.

Pitfall #5: on Ubuntu 24.04, fail2ban watches the wrong unit. Its default reads the journal of sshd.service, but on Ubuntu 24.04 the unit is called ssh.service. Freshly installed, it sees no failed attempts and bans nobody, without any error. Point it at the right unit:

# /etc/fail2ban/jail.d/sshd-ubuntu.local
[sshd]
backend = systemd
journalmatch = _SYSTEMD_UNIT=ssh.service
fail2ban-client status sshd | grep "Journal matches"   # must name ssh.service

The NAS task

In UGOS, the task is Sync & Backup → Back up server with rsync, transmission encryption on (port 22) and the module as the path. The key decision is the mode:

  • Mirror would delete on the NAS everything the server deletes, that is, every dump older than 2 days.
  • Incremental doesn’t delete on the NAS what the server deletes. Since every zip has a unique name and every upload is written once, nothing gets overwritten and the NAS keeps the whole history.

Important tip

The NAS must be on local time (the one in the table), so 04:00 falls after the 02:30 dump. If you leave it on UTC, the task runs before the dump.

Testing the restore

It’s important to run a manual restore to make sure the whole chain works. I took the production zip from the NAS, opened it with the password and loaded the dump into a throwaway PostgreSQL:

gunzip -k dump.sql.gz   # the dump comes compressed inside the zip
docker run -d --name restore -e POSTGRES_PASSWORD=restore -p 55432:5432 postgres:17-alpine
psql -h 127.0.0.1 -p 55432 -U postgres -c 'CREATE DATABASE app_restore'
psql -h 127.0.0.1 -p 55432 -U postgres -d app_restore -v ON_ERROR_STOP=1 -q -f dump.sql

That rehearsal taught me something: the dump assigns owners. Every table gets OWNER TO the production user. If that role doesn’t exist, ON_ERROR_STOP stops at the first table. The dump itself tells you which role to create:

grep -m1 -o 'OWNER TO [^;]*' dump.sql

With the role in place, the dump loaded without errors, and the main tables had the expected data. With the restore working, it’s safe to say the chain is complete, and that way I save a few dollars a month.

What’s still missing

There’s a gap this design doesn’t cover: if the Laravel scheduler stops, nobody sends a mail. backup:monitor runs on that same scheduler. The only sign is that new zips stop showing up on the NAS. Closing it takes an external dead man’s switch alert, and that’s for another post.