Skip to content

Instantly share code, notes, and snippets.

@chales
Last active March 7, 2019 15:08
Show Gist options
  • Save chales/1885f27bb9ab63dd24e9 to your computer and use it in GitHub Desktop.
Save chales/1885f27bb9ab63dd24e9 to your computer and use it in GitHub Desktop.
Based on script from http://getlevelten.com/blog/randall-knutson/yet-another-simple-amazon-s3-backup-script-drupal Requires the s3cmd library (apt-get install s3cmd, s3cmd --configure) http://s3tools.org/s3cmd
#!/bin/bash
# Requires s3cmd be installed, http://s3tools.org/s3cmd
echo 'Enter the database name you would like to backup:'
read DBNAME
# Setup vars
TMP="/tmp"
DBUSER="root"
DBPASS="password"
DBHOST="localhost"
S3_BUCKET="s3://bucket-name"
DATE=$(date +%Y-%m-%d)
MYSQL=$(which mysql)
MYSQLDUMP=$(which mysqldump)
# Dump the table schema first.
TABLES=`${MYSQL} --skip-column-names -e 'SHOW TABLES' -u ${DBUSER} -p ${DBPASS} ${DBNAME}`
$MYSQLDUMP --single-transaction --no-data -u $DBUSER -h $DBHOST -p$DBPASS --opt $DBNAME $TABLES > $TMP/$DBNAME-$DATE
# Dump data but skip cache and other temporary table data.
TABLES2=`echo "$TABLES" | grep -Ev "^(*._cache|accesslog|cache|cache_.*|flood|search_.*|semaphore|sessions|watchdog)$"`
$MYSQLDUMP --complete-insert --disable-keys --single-transaction --no-create-info --u $DBUSER -h $DBHOST -p$DBPASS $DBNAME $TABLES2 >> $TMP/$DBNAME-$DATE
# Gzip everything
gzip -v $TMP/$DBNAME-$DATE;
# Upload to Amazon S3
s3cmd put $TMP/$DBNAME-$DATE.gz $S3_BUCKET/databases/$DBNAME-$DATE.gz;
# Remove the temporary dump file.
rm $TMP/$DBNAME-$DATE.gz;
echo 'Backup complete, '
echo "$S3_BUCKET/databases/$DBNAME-$DATE.gz"
#!/bin/bash
# Requires s3cmd be installed, http://s3tools.org/s3cmd
# Setup vars
TMP="/tmp"
SITES_DIR="/var/www/site"
S3_BUCKET="s3://bucket-name"
DATE=$(date +%Y-%m-%d)
# Backup all files to S3
cd $SITES_DIR;
for DIR in $(find "$SITES_DIR" -mindepth 1 -maxdepth 1 -type d);
do
# Tar and Gzip each directory
BASE=$(basename "$DIR");
tar -czf $TMP/$BASE.tar.gz $BASE;
# Upload to Amazon S3
s3cmd put $TMP/$BASE.tar.gz $S3_BUCKET/sites/$BASE-$DATE.tar.gz;
# Cleanup
rm $TMP/$BASE.tar.gz;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment