11#! /bin/bash
2- # info: Dumps database contents in STDIN / file
3- # options: USER DATABASE [FILE]
2+ # info: Dumps database contents in STDIN or file optional file can be compressed
3+ # options: USER DATABASE [FILE] [COMPRESSION]
44#
55# example: v-dump-database user user_databse > test.sql
6- # example: v-dump-database user user_databse file
6+ # example: v-dump-database user user_databse file gzip
7+ # example: v-dump-database user user_databse file zstd
78#
8- # Dumps database in STDIN or /backup/user.database.type.sql
9+ # Dumps database in STDIN or file (/backup/user.database.type.sql)
10+ # For compression gzip or zstd is supported by default plain sql is used
911
1012# ----------------------------------------------------------#
1113# Variables & Functions #
1517user=$1
1618database=$2
1719output=$3
20+ compression=$4
1821
1922# Includes
2023# shellcheck source=/etc/hestiacp/hestia.conf
@@ -26,7 +29,7 @@ source $HESTIA/func/db.sh
2629# load config file
2730source_conf " $HESTIA /conf/hestia.conf"
2831
29- check_args ' 2' " $# " ' USER DATABASE'
32+ check_args ' 2' " $# " ' USER DATABASE [OUTPUT] [COMPRESSION] '
3033is_format_valid ' user' ' database'
3134is_system_enabled " $DB_SYSTEM " ' DB_SYSTEM'
3235is_object_valid ' user' ' USER' " $user "
@@ -63,17 +66,30 @@ case $TYPE in
6366 pgsql) dump_pgsql_database ;;
6467esac
6568
69+ if [ " $compression " = ' zstd' ]; then
70+ extension=" sql.zst"
71+ pzstd $dump
72+ rm $dump
73+ dump=" $tmpdir /$database .$TYPE .sql.zst"
74+ elif [ " $compression " = ' gzip' ]; then
75+ extension=" sql.gz"
76+ gzip $dump
77+ dump=" $tmpdir /$database .$TYPE .sql.gz"
78+ else
79+ extension=" sql"
80+ fi
81+
6682if [ " $output " = " file" ]; then
6783 # echo filename for use in the php
68- echo " ${user} _${database} _${TYPE} _${timestamp} .sql "
84+ echo " ${user} _${database} _${TYPE} _${timestamp} .${extension} "
6985
70- cp $dump $BACKUP /${user} _${database} _${TYPE} _${timestamp} .sql
86+ cp $dump $BACKUP /${user} _${database} _${TYPE} _${timestamp} .${extension}
7187
7288 # echo file location for use in the php
73- echo " $BACKUP /${user} _${database} _${TYPE} _${timestamp} .sql "
89+ echo " $BACKUP /${user} _${database} _${TYPE} _${timestamp} .${extension} "
7490
7591 # cleanup
76- echo " rm $BACKUP /${user} _${database} _${TYPE} _${timestamp} .sql " | at now + 1 hour
92+ echo " rm $BACKUP /${user} _${database} _${TYPE} _${timestamp} .${extension} " | at now + 1 hour
7793else
7894 cat $dump
7995fi
0 commit comments