Skip to content

Instantly share code, notes, and snippets.

@pjmazenot
Created August 8, 2019 15:01
Show Gist options
  • Save pjmazenot/00a7551f6baea59198dcc9a3b2b96d7f to your computer and use it in GitHub Desktop.
Save pjmazenot/00a7551f6baea59198dcc9a3b2b96d7f to your computer and use it in GitHub Desktop.
Simple website and DB backup - Generates a tar.gz archive including the website files and a database dump
#!/bin/sh
# Simple website and DB backup
# Generates a tar.gz archive including the website files and a database dump
# /!\ DO NOT USE IF YOU HAVE A FOLDER NAMED "database" IN YOUR WEBSITE ROOT FOLDER
echo ""
echo "Starting backup process..."
echo ""
# Destination of the backup file
TMP_DIR=/tmp/backups/
# Date (use to generate the file name)
CURRENT_DATE=`date +%y%m%d`
# Project name (use to generate the file name)
PROJECT_NAME=
# Absolute path to the website root folder
PROJECT_DIR=
# Database credentials
DB_HOST=
DB_NAME=
DB_USER=
DB_PASS=
echo "Creating temp directories..."
mkdir -p ${TMP_DIR}
mkdir -p ${PROJECT_DIR}/database
echo "Backing up database..."
mysqldump --host ${DB_HOST} -u ${DB_USER} -p${DB_PASS} ${DB_NAME} > ${PROJECT_DIR}/database/${DB_NAME}_${CURRENT_DATE}.sql
echo "Creating the archive..."
cd ${PROJECT_DIR}
tar czf ${TMP_DIR}/backup_${PROJECT_NAME}_${CURRENT_DATE}.tar.gz . --xform='s!^\./!!'
echo "Removing database temp directory..."
rm -rf ${PROJECT_DIR}/database
echo ""
echo "Backup complete. You can find it in ${TMP_DIR}"
echo ""
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment