JasonDaly.name

PHP, Ruby, Symfony, Rails, Doctrine, MooTools. Web Development.

Posts tagged with "shell"

August 24, 2010

Amazon S3 + Nightly Database Backups

Amazon S3 is a cost-effective, redundant, and straight-forward solution for off-site data backups. Using the highly recommended aws command line tool by Tim Kay and a simple shell script running nightly, creating an automatic remote database backup is easy.

After installing aws, I created a shell script as shown below.

#!/bin/bash
export AWS_ACCESS_KEY_ID= # your aws access key id
export AWS_SECRET_ACCESS_KEY= # your secret access key

DBUSER= # the database username
DBPASS= # the database password
DBNAME= # the name of the database
BUCKET= # the root bucket to store the backup

FILE=$DBNAME.bkp.$(date +_%b_%d_%y.%s).sql.gz
mysqldump -u $DBUSER -p$DBPASS $DBNAME | gzip -9 > /tmp/$FILE
aws put $BUCKET/bkp/sql/$FILE /tmp/$FILE --private
rm -f /tmp/$FILE # cleanup

A nightly (at 1AM) cron is then setup for the user running the above script

0 1 * * * /home/app/s3dbbkp 2>&1

This is clearly a very simple implementation, but for my current needs (and likely others) it is perfect.

Tags: shell bash cli aws amazon s3 database backup code tips

May 29, 2009

Bash Shortcuts Using the Exclamation Point

The shell provides multiple shortcuts via the usage of the exclamation point when trying to execute recently performed commands. Err the Blog’s Bash Cheat Sheet provides the following tips and more.

If you need to run the most recently executed command again

!!

If the last command run requires super user privileges

sudo !!

Run the most recently executed command beginning with foo

!foo

Retrieve the last ‘word’ of the most recently executed command. In the example below, !$ would have the value my_database.

mysqldump -u root -p my_database
!$ # Will contain the value 'my_database'

Similar to the example above, this shortcut contains all ‘words’ except the first in the most recently run command. Using the same mysqldump command above, !* would have the value -u root -p my_database.

mysqldump -u root -p my_database
!* # Will contain the value '-u root -p my_database'

Note that the above commands can have :p appended, printing the commands or ‘words’ that the exclamation point replacement would use instead of running them as part of a new command in shell. The example below would print the most recently executed command beginning with foo

!foo:p

1 note Tags: bash linux shell tips