Skip to content

Commit 517e4c8

Browse files
authored
Feature: v-dump-database (hestiacp#3644)
* Add support to dump database via command line * Update permissions * Update UI * Update description
1 parent 3586630 commit 517e4c8

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

bin/v-dump-database

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/bin/bash
2+
# info: Dumps database contents in STDIN / file
3+
# options: USER DATABASE [FILE]
4+
#
5+
# example: v-dump-database user user_databse > test.sql
6+
# example: v-dump-database user user_databse file
7+
#
8+
# Dumps database in STDIN or /backup/user.database.type.sql
9+
10+
#----------------------------------------------------------#
11+
# Variables & Functions #
12+
#----------------------------------------------------------#
13+
14+
# Argument definition
15+
user=$1
16+
database=$2
17+
output=$3
18+
19+
# Includes
20+
# shellcheck source=/etc/hestiacp/hestia.conf
21+
source /etc/hestiacp/hestia.conf
22+
# shellcheck source=/usr/local/hestia/func/main.sh
23+
source $HESTIA/func/main.sh
24+
# shellcheck source=/usr/local/hestia/func/db.sh
25+
source $HESTIA/func/db.sh
26+
# load config file
27+
source_conf "$HESTIA/conf/hestia.conf"
28+
29+
check_args '2' "$#" 'USER DATABASE'
30+
is_format_valid 'user' 'database'
31+
is_system_enabled "$DB_SYSTEM" 'DB_SYSTEM'
32+
is_object_valid 'user' 'USER' "$user"
33+
is_object_unsuspended 'user' 'USER' "$user"
34+
35+
# Perform verification if read-only mode is enabled
36+
check_hestia_demo_mode
37+
38+
#----------------------------------------------------------#
39+
# Action #
40+
#----------------------------------------------------------#
41+
42+
# Check db existence
43+
db_data=$(grep "DB='$database'" $HESTIA/data/users/$user/db.conf)
44+
if [ -z "$db_data" ]; then
45+
echo "Error: database $database doesn't exist"
46+
log_event "$E_NOTEXIST" "$ARGUMENTS"
47+
exit "$E_NOTEXIST"
48+
fi
49+
50+
parse_object_kv_list "$db_data"
51+
52+
# Creating temporary directory
53+
tmpdir=$(mktemp -p $BACKUP -d "tmp.$database.XXXXXXXXXX")
54+
55+
# Dump database
56+
dump="$tmpdir/$database.$TYPE.sql"
57+
grants="$tmpdir/$database.$TYPE.$DBUSER"
58+
case $TYPE in
59+
mysql) dump_mysql_database ;;
60+
pgsql) dump_pgsql_database ;;
61+
esac
62+
63+
if [ "$output" = "file" ]; then
64+
cp $dump $BACKUP/$user.$database.$TYPE.sql
65+
echo $BACKUP/$user.$database.$TYPE.sql
66+
67+
echo "rm $BACKUP/$user.$database.$TYPE.sql" | at now + 1 hour
68+
else
69+
cat $dump
70+
fi
71+
72+
rm -fr $tmpdir
73+
74+
#----------------------------------------------------------#
75+
# Hestia #
76+
#----------------------------------------------------------#
77+
78+
# Logging
79+
log_event "$OK" "$ARGUMENTS"
80+
81+
exit

web/download/database/index.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
use function Hestiacp\quoteshellarg\quoteshellarg;
3+
4+
ob_start();
5+
include $_SERVER["DOCUMENT_ROOT"] . "/inc/main.php";
6+
7+
// Check token
8+
verify_csrf($_GET);
9+
10+
$database = quoteshellarg($_GET["database"]);
11+
12+
exec(HESTIA_CMD . "v-dump-database " . $user . " " . $database . " file", $output, $return_var);
13+
14+
if ($return_var == 0) {
15+
header("Content-type: application/sql");
16+
header("Content-Disposition: attachment; filename=\"" . $_GET["database"] . ".sql\";");
17+
header("X-Accel-Redirect: " . $output[0]);
18+
}

web/templates/pages/list_db.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
<input type="checkbox" class="js-toggle-all-checkbox" title="<?= _("Select all") ?>" <?= $display_mode ?>>
107107
</div>
108108
<div class="clearfix l-unit__stat-col--left wide-3"><b><?= _("Name") ?></b></div>
109-
<div class="clearfix l-unit__stat-col--left u-text-right compact-3"><b>&nbsp;</b></div>
109+
<div class="clearfix l-unit__stat-col--left u-text-right"><b>&nbsp;</b></div>
110110
<div class="clearfix l-unit__stat-col--left u-text-center"><b><?= _("Disk") ?></b></div>
111111
<div class="clearfix l-unit__stat-col--left u-text-center compact"><b><?= _("Type") ?></b></div>
112112
<div class="clearfix l-unit__stat-col--left u-text-center wide"><b><?= _("Username") ?></b></div>
@@ -161,7 +161,7 @@
161161
<?php } ?>
162162
</div>
163163
<!-- START QUICK ACTION TOOLBAR AREA -->
164-
<div class="clearfix l-unit__stat-col--left u-text-right compact-3">
164+
<div class="clearfix l-unit__stat-col--left u-text-right">
165165
<div class="l-unit-toolbar__col l-unit-toolbar__col--right u-noselect">
166166
<div class="actions-panel clearfix">
167167
<?php if ($read_only === "true") { ?>
@@ -182,6 +182,11 @@
182182
</a>
183183
</div>
184184
<?php } ?>
185+
<div class="actions-panel__col actions-panel__logs shortcut-enter" data-key-action="href">
186+
<a href="/download/database/?database=<?=$key?>&token=<?=$_SESSION['token']?>" title="<?= _("Download Database") ?>">
187+
<i class="fas fa-download icon-orange icon-dim"></i>
188+
</a>
189+
</div>
185190
<div class="actions-panel__col actions-panel__suspend shortcut-s" data-key-action="js">
186191
<a
187192
class="data-controls js-confirm-action"

0 commit comments

Comments
 (0)