index.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. use yii\grid\GridView;
  3. use yii\helpers\Html;
  4. /* @var $this yii\web\View */
  5. /* @var $dataProvider yii\data\ActiveDataProvider */
  6. $this->title = '数据备份';
  7. $this->params['breadcrumbs'][] = $this->title;
  8. ?>
  9. <div class="config-index">
  10. <?php \yii\widgets\ActiveForm::begin(['id' => 'export-form', 'action' => ['init']])?>
  11. <p>
  12. <?= Html::a('立即备份', ['init'], ['class' => 'btn btn-success', 'id' => 'export']) ?>
  13. </p>
  14. <div class="box box-success">
  15. <div class="box-body">
  16. <?= GridView::widget([
  17. 'id' => 'grid',
  18. 'dataProvider' => $dataProvider,
  19. 'columns' => [
  20. [
  21. 'class' => \yii\grid\CheckboxColumn::className(),
  22. 'name' => 'tables',
  23. 'checkboxOptions' => function ($model, $key, $index, $column) {
  24. return ['value' => $model['name']];
  25. }
  26. ],
  27. 'name:text:表名',
  28. 'rows:text:数据量',
  29. [
  30. 'attribute' => 'data_length',
  31. 'label' => '数据大小',
  32. 'value' => function ($model) {
  33. return Yii::$app->formatter->asShortSize($model['data_length']);
  34. }
  35. ],
  36. 'create_time:text:创建时间',
  37. [
  38. 'class' => 'yii\grid\ActionColumn',
  39. 'template' => '{a} {b}',
  40. 'buttons' => [
  41. 'a' => function ($url, $model, $key) {
  42. return Html::a('优化表',
  43. ['export/optimize', 'tables' => $model['name']],
  44. [
  45. 'data' => [
  46. 'ajax' => 1,
  47. 'method' => 'get'
  48. ],
  49. 'class' => 'btn btn-default btn-xs'
  50. ]
  51. );
  52. },
  53. 'b' => function ($url, $model, $key) {
  54. return Html::a('修复表',
  55. ['export/repair', 'tables' => $model['name']],
  56. [
  57. 'data' => [
  58. 'ajax' => 1,
  59. 'method' => 'get'
  60. ],
  61. 'class' => 'btn btn-default btn-xs'
  62. ]
  63. );
  64. }
  65. ]
  66. ],
  67. ],
  68. ]); ?>
  69. <?php \yii\widgets\ActiveForm::end()?>
  70. </div>
  71. </div>
  72. </div>
  73. <?php $this->beginBlock('js'); ?>
  74. <!-- /应用列表 -->
  75. <script type="text/javascript">
  76. (function($){
  77. var $form = $("#export-form"), $export = $("#export"), tables
  78. $optimize = $("#optimize"), $repair = $("#repair");
  79. $optimize.add($repair).click(function(){
  80. $.post(this.href, $form.serialize(), function(data){
  81. if(data.status){
  82. $.modal.success(data.info);
  83. } else {
  84. $.modal.error(data.info);
  85. }
  86. setTimeout(function(){
  87. $('#top-alert').find('button').click();
  88. $(that).removeClass('disabled').prop('disabled',false);
  89. },1500);
  90. }, "json");
  91. return false;
  92. });
  93. $export.click(function(){
  94. if ($('#grid').yiiGridView('getSelectedRows').length <= 0) {
  95. $.modal.error('请选择要备份的表');
  96. return false;
  97. }
  98. $export.parent().children().addClass("disabled");
  99. $export.html("正在发送备份请求...");
  100. var that = this;
  101. $.post(
  102. $form.attr("action"),
  103. $form.serialize(),
  104. function(data){
  105. if(data.status){
  106. tables = data.tables;
  107. $export.html(data.info + "开始备份,请不要关闭本页面!");
  108. backup.call(that, data.tab);
  109. window.onbeforeunload = function(){ return "正在备份数据库,请不要关闭!" }
  110. } else {
  111. $.modal.error(data.info);
  112. $export.parent().children().removeClass("disabled");
  113. $export.html("立即备份");
  114. setTimeout(function(){
  115. $(that).removeClass('disabled').prop('disabled',false);
  116. },1500);
  117. }
  118. },
  119. "json"
  120. );
  121. return false;
  122. });
  123. function backup(tab, status){
  124. status && showmsg(tab.id, "开始备份...(0%)");
  125. $.post('<?= \yii\helpers\Url::to(['start'])?>', tab, function(data){
  126. if(data.status){
  127. showmsg(tab.id, data.info);
  128. if(!$.isPlainObject(data.tab)){
  129. $export.parent().children().removeClass("disabled");
  130. $export.html("备份完成,点击重新备份");
  131. window.onbeforeunload = function(){ return null }
  132. return;
  133. }
  134. backup(data.tab, tab.id != data.tab.id);
  135. } else {
  136. $.modal.error(data.info);
  137. $export.parent().children().removeClass("disabled");
  138. $export.html("立即备份");
  139. setTimeout(function(){
  140. $(this).removeClass('disabled').prop('disabled',false);
  141. },1500);
  142. }
  143. }, "json");
  144. }
  145. function showmsg(id, msg){
  146. $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
  147. }
  148. })(jQuery);
  149. </script>
  150. <?php $this->endBlock('js'); ?>