-
-
Save AHinMaine/b9efbb6e282517ae61cc63d082851d08 to your computer and use it in GitHub Desktop.
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 | |
BUCKET=ellisonmagentobackups | |
TMPDIR=/mnt/shared/tmp | |
BACKUPITEMS="app erp errors media shell var wp" | |
DATESTAMP=`date +"%Y%m%d.%H%M%S"` | |
date | |
echo "Shared backup started" | |
cd "$TMPDIR/.." | |
for ITEM in $BACKUPITEMS; do | |
date | |
echo "Compressing $ITEM..." | |
tar -zcf "tmp/backup/shared.$ITEM.$DATESTAMP.tar.gz" "$ITEM" --exclude=cache --warning=no-file-changed | |
if [[ $? -ne 0 ]]; then | |
echo "An error occured while compressing shared dir $ITEM" | |
continue | |
fi | |
FILESIZE=$(wc -c < "tmp/backup/shared.$ITEM.$DATESTAMP.tar.gz") | |
if [[ $FILESIZE -lt 5368709120 ]]; then | |
aws s3api put-object --bucket "$BUCKET" --key "shared.$ITEM.$DATESTAMP.tar.gz" --body "tmp/backup/shared.$ITEM.$DATESTAMP.tar.gz" | |
rm -f "tmp/backup/shared.$ITEM.$DATESTAMP.tar.gz" | |
continue | |
fi | |
date | |
echo "Splitting $ITEM..." | |
split --bytes=5G "tmp/backup/shared.$ITEM.$DATESTAMP.tar.gz" "tmp/backup/shared.$ITEM.$DATESTAMP.tar.gz.part." | |
if [[ $? -ne 0 ]]; then | |
echo "An error occured while splitting $ITEM" | |
continue | |
fi | |
rm -f "tmp/backup/shared.$ITEM.$DATESTAMP.tar.gz" | |
date | |
echo "Starting multipart upload for $ITEM..." | |
UPLOADID=$(aws s3api create-multipart-upload --key "shared.$ITEM.$DATESTAMP.tar.gz" --bucket "$BUCKET" | grep -Po '"UploadId":.*?[^\\]",' | perl -pe 's/"UploadId": //; s/^"//; s/",$//') | |
echo "Upload ID is $UPLOADID" | |
STRUCTURE="Parts=[" | |
PARTNO=1 | |
for PART in `find "$TMPDIR" -name "shared.$ITEM.$DATESTAMP.tar.gz.part.*" | sort`; do | |
ETAG=$(aws s3api upload-part --bucket "$BUCKET" --key "shared.$ITEM.$DATESTAMP.tar.gz" --part-number="$PARTNO" --upload-id="$UPLOADID" --body "$PART" | grep -Po '[a-z0-9]{32}') | |
if [[ $? -ne 0 ]]; then | |
echo "Part upload $PART failed" | |
else | |
echo "Uploaded part $PART" | |
fi | |
rm -f "$PART" | |
STRUCTURE="${STRUCTURE}{ETag=$ETAG,PartNumber=$PARTNO}," | |
PARTNO=`expr $PARTNO + 1` | |
done | |
STRUCTURE="${STRUCTURE}]" | |
echo "$STRUCTURE" | |
aws s3api complete-multipart-upload --key "shared.$ITEM.$DATESTAMP.tar.gz" --bucket "$BUCKET" --upload-id="$UPLOADID" --multipart-upload "$STRUCTURE" | |
done | |
date | |
echo "Done" | |
exit 0 |
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 | |
BUCKET=ellisonmagentobackups | |
TMPDIR=/mnt/shared/tmp | |
DATESTAMP=`date +"%Y%m%d.%H%M%S"` | |
date | |
echo "MySQL backup started" | |
function query() { | |
mysql -s -N -e "$1" | |
} | |
DBS=`query "SHOW DATABASES"` | |
if [[ -z "$DBS" ]]; then | |
echo "No databases were found" | |
exit 1 | |
fi | |
mkdir -p "$TMPDIR/backup" | |
cd "$TMPDIR/backup" | |
for DB in `echo "$DBS"`; do | |
date | |
echo "Backing up $DB" | |
mysqldump "$DB" --force --triggers --single-transaction --opt --skip-lock-tables > "$TMPDIR/backup/$DB.$DATESTAMP.sql" | |
date | |
echo "Compressing $DB..." | |
gzip "$TMPDIR/backup/$DB.$DATESTAMP.sql" | |
if [[ $? -ne 0 ]]; then | |
echo "An error occured while compressing $DB" | |
continue | |
fi | |
date | |
echo "Uploading $DB..." | |
aws s3api put-object --bucket "$BUCKET" --key "$DB.$DATESTAMP.sql.gz" --body "$TMPDIR/backup/$DB.$DATESTAMP.sql.gz" | |
if [[ $? -ne 0 ]]; then | |
echo "An error occured while uploading $DB to S3" | |
continue | |
fi | |
rm -f "$TMPDIR/backup/$DB.$DATESTAMP.sql.gz" | |
done | |
date | |
echo "Done" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment