Encrypted backups with types, scheduling and CLI restore

Backups are now AES-256 encrypted zips in three types (full, database
only, master key), catalogued in a new backups table, built by cron rather
than the web request, and kept under uploads/backups with retention in the
nightly job. The encryption key is one value per install held in config.php,
never in the database and never in the file name.

Restore is shared by the setup wizard and the new scripts/restore_cli.php,
which is the only path without an upload size limit. It verifies the key and
unpacks the archive before dropping anything, and dumps the current database
first so a failed import is rolled back.

A backup dumps, zips and encrypts for minutes without issuing a query, so on
a server with a short wait_timeout the connection is closed underneath it and
the UPDATE marking the backup complete is what fails - long after the archive
was written correctly. The connection is now held open for the job and
re-established before any write that follows long file work, including the
database phase of a restore. Retention recovers rows a dropped connection
left behind: still Running after six hours becomes Complete if the archive is
on disk, Failed if it is not.

cron.php's own failure path is hardened to match. It recorded job failures
through the same connection the failing job had just killed, so an uncaught
exception ended the dispatch and no trace of the original error survived.
Failures now also echo to stdout, so cron mails something useful when the
database is unreachable.

Security: the setup wizard's restore step is now closed on any install that
has users, whatever config.php says. $config_enable_setup defaulted to
enabled when the flag was absent, and the flag is only written at the end of
a successful install, so an install abandoned partway left an unauthenticated
endpoint that would drop every table, import an attacker-supplied archive,
and overwrite uploads/ including the .htaccess that stops PHP running there.
Affects 26.07 and earlier. Restoring over a live install is now CLI only.
This commit is contained in:
johnnyq
2026-07-31 16:18:20 -04:00
parent 6a5cf6704a
commit ae468d6cee
18 changed files with 2313 additions and 546 deletions

30
db.sql
View File

@@ -351,6 +351,31 @@ CREATE TABLE `auth_logs` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `backups`
--
DROP TABLE IF EXISTS `backups`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8mb4 */;
CREATE TABLE `backups` (
`backup_id` int(11) NOT NULL AUTO_INCREMENT,
`backup_type` varchar(20) NOT NULL DEFAULT 'full',
`backup_file_name` varchar(255) NOT NULL,
`backup_size` bigint(20) NOT NULL DEFAULT 0,
`backup_sha256` varchar(64) DEFAULT NULL,
`backup_status` varchar(20) NOT NULL DEFAULT 'Pending',
`backup_error` text DEFAULT NULL,
`backup_source` varchar(20) NOT NULL DEFAULT 'Manual',
`backup_created_by` varchar(200) DEFAULT NULL,
`backup_created_at` datetime NOT NULL DEFAULT current_timestamp(),
`backup_completed_at` datetime DEFAULT NULL,
`backup_downloaded_at` datetime DEFAULT NULL,
PRIMARY KEY (`backup_id`),
KEY `backup_status_created` (`backup_status`,`backup_created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `budget`
--
@@ -2278,6 +2303,9 @@ CREATE TABLE `settings` (
`config_ticket_ordering` tinyint(1) NOT NULL DEFAULT 0,
`config_ticket_moving_columns` tinyint(1) NOT NULL DEFAULT 1,
`config_cron_last_dispatch_at` datetime DEFAULT NULL,
`config_backup_retention_days` int(11) NOT NULL DEFAULT 30,
`config_backup_retention_count` int(11) NOT NULL DEFAULT 5,
`config_backup_cron_type` varchar(20) NOT NULL DEFAULT 'full',
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
@@ -3122,4 +3150,4 @@ CREATE TABLE `vendors` (
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2026-07-31 14:15:12
-- Dump completed on 2026-07-31 16:18:11