I am attempting to write a Bash script that performs a mysqldump on my live site's database, then adds and commits the dump to a Git repository. Here is what I have so far (stored in a .sh file which is called by a crontab entry):
/usr/bin/mysqldump --skip-comments --skip-dump-date -u [user] -p[pass] database | gzip > /var/www/site/backup/database.sql.gz cd var/www/site/backup && git add * cd var/www/site/backup && git commit -m 'Database $(date +%a %H:%M %h %d %Y)'
My crontab entry looks like this:
0,20,40 8-22 * * * /var/www/site/backup/script.sh
I can see that this script does dump the database, but does not add or commit the file to Git. Is there something that I am missing?
Later I made the following changes and the commit works:
cd /var/www/site/backup && /usr/bin/git add * cd /var/www/site/backup && /usr/bin/git commit -m 'Database $(date +%a %H:%M %h %d %Y)'
However, the date does not get calculated.
Latest revisions, including (most of) the recommendations:
/usr/bin/mysqldump --skip-comments --skip-dump-date -u [user] -p[pass] database > /var/www/site/backup/database.sql cd var/www/site/backup /usr/bin/git add * /usr/bin/git commit -m "Internal Forms Live Database Dump Stored $(date '+%a %H:%M %h %d %Y')"
$() Command Substitution According to the official GNU Bash Reference manual: “Command substitution allows the output of a command to replace the command itself.
$(...) is an expression that starts a new subshell, whose expansion is the standard output produced by the commands it runs. This is similar to another command/expression pair in bash : ((...)) is an arithmetic statement, while $((...)) is an arithmetic expression. Follow this answer to receive notifications.
$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails.
$(...)
and other forms of substitutions are not interpolated in single-quoted strings.
So if you want your date calculated, do
git commit -m "Database $(date '+%a %M:%H %h %d %Y')"
that is, the whole message string is double-quoted to allow $(...)
to be interpolated while the argument to date
is in single quotes to make it a single argument (passed to date
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With