> Manifests, each with 'bytes' (all archives) added. */ public static function all() { $out = array(); foreach ( glob( Rollbakr_Storage::dir() . '*/manifest.json' ) ?: array() as $file ) { $id = basename( dirname( $file ) ); if ( ! Rollbakr_Storage::is_valid_id( $id ) ) { continue; } $m = self::get( $id ); if ( $m ) { $out[] = $m; } } // Newest first. 'created' is a second, so two started in the same second are ordered by when they // finished, then by id, so that the order is always the same and the later one is the newer one. usort( $out, static fn( $a, $b ) => array( $b['created'], (int) ( $b['finished'] ?? 0 ), $b['id'] ) <=> array( $a['created'], (int) ( $a['finished'] ?? 0 ), $a['id'] ) ); return $out; } /** * One backup's manifest, or null. * * @param string $id Id. * @return array|null */ public static function get( $id ) { if ( ! Rollbakr_Storage::is_valid_id( $id ) ) { return null; } $file = self::path( $id ) . 'manifest.json'; if ( ! is_file( $file ) ) { return null; } $m = json_decode( (string) file_get_contents( $file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( ! is_array( $m ) || ( $m['id'] ?? '' ) !== $id || ! is_array( $m['archives'] ?? null ) ) { return null; } $m['bytes'] = (int) array_sum( array_column( $m['archives'], 'size' ) ); return $m; } /** * Folder of a backup, with a trailing slash. * * @param string $id Id. * @return string */ public static function path( $id ) { return Rollbakr_Storage::dir() . $id . '/'; } /** * Path of one of a backup's archives, or '' when the backup has no archive of that name. * * @param string $id Id. * @param string $name Archive name as listed in the manifest (database.sql.gz, files-1.tar.gz ...). * @return string */ public static function archive( $id, $name ) { $m = self::get( $id ); if ( ! $m || ! isset( $m['archives'][ $name ] ) || ! preg_match( '/^[a-z0-9-]+\.(sql|tar)\.gz$/', (string) $name ) ) { return ''; } $path = self::path( $id ) . $name; return is_file( $path ) ? $path : ''; } /** * Delete a backup. * * @param string $id Id. * @return bool */ public static function delete( $id ) { $m = self::get( $id ); if ( ! $m ) { return false; } // Its record (record.json, log.txt) stays on the Logs screen as .deleted: the backup is // gone, but what happened when it was made can still be read. Rollbakr_Logs::keep_deleted( $id ); if ( ! Rollbakr_Storage::remove_tree( self::path( $id ) ) ) { Rollbakr_Logs::forget_deleted( $id ); return false; } /** * A backup was removed from the server (by hand, or by prune()). An add-on that keeps a list * of where backups are can bring it up to date. * * @param string $id Backup id. * @param array $manifest Its manifest, as it was. */ do_action( 'rollbakr_backup_deleted', $id, $m ); return true; } /** * Remove old backups: manual ones beyond the 'keep' setting, and all but the newest pre-restore one. * * With "Download to your computer", a backup made by hand goes beyond the number only once the * administrator has marked it "I saved it on my computer" in the list (see saved()): it may be * the only copy, and a download that went out in full (downloads.json) says nothing about * whether the file arrived (the browser may have given up while the server's buffer still * drained). A scheduled or uploaded one beyond the number goes by the number, but stays a day * longer (DOWNLOAD_GRACE) after a download in full. Pre-restore ones, and every backup with * "Keep on the server", go by the number as before. * * @return string[] Ids removed. */ public static function prune() { $removed = array(); foreach ( self::walk()['drop'] as $id ) { if ( self::delete( $id ) ) { $removed[] = $id; } } return $removed; } /** * Backups beyond the number to keep that prune() leaves alone because they carry no "I saved it * on my computer" mark, or were downloaded less than a day ago (always empty with "Keep on the * server"). * * @return string[] Ids, newest first. */ public static function spared() { return array_keys( self::walk()['spared'] ); } /** * Why each spared backup is spared: 'not_saved' (by hand, no mark) or 'recent' (another kind, * downloaded within the grace). * * @return array Id => reason, newest first. */ public static function spared_why() { return self::walk()['spared']; } /** * Seconds after the last complete download during which a backup beyond the number is still kept. * * @return int */ public static function download_grace() { /** * How long a backup is kept beyond the number to keep after it was downloaded in full (the * server sent the last byte; whether the computer kept the file is not known). Default: a day. * * @param int $seconds Seconds; 0 keeps nothing on that account. */ return max( 0, (int) apply_filters( 'rollbakr_download_grace', self::DOWNLOAD_GRACE ) ); } /** * What prune() would do: which backups go, and which are spared (id => why: 'not_saved' or * 'recent'). * * @return array{drop:string[],spared:array} */ private static function walk() { $keep = (int) Rollbakr_Settings::get( 'keep' ); $local = Rollbakr_Storage::is_local(); $grace = self::download_grace(); $manual = 0; $pre = 0; $out = array( 'drop' => array(), 'spared' => array(), ); /** * Backups that must not be pruned right now (the one a restore is reading, for example). * * @param string[] $ids Backup ids. */ $protected = (array) apply_filters( 'rollbakr_prune_protected', array() ); foreach ( self::all() as $m ) { $kind = (string) ( $m['kind'] ?? 'manual' ); // A protected backup counts like any other but is never the one removed. if ( 'pre_restore' === $kind ) { ++$pre; $drop = $pre > 1; } else { ++$manual; $drop = $manual > $keep; if ( $drop && $local ) { if ( 'manual' === $kind ) { // By hand: only one marked "I saved it on my computer" goes, however old its // download notes are; without the mark it may be the only copy. if ( self::saved( $m['id'] ) <= 0 ) { $out['spared'][ $m['id'] ] = 'not_saved'; $drop = false; } } elseif ( $grace > 0 && self::last_download( $m['id'] ) >= time() - $grace ) { $out['spared'][ $m['id'] ] = 'recent'; $drop = false; } } } if ( $drop && ! in_array( $m['id'], $protected, true ) ) { $out['drop'][] = $m['id']; } } return $out; } /** * How many manual backups there are (every kind but pre-restore: by hand, scheduled, uploaded). * * @return int */ public static function count_manual() { return count( array_filter( self::all(), static fn( $m ) => 'pre_restore' !== ( $m['kind'] ?? 'manual' ) ) ); } /** * How a backup came to be, for the screen: manual, automatic before a restore, scheduled, uploaded. * * @param string $kind Manifest 'kind'. * @return string */ public static function kind_label( $kind ) { $kind = (string) $kind; switch ( $kind ) { case 'pre_restore': $label = __( 'automatic, before a restore', 'rollbakr' ); break; case 'uploaded': $label = __( 'uploaded from a computer', 'rollbakr' ); break; case 'scheduled': $label = __( 'scheduled (automatic)', 'rollbakr' ); break; default: $label = __( 'manual', 'rollbakr' ); } /** * The words for how a backup came to be (an add-on may name its own kinds). * * @param string $label The words. * @param string $kind The manifest's 'kind'. */ return (string) apply_filters( 'rollbakr_backup_kind_label', $label, $kind ); } /** Bytes a download (one .tar holding the archives) may hold before the archives are split over several. */ const BUNDLE_MAX = 2147483648; /** * How big one download may be (see bundles()). Filter rollbakr_bundle_max; never below 1 MB. * * @return int Bytes. */ public static function bundle_max() { /** * How many bytes one download (a .tar holding the backup's archives) may hold before the * archives are split over several downloads. Default 2 GB. * * @param int $max Bytes. */ return max( MB_IN_BYTES, (int) apply_filters( 'rollbakr_bundle_max', self::BUNDLE_MAX ) ); } /** * The downloads of a backup: its archives (the database, then files-1, files-2 …) packed into * one plain .tar, or into several when together they would pass bundle_max() (an archive that is * bigger than that on its own gets a download of its own). The entries carry the names the * separate downloads had (Rollbakr_Download::filename()), so a .tar unpacked by hand still restores. * * @param array $m Manifest. * @return array * Keyed by the download's name: 'bundle' when there is one, else 'bundle-1', 'bundle-2' … * size is the .tar's length in bytes. */ public static function bundles( array $m ) { $names = array_keys( (array) ( $m['archives'] ?? array() ) ); usort( $names, static function ( $a, $b ) { $na = 'database.sql.gz' === $a ? 0 : (int) preg_replace( '/\D/', '', (string) $a ); $nb = 'database.sql.gz' === $b ? 0 : (int) preg_replace( '/\D/', '', (string) $b ); return $na <=> $nb ?: strcmp( (string) $a, (string) $b ); } ); $max = self::bundle_max(); $groups = array(); $cur = array(); $bytes = 0; foreach ( $names as $name ) { $size = (int) ( $m['archives'][ $name ]['size'] ?? 0 ); $entry = Rollbakr_Download::entry_length( Rollbakr_Download::filename( (string) $m['id'], (string) $name ), $size ); if ( $cur && $bytes + $entry > $max ) { $groups[] = array( $cur, $bytes ); $cur = array(); $bytes = 0; } $cur[] = (string) $name; $bytes += $entry; } if ( $cur ) { $groups[] = array( $cur, $bytes ); } $count = count( $groups ); $out = array(); foreach ( $groups as $i => list( $archives, $len ) ) { $key = 1 === $count ? 'bundle' : 'bundle-' . ( $i + 1 ); $out[ $key ] = array( 'archives' => $archives, 'size' => $len + 2 * Rollbakr_Tar::BLOCK, 'index' => $i + 1, 'count' => $count, ); } return $out; } /** * Which of a backup's archives have been downloaded in full, and when: from downloads.json in * its folder, written by Rollbakr_Download when the last byte of an archive went out. * * @param string $id Id. * @return array Archive name => Unix time of the last complete download. */ public static function downloads( $id ) { if ( ! Rollbakr_Storage::is_valid_id( $id ) ) { return array(); } $file = self::path( $id ) . 'downloads.json'; if ( ! is_file( $file ) ) { return array(); } $data = json_decode( (string) file_get_contents( $file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions $out = array(); foreach ( is_array( $data ) ? $data : array() as $name => $time ) { if ( is_string( $name ) && is_numeric( $time ) && (int) $time > 0 ) { $out[ $name ] = (int) $time; } } return $out; } /** * When a backup was last downloaded in full (the latest note of any of its archives), 0 = never. * * @param string $id Id. * @return int Unix time. */ public static function last_download( $id ) { $done = self::downloads( $id ); return $done ? (int) max( $done ) : 0; } /** * Whether every archive of a backup has been downloaded in full at least once. * * @param string $id Id. * @return bool */ public static function fully_downloaded( $id ) { $m = self::get( $id ); if ( ! $m || ! $m['archives'] ) { return false; } $done = self::downloads( $id ); foreach ( array_keys( $m['archives'] ) as $name ) { if ( empty( $done[ $name ] ) ) { return false; } } return true; } /** * Note that an archive has been downloaded in full (now). Written to a temporary file and * renamed into place; a folder that cannot be written is left as it is (the download itself * has gone through, and nothing depends on the note). * * @param string $id Id. * @param string $name Archive name as listed in the manifest. * @return bool Whether the note was written. */ public static function mark_downloaded( $id, $name ) { $m = self::get( $id ); if ( ! $m || ! isset( $m['archives'][ $name ] ) ) { return false; } $done = self::downloads( $id ); $done[ $name ] = time(); return self::write_note( self::path( $id ) . 'downloads.json', $done ); } /** * When the administrator marked a backup "I saved it on my computer" (saved.json in its folder), * 0 = not marked. The mark is set by hand in the list, once the file is seen on the computer: it * is what lets prune() remove a backup made by hand beyond the number (see walk()). Pressing * "Download" sets nothing, and downloads.json does not count: the server cannot tell whether * the file arrived. * * @param string $id Id. * @return int Unix time of the mark. */ public static function saved( $id ) { if ( ! Rollbakr_Storage::is_valid_id( $id ) ) { return 0; } $file = self::path( $id ) . 'saved.json'; if ( ! is_file( $file ) ) { return 0; } $data = json_decode( (string) file_get_contents( $file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions return is_array( $data ) && is_numeric( $data['time'] ?? null ) ? max( 0, (int) $data['time'] ) : 0; } /** * Mark a backup "I saved it on my computer" (now, by the current user). Written like the * download notes: a temporary file renamed into place. * * @param string $id Id. * @return bool Whether the mark was written (false for an id that is no backup). */ public static function mark_saved( $id ) { if ( ! self::get( $id ) ) { return false; } return self::write_note( self::path( $id ) . 'saved.json', array( 'time' => time(), 'user' => get_current_user_id(), ) ); } /** * Take the "I saved it on my computer" mark off a backup (nothing to do when it has none). * * @param string $id Id. * @return bool Whether the backup now has no mark (false for an id that is no backup). */ public static function unmark_saved( $id ) { if ( ! self::get( $id ) ) { return false; } $file = self::path( $id ) . 'saved.json'; if ( is_file( $file ) ) { wp_delete_file( $file ); clearstatcache( true, $file ); } return ! is_file( $file ); } /** * Write a small JSON note into a backup's folder: to a temporary file, then renamed into place, * so that a reader never sees half of it. A folder that cannot be written leaves the note as * it was. * * @param string $file Path of the note. * @param array $data What to write. * @return bool Whether the note was written. */ private static function write_note( $file, array $data ) { $tmp = $file . '.' . strtolower( wp_generate_password( 6, false, false ) ) . '.tmp'; $json = wp_json_encode( $data, JSON_UNESCAPED_SLASHES ); if ( false === $json || false === @file_put_contents( $tmp, $json ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions return false; } if ( ! @rename( $tmp, $file ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions wp_delete_file( $tmp ); return false; } return true; } /** * Labels of the parts in a manifest, for the screen. * * @param array $m Manifest. * @return string */ public static function parts_label( array $m ) { $labels = Rollbakr_Files::labels(); $short = array( 'database' => __( 'Database', 'rollbakr' ), 'themes' => __( 'Themes', 'rollbakr' ), 'plugins' => __( 'Plugins', 'rollbakr' ), 'uploads' => __( 'Media', 'rollbakr' ), 'other' => __( 'Other files', 'rollbakr' ), ); $parts = (array) ( $m['parts'] ?? array() ); if ( count( $parts ) === count( Rollbakr_Files::PARTS ) ) { return __( 'Everything', 'rollbakr' ); } if ( array( 'database' ) === $parts ) { return __( 'Database only', 'rollbakr' ); } return implode( ', ', array_map( static fn( $p ) => $short[ $p ] ?? $labels[ $p ] ?? $p, $parts ) ); } }