Created
February 27, 2012 16:09
-
-
Save julien-duponchelle/1925021 to your computer and use it in GitHub Desktop.
Dotcloud backup to S3 improved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Improved version of http://docs.dotcloud.com/guides/backups/ | |
# | |
HOSTNAME=`hostname` | |
TAG="${HOSTNAME}_$(TZ=UTC date +%Y-%m-%d_%H:%M:%S_UTC)" | |
[ "$3" ] || { | |
echo "Please specify what to backup, how, and where." | |
exit 1 | |
} | |
case "$1" in | |
mysql) | |
FILENAME="$TAG.sql.gz" | |
FILEPATH="/tmp/$FILENAME" | |
mysqldump --all-databases | gzip > "$FILEPATH" | |
;; | |
pgsql) | |
FILENAME="$TAG.sql.gz" | |
FILEPATH="/tmp/$FILENAME" | |
pg_dumpall | gzip > "$FILEPATH" | |
;; | |
riak) | |
FILENAME="$TAG.bitcask.tar.gz" | |
FILEPATH="/tmp/$FILENAME" | |
tar -czf "$FILEPATH" /var/lib/riak | |
;; | |
data) | |
FILENAME="$TAG.data.tar.gz" | |
FILEPATH="/tmp/$FILENAME" | |
tar -C "$HOME" -czf "$FILEPATH" "data" | |
;; | |
redis) | |
FILENAME="$TAG.redis.tar.gz" | |
FILEPATH="/tmp/$FILENAME" | |
tar -C "$HOME" -czf "$FILEPATH" "dump.rdb" | |
;; | |
*) | |
echo "Sorry, I don't know how to backup $1." | |
exit 1 | |
;; | |
esac | |
if [ "$4" ] | |
then | |
DEST="$4/$FILENAME" | |
else | |
DEST="$FILENAME" | |
fi | |
case "$2" in | |
ssh) | |
scp -q -o BatchMode=yes "$FILEPATH" "$3:$DEST" | |
;; | |
ftp) | |
curl -sST "$FILEPATH" "ftp://$3/$DEST" | |
;; | |
s3) | |
s3cmd put "$FILEPATH" "s3://$3/$DEST" | |
;; | |
s3multi) | |
split --numeric-suffixes --bytes=4G "$FILEPATH" "$FILEPATH". | |
s3cmd put "$FILEPATH".?? "s3://$3/$DEST/" | |
;; | |
*) | |
echo "Sorry, transfer method $2 is not supported." | |
exit 1 | |
esac | |
SIZE="$(stat --printf %s "$FILEPATH")" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
[ "$2" ] || { | |
echo "Please specify a S3 bucket and service type." | |
exit 1 | |
} | |
s3cmd --configure | |
curl https://raw.github.com/gist/1925021/779bc84d899a428a1cd9c3b7ef718dbfbe02bde6/backup.sh > backup.sh | |
chmod 700 backup.sh | |
./backup.sh $1 s3multi $2 | |
crontab - <<EOF | |
MAILTO="" | |
$[$RANDOM%60] $[$RANDOM%24] * * * ~/backup.sh $1 s3multi $2 | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment