Putting date and time in a backup in file . sh

Asked

Viewed 293 times

0

I’m using a script to back up my database that I have in Mysql, I’m wondering how to put the date and time backup instead of just the date:

He gets like this:

alex_banco-2019-05-28.sql

But I wanted it to stay that way:

alex_banco-2019-05-28:13:18:51.sql

It would be possible to make that kind of change?

 Definindo parametros do MySQL
echo "  -- Definindo parametros do MySQL ..."
DB_NAME='atrizes'
DB_USER='alex'
DB_PASS='123456'
DB_PARAM='--add-drop-table --add-locks --extended-insert --single-transaction -quick'

# Definindo parametros do sistema
echo "  -- Definindo parametros do sistema ..."
DATE=`date +%Y-%m-%d`
MYSQLDUMP=/usr/bin/mysqldump
BACKUP_DIR=/home/alex/backup
BACKUP_NAME=alex_banco-$DATE.sql
BACKUP_TAR=ales_banco-$DATE.tar

#Gerando arquivo sql
echo "  -- Gerando Backup da base de dados $DB_NAME em $BACKUP_DIR/$BACKUP_NAME ..."
$MYSQLDUMP $DB_NAME $DB_PARAM -u $DB_USER -p$DB_PASS > $BACKUP_DIR/$BACKUP_NAME

# Compactando arquivo em tar
echo "  -- Compactando arquivo em tar ..."
tar tar -cjf $BACKUP_DIR/$BACKUP_TAR -C $BACKUP_DIR $BACKUP_NAME --remove-files

# Excluindo backups antigos
echo " -- Excluindo backups com mais de 30 dias ..."
find $BACKUP_DIR/* -mtime +30 -delete
  • 1

    This is not a question concerning Mysql but the shell.

2 answers

2


Removing the formatting specifiers from manpage of date.

FORMAT controls the output.  Interpreted sequences are:
%F     full date; same as %Y-%m-%d 
%H     hour (00..23)
%M     minute (00..59)
%S     second (00..60)
%T     time; same as %H:%M:%S 

That translated (by me):

FORMATO controla a saída. As sequências interpretadas são:
%F     data completa; o mesmo que %Y-%m-%d
%H     hora (00..23)
%M     minuto (00..59)
%S     segundo (00..60)
%T     tempo; mesmo que %H:%M:%S 

So you can try:

DATE=`date +%Y-%m-%d:%T`

or

DATE=`date +%F:%T`

or

DATE=`date +%Y-%m-%d:%H:%M:%S`

Use man date at your terminal for more information.

0

Try to use it like this:

DATE=`date +%Y-%m-%d:%H:%I:%M`

Browser other questions tagged

You are not signed in. Login or sign up in order to post.