2004-10-16 01:06:34 +02:00
< ? php
2008-08-20 08:04:28 +02:00
/**
2016-05-01 17:56:49 +02:00
* EGroupware Setup - DB backup and restore
2008-08-20 08:04:28 +02:00
*
* @ link http :// www . egroupware . org
* @ package setup
* @ author Ralf Becker < RalfBecker - AT - outdoor - training . de >
* @ license http :// opensource . org / licenses / gpl - license . php GPL - GNU General Public License
* @ version $Id $
*/
2016-02-20 21:27:17 +01:00
use EGroupware\Api ;
2016-05-01 17:56:49 +02:00
use EGroupware\Api\Framework ;
use EGroupware\Api\Egw ;
use EGroupware\Api\Vfs ;
2023-10-24 21:22:16 +02:00
use EGroupware\Stylite\Vfs\S3 ;
2016-02-20 21:27:17 +01:00
2018-04-11 15:13:21 +02:00
if ( ! is_object ( @ $GLOBALS [ 'egw' ])) // called from outside EGw ==> setup
2008-08-20 08:04:28 +02:00
{
include ( './inc/functions.inc.php' );
@ set_time_limit ( 0 );
// Check header and authentication
if ( ! $GLOBALS [ 'egw_setup' ] -> auth ( 'Config' ))
2004-10-16 01:06:34 +02:00
{
2008-08-20 08:04:28 +02:00
Header ( 'Location: index.php' );
2004-10-16 01:06:34 +02:00
exit ;
}
2008-08-20 08:04:28 +02:00
// Does not return unless user is authorized
$GLOBALS [ 'egw_setup' ] -> loaddb ();
$tpl_root = $GLOBALS [ 'egw_setup' ] -> html -> setup_tpl_dir ( 'setup' );
$self = 'db_backup.php' ;
2011-01-20 23:17:06 +01:00
$is_setup = true ;
2008-08-20 08:04:28 +02:00
}
2023-10-24 21:22:16 +02:00
if ( class_exists ( S3\Backup :: class ) && S3\Backup :: available ())
{
$db_backup = new S3\Backup ();
}
else
{
$db_backup = new Api\Db\Backup ();
}
2016-05-09 11:15:48 +02:00
$asyncservice = new Api\Asyncservice ();
2008-08-20 08:04:28 +02:00
// download a backup, has to be before any output !!!
2021-10-06 16:07:47 +02:00
if ( ! empty ( $_POST [ 'download' ]))
2008-08-20 08:04:28 +02:00
{
2023-10-24 21:22:16 +02:00
$filename = $db_backup -> backup_dir . '/' . key ( $_POST [ 'download' ]);
$file = $db_backup -> fopen_backup ( $filename , true , false );
2022-05-05 12:47:31 +02:00
// FIRST: switch off zlib.output_compression, as this would limit downloads in size to memory_limit
ini_set ( 'zlib.output_compression' , 0 );
// SECOND: end all active output buffering
while ( ob_end_clean ()) {}
2023-10-24 21:22:16 +02:00
Api\Header\Content :: type ( basename ( $filename ));
fpassthru ( $file );
fclose ( $file );
$db_backup -> log ( $filename , 'Downloaded' );
2008-08-20 08:04:28 +02:00
exit ;
}
2016-05-01 17:56:49 +02:00
$setup_tpl = new Framework\Template ( $tpl_root );
2008-08-20 08:04:28 +02:00
$setup_tpl -> set_file ( array (
'T_head' => 'head.tpl' ,
'T_footer' => 'footer.tpl' ,
'T_db_backup' => 'db_backup.tpl' ,
));
2016-05-01 17:56:49 +02:00
$setup_tpl -> set_var ( 'hidden_vars' , Api\Html :: input_hidden ( 'csrf_token' , Api\Csrf :: token ( __FILE__ )));
2014-05-01 08:26:09 +02:00
// check CSRF token for POST requests with any content (setup uses empty POST to call it's modules!)
if ( $_SERVER [ 'REQUEST_METHOD' ] == 'POST' && $_POST )
{
2016-03-05 15:02:13 +01:00
Api\Csrf :: validate ( $_POST [ 'csrf_token' ], __FILE__ );
2014-05-01 08:26:09 +02:00
}
2008-08-20 08:04:28 +02:00
$setup_tpl -> set_block ( 'T_db_backup' , 'schedule_row' , 'schedule_rows' );
$setup_tpl -> set_block ( 'T_db_backup' , 'set_row' , 'set_rows' );
$setup_tpl -> set_var ( 'stage_title' , $stage_title = lang ( 'DB backup and restore' ));
$setup_tpl -> set_var ( 'stage_desc' , lang ( 'This program lets you backup your database, schedule a backup or restore it.' ));
$setup_tpl -> set_var ( 'error_msg' , '' );
$bgcolor = array ( '#DDDDDD' , '#EEEEEE' );
if ( is_object ( $GLOBALS [ 'egw_setup' ] -> html ))
{
$GLOBALS [ 'egw_setup' ] -> html -> show_header ( $stage_title , False , 'config' , $GLOBALS [ 'egw_setup' ] -> ConfigDomain . '(' . $GLOBALS [ 'egw_domain' ][ $GLOBALS [ 'egw_setup' ] -> ConfigDomain ][ 'db_type' ] . ')' );
}
else
{
$setup_tpl -> set_block ( 'T_db_backup' , 'setup_header' );
$setup_tpl -> set_var ( 'setup_header' , '' );
$GLOBALS [ 'egw_info' ][ 'flags' ][ 'app_header' ] = $stage_title ;
2016-05-14 21:26:36 +02:00
echo $GLOBALS [ 'egw' ] -> framework -> header ();
echo $GLOBALS [ 'egw' ] -> framework -> navbar ();
2010-08-15 17:46:23 +02:00
$run_in_egw = true ;
2008-08-20 08:04:28 +02:00
}
2009-08-27 14:46:12 +02:00
// save backup housekeeping settings
2023-03-10 08:33:07 +01:00
if ( ! empty ( $_POST [ 'save_backup_settings' ]))
2009-08-27 14:46:12 +02:00
{
$matches = array ();
2020-05-12 17:30:55 +02:00
preg_match ( '/^\d*$/' , $_POST [ 'backup_mincount' ], $matches );
$minCount = ( int ) $matches [ 0 ];
2011-01-20 23:17:06 +01:00
$filesBackup = $_POST [ 'backup_files' ] === 'backup_files' ;
2020-05-12 17:30:55 +02:00
if ( empty ( $minCount ) && $matches [ 0 ] != '0' )
2009-08-27 14:46:12 +02:00
{
2009-08-27 15:14:54 +02:00
$minCount = 0 ;
2009-08-27 14:46:12 +02:00
$setup_tpl -> set_var ( 'error_msg' , htmlspecialchars ( lang ( " '%1' must be integer " , lang ( " backup min count " ))));
}
2023-10-24 21:22:16 +02:00
$db_backup -> saveConfig ( $minCount , ! empty ( $is_setup ) ? $filesBackup : null );
2011-01-20 23:17:06 +01:00
if ( is_int ( $minCount ) && $minCount > 0 )
2009-08-27 15:14:54 +02:00
{
2009-08-27 14:46:12 +02:00
$cleaned_files = array ();
/* Remove old backups. */
2009-08-28 13:46:07 +02:00
$db_backup -> housekeeping ( $cleaned_files );
2009-08-27 14:46:12 +02:00
foreach ( $cleaned_files as $file )
{
echo '<div align="center">' . lang ( 'entry has been deleted sucessfully' ) . ': ' . $file . " </div> \n " ;
}
}
}
2023-03-10 08:33:07 +01:00
if ( ! empty ( $_POST [ 'mount' ]))
2012-03-27 13:20:08 +02:00
{
2016-05-01 17:56:49 +02:00
Vfs :: $is_root = true ;
2012-03-27 13:20:08 +02:00
echo '<div align="center">' .
2016-05-01 17:56:49 +02:00
( Vfs :: mount ( 'filesystem://default' . $db_backup -> backup_dir . '?group=Admins&mode=070' , '/backup' , false ) ?
2012-03-27 13:20:08 +02:00
lang ( 'Backup directory %1 mounted as %2' , $db_backup -> backup_dir , '/backup' ) :
lang ( 'Failed to mount Backup directory!' )) .
" </div> \n " ;
2016-05-01 17:56:49 +02:00
Vfs :: $is_root = false ;
2012-03-27 13:20:08 +02:00
}
2008-08-20 08:04:28 +02:00
// create a backup now
2023-03-10 08:33:07 +01:00
if ( ! empty ( $_POST [ 'backup' ]))
2008-08-20 08:04:28 +02:00
{
2023-10-24 21:22:16 +02:00
try {
$f = $db_backup -> fopen_backup ();
2019-02-18 13:41:42 +01:00
$starttime = microtime ( true );
2008-08-20 08:04:28 +02:00
$db_backup -> backup ( $f );
2009-08-27 14:46:12 +02:00
if ( is_resource ( $f ))
2023-10-24 21:22:16 +02:00
{
2009-08-27 14:46:12 +02:00
fclose ( $f );
2023-10-24 21:22:16 +02:00
}
2019-02-18 13:41:42 +01:00
$setup_tpl -> set_var ( 'error_msg' , lang ( 'backup finished' ) . ': ' . number_format ( microtime ( true ) - $starttime , 1 ) . 's' );
2009-08-27 14:46:12 +02:00
/* Remove old backups. */
$cleaned_files = array ();
2009-08-28 13:46:07 +02:00
$db_backup -> housekeeping ( $cleaned_files );
2009-08-27 14:46:12 +02:00
foreach ( $cleaned_files as $file )
{
echo '<div align="center">' . lang ( 'entry has been deleted sucessfully' ) . ': ' . $file . " </div> \n " ;
}
2004-10-16 01:06:34 +02:00
}
2023-10-24 21:22:16 +02:00
catch ( \Exception $e ) {
$setup_tpl -> set_var ( 'error_msg' , $e -> getMessage ());
2004-10-16 01:06:34 +02:00
}
2008-08-20 08:04:28 +02:00
}
2023-03-10 08:33:07 +01:00
$setup_tpl -> set_var ( 'backup_now_button' , '<input type="submit" name="backup" title="' .
htmlspecialchars ( lang ( " back's up your DB now, this might take a few minutes " )) . '" value="' . htmlspecialchars ( lang ( 'backup now' )) .
'" onclick="if (egw && egw.loading_prompt) egw.loading_prompt(\'db_backup\', true, \'' . htmlspecialchars ( lang ( 'backup started, this might take a few minutes ...' )) . '\'); return true;" />' );
2008-08-20 08:04:28 +02:00
$setup_tpl -> set_var ( 'upload' , '<input type="file" name="uploaded" /> ' .
'<input type="submit" name="upload" value="' . htmlspecialchars ( lang ( 'upload backup' )) . '" title="' . htmlspecialchars ( lang ( " uploads a backup to the backup-dir, from where you can restore it " )) . '" />' );
2009-08-27 14:46:12 +02:00
$setup_tpl -> set_var ( 'backup_mincount' , '<input type="text" name="backup_mincount" value="' . $db_backup -> backup_mincount . '" size="3" maxlength="3"/>' );
2011-01-20 23:17:06 +01:00
$setup_tpl -> set_var ( 'backup_files' , '<input type="checkbox" name="backup_files" value="backup_files"' .
( $db_backup -> backup_files ? ' checked="true"' : '' ) .
// do NOT allow to change "backup files" outside of setup
( $is_setup ? '' : ' disabled="true" title="' . htmlspecialchars ( lang ( 'Can only be change via Setup!' )) . '"' ) . '/>' );
2009-08-27 14:46:12 +02:00
$setup_tpl -> set_var ( 'backup_save_settings' , '<input type="submit" name="save_backup_settings" value="' . htmlspecialchars ( lang ( 'save' )) . '" />' );
2012-03-27 13:20:08 +02:00
$setup_tpl -> set_var ( 'backup_mount' , '<input type="submit" name="mount" value="' . htmlspecialchars ( lang ( 'Mount backup directory to %1' , '/backup' )) . '" />' );
2004-10-16 01:06:34 +02:00
2023-03-10 08:33:07 +01:00
if ( ! empty ( $_POST [ 'upload' ]) && is_array ( $_FILES [ 'uploaded' ]) && ! $_FILES [ 'uploaded' ][ 'error' ] &&
2023-10-24 21:22:16 +02:00
is_uploaded_file ( $_FILES [ 'uploaded' ][ 'tmp_name' ]) && ( $msg = $db_backup -> upload ( $_FILES [ 'uploaded' ])))
2008-08-20 08:04:28 +02:00
{
2023-10-24 21:22:16 +02:00
$setup_tpl -> set_var ( 'error_msg' , $msg );
2008-08-20 08:04:28 +02:00
}
// delete a backup
2023-10-24 21:22:16 +02:00
if ( ! empty ( $_POST [ 'delete' ]) && ( $msg = $db_backup -> delete ( key ( $_POST [ 'delete' ]))))
2008-08-20 08:04:28 +02:00
{
2023-10-24 21:22:16 +02:00
$setup_tpl -> set_var ( 'error_msg' , $msg );
2008-08-20 08:04:28 +02:00
}
// rename a backup
2023-10-24 21:22:16 +02:00
if ( ! empty ( $file = $_POST [ 'rename' ]) && ! empty ( $_POST [ 'new_name' ][ $file ]) &&
( $msg = $db_backup -> rename ( $file , $_POST [ 'new_name' ][ $file ])))
2008-08-20 08:04:28 +02:00
{
2023-10-24 21:22:16 +02:00
$setup_tpl -> set_var ( 'error_msg' , $msg );
2008-08-20 08:04:28 +02:00
}
// restore a backup
2021-10-06 16:07:47 +02:00
if ( ! empty ( $_POST [ 'restore' ]))
2008-08-20 08:04:28 +02:00
{
2019-02-12 22:13:45 +01:00
$file = key ( $_POST [ 'restore' ]);
2008-08-20 08:04:28 +02:00
$file = $db_backup -> backup_dir . '/' . basename ( $file ); // basename to not allow to change the dir
2023-10-24 21:22:16 +02:00
try {
$f = $db_backup -> fopen_backup ( $file , true );
2012-06-10 14:38:35 +02:00
$start = time ();
2023-10-24 21:22:16 +02:00
$db_backup -> restore ( $f , true , $file ); // always convert to current system charset on restore
2012-06-10 14:38:35 +02:00
$setup_tpl -> set_var ( 'error_msg' , lang ( " backup '%1' restored " , $file ) . ' (' . ( time () - $start ) . ' s)' );
2023-10-24 21:22:16 +02:00
if ( isset ( $run_in_egw ))
2010-08-15 17:46:23 +02:00
{
// updating the backup
$cmd = new setup_cmd_update ( $GLOBALS [ 'egw' ] -> session -> account_domain ,
$GLOBALS [ 'egw_info' ][ 'server' ][ 'header_admin_user' ] = 'admin' ,
2023-11-02 13:42:14 +01:00
$GLOBALS [ 'egw_info' ][ 'server' ][ 'header_admin_password' ] = uniqid ( 'pw' , true ), false );
2010-08-15 17:46:23 +02:00
echo $cmd -> run () . " \n " ;
2016-05-01 17:56:49 +02:00
echo '<h3>' . lang ( 'You should %1log out%2 and in again, to update your current session!' , '<a href="' . Egw :: link ( '/logout.php' ) . '" target="_parent">' , '</a>' ) . " </h3> \n " ;
2010-08-15 17:46:23 +02:00
}
2004-10-16 01:06:34 +02:00
}
2023-10-24 21:22:16 +02:00
catch ( \Exception $e )
2004-10-16 01:06:34 +02:00
{
2023-10-24 21:22:16 +02:00
$setup_tpl -> set_var ( 'error_msg' , $e -> getMessage ());
2008-08-20 08:04:28 +02:00
}
}
// create a new scheduled backup
2021-10-06 16:07:47 +02:00
if ( ! empty ( $_POST [ 'schedule' ]))
2008-08-20 08:04:28 +02:00
{
$asyncservice -> set_timer ( $_POST [ 'times' ], 'db_backup-' . implode ( ':' , $_POST [ 'times' ]), 'admin.admin_db_backup.do_backup' , '' );
}
// cancel a scheduled backup
2021-10-06 16:07:47 +02:00
if ( ! empty ( $_POST [ 'cancel' ]) && is_array ( $_POST [ 'cancel' ]))
2008-08-20 08:04:28 +02:00
{
2019-02-12 22:13:45 +01:00
$id = key ( $_POST [ 'cancel' ]);
2008-08-20 08:04:28 +02:00
$asyncservice -> cancel_timer ( $id );
}
// list scheduled backups
if (( $jobs = $asyncservice -> read ( 'db_backup-%' )))
{
foreach ( $jobs as $job )
2004-10-16 01:06:34 +02:00
{
2008-08-20 08:04:28 +02:00
$setup_tpl -> set_var ( $job [ 'times' ]);
$setup_tpl -> set_var ( 'next_run' , date ( 'Y-m-d H:i' , $job [ 'next' ]));
$setup_tpl -> set_var ( 'actions' , '<input type="submit" name="cancel[' . $job [ 'id' ] . ']" value="' . htmlspecialchars ( lang ( 'delete' )) . '" />' );
$setup_tpl -> parse ( 'schedule_rows' , 'schedule_row' , true );
}
}
// input-fields to create a new scheduled backup
foreach ( $times = array ( 'year' => '*' , 'month' => '*' , 'day' => '*' , 'dow' => '2-6' , 'hour' => 3 , 'min' => 0 ) as $name => $default )
{
$setup_tpl -> set_var ( $name , '<input name="times[' . $name . ']" size="5" value="' . $default . '" />' );
}
$setup_tpl -> set_var ( 'next_run' , ' ' );
$setup_tpl -> set_var ( 'actions' , '<input type="submit" name="schedule" value="' . htmlspecialchars ( lang ( 'schedule' )) . '" />' );
$setup_tpl -> parse ( 'schedule_rows' , 'schedule_row' , true );
2023-10-24 21:22:16 +02:00
// listing the available backups
2008-08-20 08:04:28 +02:00
$setup_tpl -> set_var ( 'backup_dir' , $db_backup -> backup_dir );
$setup_tpl -> set_var ( 'set_rows' , '' );
2004-10-16 01:06:34 +02:00
2023-10-24 21:22:16 +02:00
foreach ( $db_backup -> index () as $file => $attrs )
2008-08-20 08:04:28 +02:00
{
2004-10-16 01:06:34 +02:00
$setup_tpl -> set_var ( array (
2008-08-20 08:04:28 +02:00
'filename' => $file ,
2023-10-24 21:22:16 +02:00
'mod' => date ( 'Y-m-d H:i' , $attrs [ 'ctime' ]),
'size' => sprintf ( '%3.1f MB (%d)' , $attrs [ 'size' ] / ( 1024 * 1024 ), $attrs [ 'size' ]),
2008-08-20 08:04:28 +02:00
'actions' => '<input type="submit" name="download[' . $file . ']" value="' . htmlspecialchars ( lang ( 'download' )) . '" /> ' . " \n " .
2023-03-10 08:33:07 +01:00
( $file === Api\Db\Backup :: LOG_FILE ? '' :
2008-08-20 08:04:28 +02:00
'<input type="submit" name="delete[' . $file . ']" value="' . htmlspecialchars ( lang ( 'delete' )) . '" onclick="return confirm(\'' .
htmlspecialchars ( lang ( 'Confirm to delete this backup?' )) . '\');" /> ' . " \n " .
'<input name="new_name[' . $file . ']" value="" size="15" /><input type="submit" name="rename[' . $file . ']" value="' . htmlspecialchars ( lang ( 'rename' )) . '" /> ' . " \n " .
2023-03-10 08:33:07 +01:00
'<input type="submit" name="restore[' . $file . ']" value="' . htmlspecialchars ( lang ( 'restore' )) . '" onclick="if (confirm(\'' .
htmlspecialchars ( lang ( 'Restoring a backup will delete/replace all content in your database. Are you sure?' )) .
'\')) { if (egw && egw.loading_prompt) egw.loading_prompt(\'db_backup\', true, \'' . htmlspecialchars ( lang ( 'restore started, this might take a few minutes ...' )) .
'\'); return true; } else return false;" />' ),
2004-10-16 01:06:34 +02:00
));
2008-08-20 08:04:28 +02:00
$setup_tpl -> parse ( 'set_rows' , 'set_row' , true );
}
$setup_tpl -> set_var ( array (
'lang_scheduled_backups' => lang ( 'scheduled backups' ),
'lang_year' => lang ( 'year' ),
'lang_month' => lang ( 'month' ),
'lang_day' => lang ( 'day' ),
'lang_dow' => lang ( 'day of week<br />(0-6, 0=sunday)' ),
'lang_hour' => lang ( 'hour (0-24)' ),
'lang_minute' => lang ( 'minute' ),
'lang_next_run' => lang ( 'next run' ),
'lang_actions' => lang ( 'actions' ),
'lang_backup_sets' => lang ( 'backup sets' ),
2009-08-27 14:46:12 +02:00
'lang_backup_cleanup' => lang ( 'backup housekeeping' ),
'lang_backup_mincount' => lang ( 'min backup count' ),
'lang_backup_files_info' => lang ( 'backup files (needs ZipArchive)' ),
'lang_backup_files' => lang ( 'check to backup and restore the files directory (may use a lot of space, make sure to configure housekeeping accordingly)' ),
2008-08-20 08:04:28 +02:00
'lang_filename' => lang ( 'filename' ),
'lang_date' => lang ( 'created' ),
2010-04-20 16:06:26 +02:00
'lang_mod' => lang ( 'modified' ),
2008-08-20 08:04:28 +02:00
'lang_size' => lang ( 'size' ),
));
$setup_tpl -> set_var ( 'self' , $self );
$setup_tpl -> pparse ( 'out' , 'T_db_backup' );
2023-10-24 21:22:16 +02:00
if ( isset ( $run_in_egw ))
2010-08-15 17:46:23 +02:00
{
2016-05-14 21:26:36 +02:00
echo $GLOBALS [ 'egw' ] -> framework -> footer ();
2010-08-15 17:46:23 +02:00
}
else
2008-08-20 08:04:28 +02:00
{
$GLOBALS [ 'egw_setup' ] -> html -> show_footer ();
2022-05-05 12:47:31 +02:00
}