Skip to content

Instantly share code, notes, and snippets.

@rtrudel
Last active November 10, 2016 04:32
Show Gist options
  • Save rtrudel/7a457453c82d0070fd1a7ca947b6597c to your computer and use it in GitHub Desktop.
Save rtrudel/7a457453c82d0070fd1a7ca947b6597c to your computer and use it in GitHub Desktop.
Backup Craft CMS website to Google Drive
#!/bin/sh
# REQUIREMENTS
# gdrive - Google Drive CLI Client at https://github.com/prasmussen/gdrive
# cronjon creation ability
# GENERAL
#File prefix
PREFIX="backup-"
# MYSQL SETTINGS
#MySQL username
MYSQLUSER="mysqluser"
#MySQL password
MYSQLPASS="MyDbPaSs"
#MySQL database name
MYSQLDB="dbname"
# CRAFT WEBSITE FILES PATHS
#Craft path (absolute with trailing slash)
CRAFTPATH="/home/user/craft/"
#Public path (absolute without trailing slash)
PUBLICPATH="/home/user/public_html"
# GDRIVE UTILITY
# gdrive program path
GDRIVEPATH="/bin/"
# gdrive config path
GDRIVECONFIG="/home/.gdrive"
# GOOGLE DRIVE SETTINGS
# Google Drive parent folder id (ex: 0B1A6QQPSDf-LdFd4eEgwZW1aTTz for https://drive.google.com/drive/folders/0B1A6QQPSDf-LdFd4eEgwZW1aTTz)
GDRIVEPARENT="0B1A6QQPSDf-LdFd4eEgwZW1aTTz"
# Maximum files in the folder for backup rotation (Warning: Use a dedicated folder for backups, do not store anything else to avoid accidental deletion!)
GDRIVEMAXFILES=10
# ****************************************************************************
#DO NOT EDIT BELOW UNLESS YOU KNOW WHAT YOU ARE DOING
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
# MySQL dump
echo -e "${GREEN}Generating MySQL dump file${NC}"
mysqldump --user=$MYSQLUSER --password=$MYSQLPASS --default-character-set=utf8 $MYSQLDB > "database.sql"
echo -e "${GREEN}MySQL dump file created"
# Compress files to tar file
echo -e "${GREEN}Preparing archive${NC}"
tar -zcf "${PREFIX}$(date '+%Y-%m-%d').tar.gz" ${CRAFTPATH}config ${CRAFTPATH}plugins ${CRAFTPATH}templates ${PUBLICPATH} database.sql
echo -e "${GREEN}Archive created successfuly${NC}"
# Upload to Google Drive
echo -e "${GREEN}Upload to Google Drive${NC}"
${GDRIVEPATH}gdrive --config "${GDRIVECONFIG}" upload --parent ${GDRIVEPARENT} --delete "${PREFIX}$(date '+%Y-%m-%d').tar.gz"
# Removing MySQL dump file
echo -e "${GREEN}Removing MySQl dump file${NC}"
rm -rf "database.sql"
# Keep only last 10 files in the Google Drive folder
echo -e "${GREEN}Rotating backups${NC}"
files=$(GDRIVEPATH}gdrive --config "${GDRIVECONFIG}" list --order "createdTime desc" --no-header --query "'${GDRIVEPARENT}' in parents and trashed = false and 'me' in owners" | awk {'print $1'})
c=0;
IFS="
"
for i in $files
do
c=$((c + 1))
if [ "$c" -gt "$GDRIVEMAXFILES" ]; then
$(GDRIVEPATH}gdrive --config "${GDRIVECONFIG}" delete "$i"
fi
done <<EOF
"$files"
EOF
echo -e "${RED}Done!${NC}"
# HOW TO SET YOUR CRONJOB
# nano /etc/crontab
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
# 0 3 * * * root /path/to/craft-backup.sh > /dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment