0

I run the following script:

#!/bin/bash
{
date=$( date '+%Y-%m-%d_%H-%M-%S' )
file="mysql_dump_$date"

echo '###Start DB Backup '$date'###'
        mysqldump -h localhost -u 'USERNAME' -pPASSWD 'DBNAME' > /backup/$file
echo '###DB Backuo Finished '$date'###'
} >> /backup/backup.log

As you can see in the mysqldump line, $file will be named mysql_dump_date

I would like to cp the file somewhere else but don't know how I can specify to use the $file which is used in the mysqldump line. Obviously, I could just used $file but if the DB backup starts at 10:49:59 and the cp will start at 10:50:00, it won't have the same timestamp and wouldn't be able to find the file and the copy would fail.

Any ideas?

0

1 Answer 1

2

Perhaps I'm misunderstanding your doubt. You seem to think that $file will change its value if it is used a second time.

Bash variables don't change their values. $date and $file are set once in your script, and then they keep the same value until they disappear at the end of the script (unless they are reassigned). So you can just use $file again, and it will have the same value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.