HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to generate a dynamic file name for a mysqldump on Linux?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
filehowmysqldumpgeneratenameforlinuxdynamic

Problem

I want to schedule my MySql backs to happen on an hourly basis, but the problem is I want to customize the name of the file that is outputs but not to sure how to?

This is the command I am using now

mysqldump -u yourusername -h yourusername.mysql.pythonanywhere-services.com 'yourusername$dbname'  > db-backup.sql


This works but I would to have it output a file something like this, but for it to happen dynamically, when the command is run

1-01-18.13:50.db-backup.sql


How could I do this on a single line of code

Solution

This should do it :

mysqldump -u yourusername -h yourusername.mysql.pythonanywhere-services.com 'yourusername$dbname'  > `date +"\%Y-\%m-\%d_\%H-\%M"`.db-backup.sql


Will generate files like : 2018-01-01_13-50.db-backup.sql. You can adapt this to whatever exact format you want (but you should avoid using special characters such as :).

If you want to compress the file on the fly :

mysqldump -u yourusername -h yourusername.mysql.pythonanywhere-services.com 'yourusername$dbname'  | gzip > `date +"\%Y-\%m-\%d_\%H-\%M"`.db-backup.sql.gz

Code Snippets

mysqldump -u yourusername -h yourusername.mysql.pythonanywhere-services.com 'yourusername$dbname'  > `date +"\%Y-\%m-\%d_\%H-\%M"`.db-backup.sql
mysqldump -u yourusername -h yourusername.mysql.pythonanywhere-services.com 'yourusername$dbname'  | gzip > `date +"\%Y-\%m-\%d_\%H-\%M"`.db-backup.sql.gz

Context

StackExchange Database Administrators Q#226123, answer score: 2

Revisions (0)

No revisions yet.