Código PHP:
if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['databasename'])) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$databasename = trim($_POST['databasename']);
$backupRestore = $_POST['backupRestore'];
if ($backupRestore == 'backup'){
$data = $_POST['data'];
$now = str_replace(":", "", date("Y-m-d H:i:s"));
$outputfilename = $databasename . '-' . $now . '.sql';
$outputfilename = str_replace(" ", "-", $outputfilename);
//Dump the MySQL database
if ($data == "wd"){
//With data
exec('mysqldump -u '. $username .' -p'. $password .' '. $databasename .' > '. $outputfilename);
}
else{
//Without data
exec('mysqldump --no-data -u '. $username .' -p'. $password .' '. $databasename .' > '. $outputfilename);
}
//Download the database file
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($outputfilename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($outputfilename));
ob_clean();
flush();
readfile($outputfilename);
//After download remove the file from server
exec('rm ' . $outputfilename);
}
else{//Restore the database
$target_path = getcwd();
$databasefilename = $_FILES["databasefile"]["name"];
//Upload the database file to current working directory
move_uploaded_file($_FILES["databasefile"]["tmp_name"], $target_path . '/' . $databasefilename);
//Restore the database
exec('mysql -u '. $username .' -p'. $password .' '. $databasename .' < '. $databasefilename);
//Remove the file from server
exec('rm ' . $databasefilename);
}
}
Código PHP:
<form action="" method="POST" class="form-horizontal" onsubmit="return checkParameters();" autocomplete="off">
<div class="form-group">
<label for="inputEmail"class="col-xs-12 col-sm-3 col-md-3 col-lg-3 control-label">Usuario:</label>
<div class="col-xs-12 col-sm-9 col-md-9 col-lg-9">
<input type="name" name="username" id="username" class="form-control" placeholder="" value="root">
</div>
</div>
<div class="form-group">
<label for="inputEmail"class="col-xs-12 col-sm-3 col-md-3 col-lg-3 control-label">Contraseña:</label>
<div class="col-xs-12 col-sm-9 col-md-9 col-lg-9">
<input type="password" name="password" id="password" class="form-control" value="root" readonly="true">
</div>
</div>
<div class="form-group">
<label for="inputEmail"class="col-xs-12 col-sm-3 col-md-3 col-lg-3 control-label">Base De Datos:</label>
<div class="col-xs-12 col-sm-9 col-md-9 col-lg-9">
<input type="name" id="databasename" name="databasename" class="form-control" value="db_sicopa_desa" readonly="true">
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-xs-12 col-sm-3 col-md-3 col-lg-3 control-label">Controles :</label>
<div class="col-xs-12 col-sm-9 col-md-9 col-lg-9">
<div class="radio">
<label>
<input type="radio" name="backupRestore" id="backup" value="backup" checked="true" onclick="showHide(this.id);">
Backup
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="backupRestore" id="restore" value="restore" onclick="showHide(this.id);">
Restaurar
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-xs-12 col-sm-3 col-md-3 col-lg-3 control-label">Controles Datos BD :</label>
<div class="col-xs-12 col-sm-9 col-md-9 col-lg-9">
<div class="radio">
<label>
<input type="radio" name="data" value="wd" checked="true" >
BD Con Informacion
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="data" value="wod" >
BD Vacio
</label>
</div>
</div>
</div>
<div class="form-group">
<center>
<div class="col-xs-12 col-sm-2 col-sm-offset-3">
<input type="submit" class="btn btn-primary" value="Procesar">
</div>
<div class="col-xs-12 col-sm-2 col-sm-offset-1">
<input type="file" name="databasefile" class="restoreFile">
</div>
</center>
</div>
</form>
despues de haber mostrado mi codigo tengo dos errores el primer error es que al hacer un backup me hace un backup vacio
y el segundo error es que al restaurar un archivo me muestra el siguiente error:
Notice: Undefined index: databasefile in C:\wamp\www\sicopa\user\v_Bd.php on line 63
Notice: Undefined index: databasefile in C:\wamp\www\sicopa\user\v_Bd.php on line 66
y ese error sucede en estas lineas :
linea 63:
Código PHP:
$databasefilename = $_FILES["databasefile"]["name"];
Código PHP:
move_uploaded_file($_FILES["databasefile"]["tmp_name"], $target_path . '/' . $databasefilename);