Command to run after MySQL backup

7 posts / 0 new
Last post
#1 Tue, 10/31/2017 - 03:13

Command to run after MySQL backup

Hi there

I am using the Backup All Databases schedule - to backup all databases to a local folder.

As space is not an issue - is there an easy way to create a zip folder of all databases once the backup is completed, including the date as a reference in the folder name would be nice.

This is to stop the backup schedule overwriting the databases on each run, of course .

Any assistance would be appreciated

Kind Regards Brad

Tue, 10/31/2017 - 06:48

I presume you are using Webmin->Servers->MySQL Database Server->Backup All Databases.

There doesn't appear to be an ability to use 'date' options directly (like having your backup directory be /var/backups/mysql/%F, for example). In that case, I'd probably write a small shell script that creates the dated directory and then moves the backed up files into it. You can run it from the "Command to run after backup" option.

Edit: Just realised this is in the Newbies section. I've added some comments to the sample code below. You should make sure you understand what random code you find on the Internet does before you commit it to your server...

# Warning, untested, million ways to improve, etc
# BDIR is the directory you are saving the backups to in Webmin
# This gives us the current date in yyyy-mm-dd format.
DATE=`date +%F`
# This creates a new directory. If the date was 31st October 2017,
# the directory will be /var/backups/mysql/2017-10-31
mkdir -p $BDIR/$DATE
# This moves any files ending in sql.gz from the Webmin specified
# directory, to the new directory. Change the file extension depending
# on options selected on your backups. This one assumes gzip used.
mv $BDIR/*sql.gz $BDIR/$DATE/

Of course, Joe or another user may pop up with a way to do this directly in Webmin. :)

Wed, 11/01/2017 - 17:07

Hi @noisemarine

Ah thank you very much for your reply and explanation. It works perfectly and does just what I wanted.

Most grateful.

Kind Regards Brad

Mon, 11/06/2017 - 09:04
J Rudderham

I don't know if this will help, but I've set mine as follows:

In Webmin > Servers > Backup All Databases, I've set "Backup to directory" to "Path on server" at /_mariadb-backup/%Y%m%d-%H%M and have the "Create destinations directory?" option set to "Yes".

This gives me a succession of folders in my "_mariadb-backup" folder like this:

  • 20171103-0000
  • 20171104-0000
  • 20171105-0000
  • 20171106-0000
  • etc

...with each folder containing .sql.gz of each database.

The %Y%m%d-%H%M breaks down thus:

%Y is the current year in 4-digit format
%m is the current month in 2-digit format
%d is the current date in 2-digit format
- is just a separator
%H is the current hour
$M is the current minutes

I have my backup set to run at midnight, hence the %H%M is "0000"

Wed, 11/08/2017 - 05:16 (Reply to #4)

Provided variables don't give me correct folder names. I've set "Backup to directory" /backup/database/%Y%m%d-%H%M and I have only one folder in /backup/database/ which name is %Y%m%d-%H%M. I am using CentOS 6.

Mon, 11/06/2017 - 11:05

Thank you very much for the helpful information.

Fri, 07/13/2018 - 13:06
DreTaX's picture

A simple bash that does renaming.

for i in *;do
    if [[ "$i" == *"$asd"* ]];then
        if [[ "$i" != *"$asd2"* ]];then
    mv "$i" "$(date +"%F_%H-%M-%S")_$i";
Topic locked