Skip to content

Backup and Restore

Arthu Santiago edited this page Nov 30, 2026 · 1 revision

Backup and Restore

All the data of KeyAnyWhere is in the PostgreSQL database, but the database alone is not enough: the secrets in it are encrypted with KEY_CRIPTOGRAFIC, which lives outside it. This page lists what to back up, schedules a daily database dump and shows how to restore it.

Warning

A backup without the same KEY_CRIPTOGRAFIC cannot be read: titles, usernames, passwords, links, notes, category names and 2FA secrets stay encrypted forever. Keep a copy of the key away from the server and away from the backups (see Generating Security Keys).

What to back up

What Where How often
The database PostgreSQL keyanywhere Daily (step 1 below)
The environment file, with the keys /etc/keyanywhere/keyanywhere.env Once, and again whenever it changes. Keep it separate from the database backups.
Local configuration /var/www/keyanywhere/config/app_local.php Whenever it changes
Local plugins, if any /var/www/keyanywhere/plugins/ and config/plugins_local.php Whenever they change

The code itself doesn't need a backup: it is the Git tag you installed.

1. Schedule a daily database dump

This step creates a backup folder that only postgres can read and a daily job (cron) that writes a compressed dump at 3 a.m. and deletes dumps older than 14 days. Running as postgres avoids storing a database password anywhere.

sudo install -d -m 700 -o postgres -g postgres /var/backups/keyanywhere
sudo tee /etc/cron.d/keyanywhere-backup > /dev/null <<'EOF'
# Daily KeyAnyWhere database dump, kept for 14 days.
0 3 * * * postgres pg_dump --format=custom --file=/var/backups/keyanywhere/keyanywhere-$(date +\%F).dump keyanywhere && find /var/backups/keyanywhere -name '*.dump' -mtime +14 -delete
EOF

Run a first dump now, by hand, to check that it works:

sudo -u postgres pg_dump --format=custom --file=/var/backups/keyanywhere/keyanywhere-$(date +%F).dump keyanywhere
sudo ls -lh /var/backups/keyanywhere

Important

A backup on the same disk as the server doesn't survive the loss of the server. Copy /var/backups/keyanywhere to another machine or to object storage (for example with rsync or rclone).

2. Restore a dump

Restoring replaces the current data with the data of the dump. Stop Apache so nobody writes in the meantime, recreate the database, load the dump and start Apache again. Set the file you want to restore first:

KAW_DUMP=/var/backups/keyanywhere/keyanywhere-2026-11-29.dump
sudo systemctl stop apache2
sudo -u postgres dropdb --if-exists keyanywhere && sudo -u postgres createdb --owner=kaw --encoding=UTF8 keyanywhere
sudo -u postgres pg_restore --no-owner --role=kaw --dbname=keyanywhere "$KAW_DUMP"
sudo -u www-data kaw-cake cache clear_all && sudo systemctl start apache2

3. Check that it worked

Log in and open an entry: if its password shows up, the dump and the key match. On a new server, restore /etc/keyanywhere/keyanywhere.env (with the same KEY_CRIPTOGRAFIC) before testing.

Tip

Test a restore every now and then, on another machine. A backup that was never restored is only a hope.

Clone this wiki locally