Daily automatic database download

I want to be created The database download (database export) is stored in the desired web folder. automatically every day. How do I use it? Can I use a Cron Job or do I have to do it?

Thanks.

It should be possible with a bash script and cron job.

<?php
$fecha = date("Y-m-d_His");  
$nombre = "APIMP_BD_".$fecha.".sql";
//v-dump-database user user_databse > test.sql
system('v-dump-database XXXX XXX_mp > '.$nombre);
system('zstd '.$nombre);
unlink($nombre);
?>


Error

You can’t run hestia commands as normal user

mysqldump -u user -p password database > test.sql

Will work fine

Here’s a script I run (as root) on some of my servers once a day. It loops through the users and creates dumps of all their databases into /home/user/dbbackup. Your mileage may vary. Read and understand before using. I claim diplomatic immunity if it breaks your server (but it probably won’t)

#!/bin/bash
# script needs these to use Hestia Tools
export HESTIA=/usr/local/hestia
export PATH=$PATH:/usr/local/hestia/bin

# Loop through  Users
for USER in $( /usr/local/hestia/bin/v-list-users plain | awk '{print $1}' )
do
	DIR="/home/$USER/dbbackup"
	echo "= Processing $USER "
        # Make dir if this is the first time
	if [ ! -d "$DIR" ] ; then
		echo "Creating dir"
		mkdir -p "$DIR"
		chown -f "$USER":"$USER" "$DIR"
	fi

	# Loop through hestia DBs for that user
	for DB in  $( /usr/local/hestia/bin/v-list-databases "$USER" plain | awk '{print $1}' )
	do
		# Dump DB
		/usr/bin/mysqldump --defaults-extra-file=/root/.my.cnf --quick --single-transaction --skip-add-locks "$DB" | gzip > "$DIR"/"$(date +%F-%H-%M)"_"$DB".tar.gz
		# Pause to give the server a break
		sleep 10
	done
	# fix perms (silently)
	chown -f "$USER":"$USER" "$DIR"/*.tar.gz
	# Keep only last three days' files locally. Older can be recovered from offsite backup
	echo "Deleting old backups in $DIR"
	find "$DIR" -name '*tar.gz' -mtime +3 -delete
done

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.