user1177292
user1177292

Reputation: 273

Using a variable in shell script and running as a cron job

I have the following script dbbackup.sh

mysqldump -P 3306 -h 436543545.amazonaws.com -u admin--password=goaway mydb > dump-1.sql
s3cmd put dump-1.sql s3://database-blah-bucket
rm -f -r dump-1.sql

I need to make the following changes

  1. The file to be in the following format (dump-1.sql) is to be dump-dd-mm-yyy-hh-mm.sql
  2. Running the script by sh dbbackup.sh which I need to run as a cron job everyday at 3am

Completely new to UNIX so not sure how to do above

Upvotes: 0

Views: 358

Answers (2)

Geoff Montee
Geoff Montee

Reputation: 2597

Contents of dbbackup.sh:

#!/bin/bash

HOST="436543545.amazonaws.com"
PORT=3306
USER="admin"
PASSWORD="goaway"
DATABASE="mydb"

DATE_STR=$(date +%d-%m-%Y-%H-%M)

FILENAME="dump-${DATE_STR}.sql

mysqldump -P $PORT -h $HOST -u $USER --password=${PASSWORD} $DATABASE > $FILENAME
s3cmd put $FILENAME s3://database-blah-bucket
rm -f -r $FILENAME

Then go into crontab using crontab -e. Enter:

0 3 * * *  /path/to/dbbackup.sh

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185073

You just need to use date command, see the following shell code :

dump="dump-$(date +%d-%m-%Y-%H-%M).sql"
mysqldump -P 3306 -h 436543545.amazonaws.com -u admin--password=goaway mydb > "$dump"
s3cmd put "$dump" s3://database-blah-bucket
rm -f -r "$dump"

Upvotes: 2

Related Questions