databacker / mysql-backup

image to enable automated backups of mysql databases in containers
636 stars 178 forks source link

Simple script for moving to target backup by year and month #326

Open Doctorf opened 2 months ago

Doctorf commented 2 months ago

Hi team. Here is a simple script for moving target backup to a new directory by year and month:

File /scripts.d/target.sh:

#!/bin/bash
#  Debug mode
if [[ -n "$DB_DUMP_DEBUG" ]]; then
  set -x
fi
YEAR=$(date '+%Y')
MONTH=$(date '+%m')
new_name=$(date -u +"%Y-%m-%dT%H_%M_%SZ")

# Create new directory and return new name
mkdir -p ${DB_DUMP_TARGET}/${YEAR}/${MONTH}
echo -n "${YEAR}/${MONTH}/df_api_backup_${new_name}.tar.gz"

Could you please check this script and add to repo as sample and to docker hub page? It's a simple solution, but I can't find this example. Maybe it will be helpful.

deitch commented 1 month ago

It is rather simple and elegant. I would be happy to add it. I have a few questions:

deitch commented 1 month ago

Also, have you looked at the Custom backup file name option?

Doctorf commented 1 month ago

Hi @deitch 1) I'm using next image in docker image: databack/mysql-backup:latest and it's related to https://hub.docker.com/layers/databack/mysql-backup/latest/images/sha256-fb35135e01f1746ea46dbabb6ee2f85cc1f0b42cb3c79c8718e8d87288bf288d?context=explore I'm trying to use master, but I have a problem with post scripts. Now all changes moved to target.sh script and can be working on master. But I don't test it. 2) Thanks for clarification. Yes, In my version, I need to set up only one dump target and it's local mounted directory. Script can be working with it. But it's possible to check variable or split variable for multiple targets. 3) This env variable not described in docker hub page and I see, that variable not working in mysql-backup:latest image. I will try to use, but new directory not created, Maybe need time for recheck.

deitch commented 1 month ago

but I have a problem with post scripts

Yes, I know. That fix just went through a few hours ago. Try the most recent, either pull master again or tag d6dc847c1df98a32936ff29b2007fcfa8640fb4f.

that variable not working

Yes, that probably is it. I will get a fix in the next day or so.

deitch commented 1 month ago

Did you try the custom backup filename option?

deitch commented 1 month ago

The more I think about it, the less I understand how to use the example.

There are two renaming options: source and target. The process is:

  1. Dump from database to some temp source file
  2. Upload temp source file contents to the target using the name "target name"

Source

Source renaming lets you change the name of the temporary file, i.e. the output from the actual dump. That file is temporary, and has no meaning beyond the current dump. The name - default or what you change it to - will be ignored. The only real value it has is if you want to append some data to the file, encrypt it, change it somehow. From mysqlbackup's perspective, it doesn't know or care. It just sees those 2 stages - dump and upload - and lets you change the value of it in the middle.

Target

Target doesn't do anything, ignores any local activity. The only thing it does is change what the remote name would be. So if it was "foo.tgz" by default and you want it to be "bar.tgz", mysqlbackup says, "ok, sure, I will call it that name when I push it in stage 2."

I am not convinced a script is necessary for it, but even if it is, how does knowing the target help? More to the point, making directories and such will not do anything.