state (saved to job.json): * parts what was chosen * db which table and row the dump has reached * list the folders still to walk (the file list itself is list.txt in the job folder) * files which line of list.txt, which byte of that file, and which archive part * finish the hashes being computed * summary one line per finished phase, for the screen * Archives are written in append mode; after every step their lengths are recorded, and a * step that starts over a longer file cuts it back (a step that died mid-way). * * @package ROLLBAKR */ defined( 'ABSPATH' ) || exit; /** * Runner for jobs of type 'backup'. */ class Rollbakr_Backup { const TYPE = 'backup'; /** Bytes read from a file at a time. */ const CHUNK = 1048576; /** Start a new files-N.tar.gz once the current one is this big (checked between files). */ const PART_SIZE = 1073741824; /** Files bigger than this cannot be stored in a ustar header and are skipped. */ const MAX_FILE = 8589934591; /** Free space that must be left over after the backup. */ const SPARE = 100 * MB_IN_BYTES; /** The ways a backup comes to be: by hand, by itself before a restore, on a schedule (an add-on). */ const KINDS = array( 'manual', 'pre_restore', 'scheduled' ); /** * Register the type. * * @return void */ public static function register() { Rollbakr_Job::register( self::TYPE, array( __CLASS__, 'run' ), array( __CLASS__, 'cleanup' ), array( // Closures: the types are registered before 'init', when translations must not be loaded yet. 'label' => static fn() => __( 'Backup', 'rollbakr' ), 'kind' => 'backup', 'steps' => array( 'prepare' => static fn() => __( 'Getting ready', 'rollbakr' ), 'database' => static fn() => __( 'Database', 'rollbakr' ), 'files' => static fn() => __( 'Files', 'rollbakr' ), 'finish' => static fn() => __( 'Finishing', 'rollbakr' ), ), ) ); } /** * Start a backup job. * * @param string[] $parts Parts (see Rollbakr_Files::PARTS). * @param string $kind 'manual', 'pre_restore' or 'scheduled' (anything else counts as manual). * @param array $extra What an add-on's form sent along (see Rollbakr_Rest::start_backup()): * kept in the job's args for the add-on's hooks, never shown or stored * in the manifest. * @return Rollbakr_Job|WP_Error */ public static function start( array $parts, $kind = 'manual', array $extra = array() ) { $parts = array_values( array_intersect( Rollbakr_Files::PARTS, array_map( 'strval', $parts ) ) ); if ( ! $parts ) { return new WP_Error( 'rollbakr_nothing', __( 'Choose at least one thing to back up.', 'rollbakr' ) ); } return Rollbakr_Job::start( self::TYPE, array( 'parts' => $parts, 'kind' => in_array( (string) $kind, self::KINDS, true ) ? (string) $kind : 'manual', // Where it goes, as set when it started: the progress screen shows the right ending. 'destination' => Rollbakr_Storage::is_local() ? 'local' : 'server', 'extra' => self::clean_extra( $extra ), ) ); } /** * Only short strings under short keys may come along as 'extra': at most 20 of them, keys of * letters, digits, '_' and '-' cut to 40 characters, values cut to 200 characters (characters, * not bytes: a Japanese note of 200 characters fits). * * @param mixed $extra What was given. * @return array */ public static function clean_extra( $extra ) { $out = array(); if ( ! is_array( $extra ) ) { return $out; } foreach ( $extra as $key => $value ) { if ( count( $out ) >= 20 ) { break; } if ( ! is_string( $key ) || ! preg_match( '/^[A-Za-z0-9_-]+$/', $key ) || ! is_scalar( $value ) ) { continue; } $value = is_bool( $value ) ? ( $value ? '1' : '' ) : (string) $value; $out[ self::cut( $key, 40 ) ] = self::cut( $value, 200 ); } return $out; } /** * The first $max characters of a string (bytes when mbstring is not there). * * @param string $text Text. * @param int $max Characters. * @return string */ private static function cut( $text, $max ) { if ( function_exists( 'mb_substr' ) ) { return mb_strlen( $text, 'UTF-8' ) > $max ? mb_substr( $text, 0, $max, 'UTF-8' ) : $text; } return strlen( $text ) > $max ? substr( $text, 0, $max ) : $text; } /** * One step. * * @param Rollbakr_Job $job Job. * @param float $deadline Stop before this time. * @return bool True when the backup is complete. */ public static function run( Rollbakr_Job $job, $deadline ) { switch ( $job->phase ) { case '': self::prepare( $job ); return false; case 'database': self::database( $job, $deadline ); return false; case 'list': self::list( $job, $deadline ); return false; case 'files': self::files( $job, $deadline ); return false; case 'finish': return self::finish( $job, $deadline ); default: throw new RuntimeException( esc_html( 'Unknown phase ' . $job->phase ) ); } } /** * Cleanup when the job is cancelled or fails. Cancelled: the folder goes. Failed: the archives go * (they are useless and large), the log stays, and the folder is renamed .failed. * * @param Rollbakr_Job $job Job. * @return void */ public static function cleanup( Rollbakr_Job $job ) { $dir = $job->dir(); foreach ( glob( $dir . '*.gz' ) ?: array() as $f ) { wp_delete_file( $f ); } foreach ( array( 'list.txt', 'manifest.json' ) as $f ) { if ( is_file( $dir . $f ) ) { wp_delete_file( $dir . $f ); } } // The log stays, in a folder named after how it ended (the Logs screen reads it). $suffix = 'failed' === $job->status ? '.failed' : '.cancelled'; $final = Rollbakr_Storage::dir() . $job->id . $suffix; if ( ! file_exists( $final ) && rename( rtrim( $dir, '/' ), $final ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions $job->state['dir'] = $job->id . $suffix; } } /** * Phase 1: check the space against what is known, write the dump header. * * The sizes come from the screen's cache when there is one; wp-content is not walked here (on a * big site that alone could outlive the request). The real check is made again once the file * list is complete, see list(). * * @param Rollbakr_Job $job Job. * @return void */ private static function prepare( Rollbakr_Job $job ) { global $wpdb; $parts = (array) ( $job->args['parts'] ?? array() ); $job->log( 'backup ' . $job->id . ': ' . implode( ', ', $parts ) ); $job->set_progress( 0, 0, __( 'Getting ready', 'rollbakr' ) ); $sizes = Rollbakr_Files::sizes_cached() ?: array( 'database' => Rollbakr_Files::database_size() ); $estimate = 0; foreach ( $parts as $p ) { $estimate += (int) ( $sizes[ $p ]['bytes'] ?? 0 ); } self::check_space( $job, $estimate ); $job->state = array( 'parts' => $parts, 'estimate' => $estimate, 'started' => time(), 'home' => home_url(), 'siteurl' => site_url(), 'prefix' => $wpdb->prefix, // Whether tables without the prefix are in (the setting as it is now, the same objects() reads). 'all' => (bool) Rollbakr_Settings::get( 'all_tables' ), 'times' => array( 'prepare' => time() ), 'summary' => array(), 'db' => array( 'objects' => array(), 'i' => 0, 'key' => '', 'after' => 0, // Set per table when it is opened: PHP_INT_MIN with an integer key, 0 (an offset) without. 'limit' => Rollbakr_Db_Export::ROWS, 'open' => false, 'cols' => array(), 'tables' => 0, 'rows' => 0, 'len' => 0, 'utc' => false, ), 'list' => array( 'queue' => array(), 'files' => 0, 'bytes' => 0, 'dirs' => 0, 'links' => 0, 'len' => 0, ), 'files' => array( 'lpos' => 0, 'cur' => null, 'offset' => 0, 'part' => 1, 'parts' => array(), 'rotate' => false, 'done' => 0, 'bytes' => 0, 'skipped' => 0, ), 'finish' => array( 'todo' => array(), 'name' => '', 'ctx' => '', 'offset' => 0, 'hashes' => array(), ), ); if ( in_array( 'database', $parts, true ) ) { $objects = array(); foreach ( Rollbakr_Db_Export::objects() as $name => $kind ) { $objects[] = array( $name, $kind ); } // Tables first, views after (a view may depend on a table). usort( $objects, static fn( $a, $b ) => strcmp( $a[1] . $a[0], $b[1] . $b[0] ) ); $job->state['db']['objects'] = $objects; $tz = Rollbakr_Db_Export::use_utc(); $job->state['db']['utc'] = false !== $tz; Rollbakr_Db_Export::restore_time_zone( $tz ); self::gz_append( $job->dir() . 'database.sql.gz', Rollbakr_Db_Export::header( $job->id, $job->state['db']['utc'] ) ); $job->state['db']['len'] = (int) filesize( $job->dir() . 'database.sql.gz' ); $job->log( count( $objects ) . ' tables and views' . ( $job->state['db']['utc'] ? '' : ' (time zone left as is)' ) ); self::enter( $job, 'database' ); } else { self::after_database( $job ); } } /** * Fail when the disk would not hold the backup plus some spare. * * @param Rollbakr_Job $job Job. * @param int $bytes What the backup is expected to take (uncompressed). * @return void */ private static function check_space( Rollbakr_Job $job, $bytes ) { $free = Rollbakr_Storage::free_space(); $job->log( sprintf( 'expected %s, free %s', size_format( $bytes ), null === $free ? 'unknown' : size_format( $free ) ) ); if ( null !== $free && $free < $bytes + self::SPARE ) { throw new RuntimeException( esc_html( sprintf( /* translators: 1: needed, 2: free */ __( 'Not enough free space: about %1$s is needed and %2$s is free.', 'rollbakr' ), size_format( $bytes + self::SPARE ), size_format( $free ) ) ) ); } } /** * Move to the next phase: close the summary of the one that ends, note the time, set the label. * * @param Rollbakr_Job $job Job. * @param string $phase Next phase. * @return void */ private static function enter( Rollbakr_Job $job, $phase ) { $now = time(); switch ( $job->phase ) { case '': $job->state['summary']['prepare'] = self::took( $job, 'prepare', $now ); break; case 'database': $db = $job->state['db']; $job->state['summary']['database'] = sprintf( /* translators: 1: tables, 2: rows, 3: size */ __( '%1$s tables · %2$s rows · %3$s', 'rollbakr' ), number_format_i18n( (int) $db['tables'] ), number_format_i18n( (int) $db['rows'] ), Rollbakr_Admin::size( (int) $db['len'] ) ) . ' · ' . self::took( $job, 'database', $now ); break; case 'list': case 'files': // The screen shows finding and writing the files as one step, so they are summed up // together, from the moment the list began (also when the list turned out empty). $f = $job->state['files']; $job->state['summary']['files'] = sprintf( /* translators: 1: files, 2: size */ __( '%1$s files · %2$s', 'rollbakr' ), number_format_i18n( (int) $f['done'] ), Rollbakr_Admin::size( (int) $f['bytes'] ) ) . ' · ' . self::took( $job, isset( $job->state['times']['list'] ) ? 'list' : 'files', $now ); break; } $job->state['times'][ $phase ] = $now; $job->phase = $phase; $labels = array( 'database' => __( 'Database', 'rollbakr' ), 'list' => __( 'Finding the files', 'rollbakr' ), 'files' => __( 'Writing the files', 'rollbakr' ), 'finish' => __( 'Finishing', 'rollbakr' ), ); $job->set_progress( 0, 0, $labels[ $phase ] ?? '' ); } /** * "41 s": how long a phase took. * * @param Rollbakr_Job $job Job. * @param string $phase Phase. * @param int $now Now. * @return string */ private static function took( Rollbakr_Job $job, $phase, $now ) { return Rollbakr_Admin::duration( $now - (int) ( $job->state['times'][ $phase ] ?? $now ) ); } /** * Move on from the dump to the files (or straight to the finish). * * @param Rollbakr_Job $job Job. * @return void */ private static function after_database( Rollbakr_Job $job ) { $roots = Rollbakr_Files::roots( $job->state['parts'] ); if ( $roots ) { self::enter( $job, 'list' ); $job->state['list']['queue'] = $roots; } else { self::enter( $job, 'finish' ); } } /** * Phase 2: the dump. * * @param Rollbakr_Job $job Job. * @param float $deadline Deadline. * @return void */ private static function database( Rollbakr_Job $job, $deadline ) { $db = &$job->state['db']; $path = $job->dir() . 'database.sql.gz'; $tz = false; if ( $db['utc'] ) { $tz = Rollbakr_Db_Export::use_utc(); if ( false === $tz ) { throw new RuntimeException( 'Cannot set the connection time zone' ); } } Rollbakr_Tar::truncate( $path, (int) $db['len'] ); $gz = gzopen( $path, 'ab6' ); if ( ! $gz ) { Rollbakr_Db_Export::restore_time_zone( $tz ); throw new RuntimeException( 'Cannot write database.sql.gz' ); } $write = static function ( $sql ) use ( $gz ) { if ( '' !== $sql && gzwrite( $gz, $sql ) !== strlen( $sql ) ) { throw new RuntimeException( 'Cannot write database.sql.gz (disk full?)' ); } }; try { $count = count( $db['objects'] ); while ( microtime( true ) < $deadline ) { if ( $db['i'] >= $count ) { $write( Rollbakr_Db_Export::footer() ); $job->log( sprintf( 'database: %d tables, %d rows', $db['tables'], $db['rows'] ) ); gzclose( $gz ); $gz = null; clearstatcache( true, $path ); $db['len'] = (int) filesize( $path ); self::after_database( $job ); break; } list( $name, $kind ) = $db['objects'][ $db['i'] ]; $job->set_progress( $db['i'], $count, $name, array( 'unit' => 'tables' ) ); if ( ! $db['open'] ) { $write( Rollbakr_Db_Export::create( $name, $kind ) ); if ( 'view' === $kind ) { ++$db['i']; continue; } $db['cols'] = Rollbakr_Db_Export::columns( $name ); $db['key'] = Rollbakr_Db_Export::int_key( $name ); $db['limit'] = Rollbakr_Db_Export::limit_for( $name ); // With a key: every value, 0 and negative ones included, is > PHP_INT_MIN. Without one // (a composite key, as term_relationships has, or none): an offset, from the first row. $db['after'] = '' !== $db['key'] ? PHP_INT_MIN : 0; $db['open'] = true; ++$db['tables']; } $rows = Rollbakr_Db_Export::rows( $name, $db['key'], $db['after'], (int) $db['limit'] ); if ( ! $rows ) { $db['open'] = false; ++$db['i']; continue; } $write( Rollbakr_Db_Export::insert( $name, $db['cols'], $rows ) ); $db['rows'] += count( $rows ); $db['after'] = '' !== $db['key'] ? (int) end( $rows )[ $db['key'] ] : $db['after'] + count( $rows ); unset( $rows ); } } finally { if ( $gz ) { gzclose( $gz ); clearstatcache( true, $path ); $db['len'] = (int) filesize( $path ); } // The rest of this request (other plugins, a cron that goes on) sees NOW() as before. Rollbakr_Db_Export::restore_time_zone( $tz ); } } /** * Phase 3: walk the folders and write list.txt (one entry per line: type, size, mtime, mode, path). * * @param Rollbakr_Job $job Job. * @param float $deadline Deadline. * @return void */ private static function list( Rollbakr_Job $job, $deadline ) { $list = &$job->state['list']; $content = Rollbakr_Files::content_dir() . '/'; $excluded = Rollbakr_Files::excluded(); $path = $job->dir() . 'list.txt'; // A step that died after writing some lines but before saving: those lines would be written again. Rollbakr_Tar::truncate( $path, (int) $list['len'] ); $fh = fopen( $path, 'ab' ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( ! $fh ) { throw new RuntimeException( 'Cannot write list.txt' ); } $line = static function ( $type, $rel, $abs ) use ( $fh ) { $st = @stat( $abs ); // phpcs:ignore WordPress.PHP.NoSilencedErrors if ( ! $st ) { return 0; } $size = 'f' === $type ? (int) $st['size'] : 0; fwrite( $fh, $type . "\t" . $size . "\t" . (int) $st['mtime'] . "\t" . ( $st['mode'] & 0777 ) . "\t" . $rel . "\n" ); // phpcs:ignore WordPress.WP.AlternativeFunctions return $size; }; try { // The queue holds the roots (followed even when they are links: uploads is often one) and // folders found under them (links among those were skipped when they were found). while ( $list['queue'] && microtime( true ) < $deadline ) { $rel = array_shift( $list['queue'] ); $abs = $content . $rel; if ( is_file( $abs ) ) { $list['bytes'] += $line( 'f', $rel, $abs ); ++$list['files']; continue; } if ( ! is_dir( $abs ) ) { continue; } $line( 'd', $rel, $abs ); ++$list['dirs']; $entries = @scandir( $abs ); // phpcs:ignore WordPress.PHP.NoSilencedErrors if ( false === $entries ) { $job->log( 'cannot read ' . $rel ); continue; } foreach ( $entries as $entry ) { if ( '.' === $entry || '..' === $entry ) { continue; } $child = $rel . '/' . $entry; if ( Rollbakr_Files::is_excluded( $child, $excluded ) ) { continue; } $cabs = $abs . '/' . $entry; if ( is_link( $cabs ) ) { ++$list['links']; $job->log( 'skipped link ' . $child ); } elseif ( is_dir( $cabs ) ) { $list['queue'][] = $child; } elseif ( is_file( $cabs ) ) { $list['bytes'] += $line( 'f', $child, $cabs ); ++$list['files']; } } $job->set_progress( $list['files'], 0, $rel, array( 'unit' => 'found' ) ); } } finally { fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions clearstatcache( true, $path ); $list['len'] = (int) filesize( $path ); } if ( ! $list['queue'] ) { $job->log( sprintf( 'files: %d files, %d folders, %s', $list['files'], $list['dirs'], size_format( $list['bytes'] ) ) ); // Now the size is known for real: the dump is already on the disk, so only the files count. self::check_space( $job, (int) $list['bytes'] ); self::enter( $job, $list['files'] || $list['dirs'] ? 'files' : 'finish' ); } } /** * Phase 4: write the files into files-N.tar.gz. * * @param Rollbakr_Job $job Job. * @param float $deadline Deadline. * @return void */ private static function files( Rollbakr_Job $job, $deadline ) { $f = &$job->state['files']; $total = (int) $job->state['list']['bytes']; $count = (int) $job->state['list']['files']; $content = Rollbakr_Files::content_dir() . '/'; $dir = $job->dir(); $part = static fn( $n ) => $dir . 'files-' . $n . '.tar.gz'; // A step that died mid-way may have left extra bytes: cut back to the last known length. Rollbakr_Tar::truncate( $part( $f['part'] ), (int) ( $f['parts'][ $f['part'] ] ?? 0 ) ); $list = fopen( $dir . 'list.txt', 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( ! $list ) { throw new RuntimeException( 'Cannot read list.txt' ); } fseek( $list, (int) $f['lpos'] ); $tar = new Rollbakr_Tar( $part( $f['part'] ) ); $tar->open(); $in = null; try { while ( microtime( true ) < $deadline ) { if ( null === $f['cur'] ) { $raw = fgets( $list ); if ( false === $raw ) { // Done. (A part flagged for rotation is simply finished: no empty part after it.) $tar->finish(); $job->log( sprintf( 'files: %d written, %d skipped', $f['done'], $f['skipped'] ) ); self::enter( $job, 'finish' ); break; } // Between files: a part that grew big enough is closed and the next one begun. if ( $f['rotate'] ) { $tar->finish(); $tar->close(); clearstatcache( true, $part( $f['part'] ) ); $f['parts'][ $f['part'] ] = (int) filesize( $part( $f['part'] ) ); $job->log( sprintf( 'files-%d.tar.gz: %s', $f['part'], size_format( $f['parts'][ $f['part'] ] ) ) ); ++$f['part']; $f['rotate'] = false; // The step that died may have been past this point already: start the part clean. Rollbakr_Tar::truncate( $part( $f['part'] ), 0 ); $tar = new Rollbakr_Tar( $part( $f['part'] ) ); $tar->open(); } $f['lpos'] = ftell( $list ); $e = explode( "\t", rtrim( $raw, "\n" ), 5 ); if ( 5 !== count( $e ) ) { continue; } list( $type, $size, $mtime, $mode, $rel ) = $e; if ( 'd' === $type ) { $tar->header( $rel . '/', 0, (int) $mtime, (int) $mode, '5' ); continue; } if ( (int) $size > self::MAX_FILE ) { ++$f['skipped']; $job->log( 'skipped (too big for tar) ' . $rel ); continue; } $f['cur'] = array( $rel, (int) $size, (int) $mtime, (int) $mode ); $f['offset'] = 0; $tar->header( $rel, (int) $size, (int) $mtime, (int) $mode, '0' ); } list( $rel, $size, , ) = $f['cur']; $job->set_progress( $f['bytes'] + $f['offset'], $total, $rel, array( 'unit' => 'files', 'files' => array( $f['done'], $count ) ) ); if ( null === $in ) { $in = @fopen( $content . $rel, 'rb' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors, WordPress.WP.AlternativeFunctions if ( ! $in ) { // The header is already written: fill with zeros so the archive stays valid. $job->log( 'cannot read ' . $rel . ' (filled with zeros)' ); ++$f['skipped']; $in = fopen( 'php://memory', 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions } else { fseek( $in, $f['offset'] ); } } while ( $f['offset'] < $size && microtime( true ) < $deadline ) { $want = min( self::CHUNK, $size - $f['offset'] ); $data = fread( $in, $want ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( false === $data || '' === $data ) { // The file shrank while being read: the header promised $size bytes. $data = str_repeat( "\0", $want ); } $tar->write( $data ); $f['offset'] += strlen( $data ); } if ( $f['offset'] >= $size ) { fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions $in = null; $tar->pad( $size ); $f['bytes'] += $size; ++$f['done']; $f['cur'] = null; if ( $f['bytes'] - ( $f['part_start'] ?? 0 ) > self::part_size() ) { $f['rotate'] = true; $f['part_start'] = $f['bytes']; } } } } finally { if ( $in ) { fclose( $in ); // phpcs:ignore WordPress.WP.AlternativeFunctions } fclose( $list ); // phpcs:ignore WordPress.WP.AlternativeFunctions $tar->close(); clearstatcache( true, $part( $f['part'] ) ); $f['parts'][ $f['part'] ] = (int) filesize( $part( $f['part'] ) ); } } /** * How much (uncompressed) goes into one files-N.tar.gz before the next one is started. * * @return int */ public static function part_size() { /** * Uncompressed bytes per files-N.tar.gz. * * @param int $bytes Default 1 GB. */ return (int) apply_filters( 'rollbakr_part_size', self::PART_SIZE ); } /** * Phase 5: hashes, manifest.json, the final folder name, pruning. * * @param Rollbakr_Job $job Job. * @param float $deadline Deadline. * @return bool True when done. */ private static function finish( Rollbakr_Job $job, $deadline ) { $fin = &$job->state['finish']; $dir = $job->dir(); if ( ! $fin['todo'] && ! $fin['hashes'] ) { // Only the archives the job knows it wrote: nothing a dead step may have left behind. $fin['todo'] = array(); if ( in_array( 'database', $job->state['parts'], true ) ) { $fin['todo'][] = 'database.sql.gz'; } foreach ( array_keys( (array) $job->state['files']['parts'] ) as $n ) { $fin['todo'][] = 'files-' . $n . '.tar.gz'; } foreach ( glob( $dir . '*.gz' ) ?: array() as $stray ) { if ( ! in_array( basename( $stray ), $fin['todo'], true ) ) { $job->log( 'removed stray ' . basename( $stray ) ); wp_delete_file( $stray ); } } } // Hash the archives, a few MB at a time; the hash state survives between steps. while ( ( $fin['todo'] || '' !== $fin['name'] ) && microtime( true ) < $deadline ) { if ( '' === $fin['name'] ) { $fin['name'] = array_shift( $fin['todo'] ); $fin['ctx'] = Rollbakr_Job::save_hash( hash_init( 'sha256' ) ); $fin['offset'] = 0; } $job->set_progress( 0, 0, sprintf( /* translators: %s: file */ __( 'Checking %s', 'rollbakr' ), $fin['name'] ) ); $ctx = Rollbakr_Job::load_hash( $fin['ctx'] ); $fh = fopen( $dir . $fin['name'], 'rb' ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( ! $fh ) { throw new RuntimeException( esc_html( sprintf( 'Cannot read %s', $fin['name'] ) ) ); } fseek( $fh, $fin['offset'] ); while ( ! feof( $fh ) && microtime( true ) < $deadline ) { $data = fread( $fh, 8 * self::CHUNK ); // phpcs:ignore WordPress.WP.AlternativeFunctions hash_update( $ctx, $data ); $fin['offset'] += strlen( $data ); } $done = feof( $fh ); fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( $done ) { $fin['hashes'][ $fin['name'] ] = array( 'size' => (int) filesize( $dir . $fin['name'] ), 'sha256' => hash_final( $ctx ), ); $fin['name'] = ''; $fin['ctx'] = ''; } else { $fin['ctx'] = Rollbakr_Job::save_hash( $ctx ); } } if ( $fin['todo'] || '' !== $fin['name'] ) { return false; } $manifest = self::manifest( $job, $fin['hashes'] ); file_put_contents( $dir . 'manifest.json', wp_json_encode( $manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions if ( is_file( $dir . 'list.txt' ) ) { wp_delete_file( $dir . 'list.txt' ); } $job->log( sprintf( 'done in %s: %s', human_time_diff( $job->state['started'], time() ), size_format( array_sum( array_column( $fin['hashes'], 'size' ) ) ) ) ); $final = Rollbakr_Storage::dir() . $job->id; if ( ! rename( rtrim( $dir, '/' ), $final ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions throw new RuntimeException( 'Cannot rename the backup folder' ); } $job->state['dir'] = $job->id; $job->state['bytes'] = array_sum( array_column( $fin['hashes'], 'size' ) ); $job->state['summary']['finish'] = self::took( $job, 'finish', time() ); $job->set_progress( 1, 1, __( 'Done', 'rollbakr' ) ); // The backup that has just been made is never the one to go, whatever the order of ids says. $own = $job->id; add_filter( 'rollbakr_prune_protected', $protect = static fn( $ids ) => array_merge( (array) $ids, array( $own ) ) ); $removed = Rollbakr_Backups::prune(); remove_filter( 'rollbakr_prune_protected', $protect ); if ( $removed ) { file_put_contents( $final . '/log.txt', wp_date( 'H:i:s' ) . ' removed old backups: ' . implode( ', ', $removed ) . "\n", FILE_APPEND ); // phpcs:ignore WordPress.WP.AlternativeFunctions } /** * A backup finished. * * @param array $manifest The backup's manifest. * @param Rollbakr_Job $job The job. */ do_action( 'rollbakr_backup_finished', $manifest, $job ); return true; } /** * manifest.json contents. * * @param Rollbakr_Job $job Job. * @param array $hashes Archive hashes. * @return array */ private static function manifest( Rollbakr_Job $job, array $hashes ) { global $wpdb; $s = $job->state; $user = get_userdata( (int) $job->user ); return array( 'format' => 1, 'id' => $job->id, 'kind' => $job->args['kind'] ?? 'manual', 'created' => (int) $s['started'], 'finished' => time(), 'site_id' => Rollbakr_Storage::site_id(), 'home' => $s['home'], 'siteurl' => $s['siteurl'], 'prefix' => $s['prefix'], // True when tables without the prefix were dumped too: a restore then lets them through // under their own names (a backup without this key is taken to hold the site's tables only). 'all_tables' => ! empty( $s['all'] ), 'content_dir' => basename( Rollbakr_Files::content_dir() ), 'versions' => array( 'wordpress' => get_bloginfo( 'version' ), 'php' => PHP_VERSION, 'mysql' => $wpdb->db_version(), 'rollbakr' => ROLLBAKR_VERSION, ), 'parts' => $s['parts'], 'user' => (int) $job->user, 'user_login' => $user ? $user->user_login : '', 'status' => 'complete', 'database' => array( 'tables' => (int) $s['db']['tables'], 'rows' => (int) $s['db']['rows'], 'utc' => (bool) $s['db']['utc'], ), 'files' => array( 'count' => (int) $s['files']['done'], 'bytes' => (int) $s['files']['bytes'], 'folders' => (int) $s['list']['dirs'], 'skipped' => (int) $s['files']['skipped'] + (int) $s['list']['links'], ), 'archives' => $hashes, 'excluded' => Rollbakr_Files::excluded(), ); } /** * Append text to a gzip file (used once for the dump header). * * @param string $path Path. * @param string $text Text. * @return void */ private static function gz_append( $path, $text ) { $gz = gzopen( $path, 'ab6' ); if ( ! $gz ) { throw new RuntimeException( esc_html( 'Cannot write ' . basename( $path ) ) ); } gzwrite( $gz, $text ); gzclose( $gz ); } }