Skip to content

Instantly share code, notes, and snippets.

@willjobs
Forked from matteomattei/megabackup.sh
Last active October 1, 2017 10:14
Show Gist options
  • Save willjobs/ca292c3d0b62d7e6c555cf384dfb210d to your computer and use it in GitHub Desktop.
Save willjobs/ca292c3d0b62d7e6c555cf384dfb210d to your computer and use it in GitHub Desktop.
Mega server backup using megatools (revised for updated megatools, serverpilot app locations, and to include timestamps)
#!/bin/bash
now=$(date +"%T")
echo "Starting backup at ${now}..." | tee -a /root/megabackup.log
SERVER="servername"
DAYS_TO_BACKUP=7
WORKING_DIR="/root/backup_tmp_dir"
BACKUP_MYSQL="true"
MYSQL_USER="root"
MYSQL_PASSWORD="MyRootPassword"
DOMAINS_FOLDER="/srv/users/serverpilot/apps"
##################################
# Create local working directory and collect all data
rm -rf ${WORKING_DIR} | tee -a /root/megabackup.log
mkdir ${WORKING_DIR} | tee -a /root/megabackup.log
cd ${WORKING_DIR}
# Backup /etc folder
cd /
tar cJf ${WORKING_DIR}/etc.tar.gx etc | tee -a /root/megabackup.log
cd - > /dev/null
# Backup MySQL
if [ "${BACKUP_MYSQL}" = "true" ]
then
mkdir ${WORKING_DIR}/mysql | tee -a /root/megabackup.log
for db in $(mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e 'show databases;' | grep -Ev "^(Database|mysql|information_schema|performance_schema|phpmyadmin)$")
do
now=$(date +"%T")
echo "processing ${db} at ${now}..." | tee -a /root/megabackup.log
mysqldump --opt -u${MYSQL_USER} -p${MYSQL_PASSWORD} "${db}" | gzip > ${WORKING_DIR}/mysql/${db}_$(date +%F_%T).sql.gz | tee -a /root/megabackup.log
now=$(date +"%T")
echo "done at ${now}." | tee -a /root/megabackup.log
done
mysqldump --opt -u${MYSQL_USER} -p${MYSQL_PASSWORD} --events --ignore-table=mysql.event --all-databases | gzip > ${WORKING_DIR}/mysql/ALL_DATABASES_$(date +%F_%T).sql.gz | tee -a /root/megabackup.log
fi
# Backup domains
mkdir ${WORKING_DIR}/domains | tee -a /root/megabackup.log
for folder in $(find ${DOMAINS_FOLDER} -mindepth 1 -maxdepth 1 -type d)
do
now=$(date +"%T")
echo "processing ${folder} at ${now}..." | tee -a /root/megabackup.log
cd $(dirname ${folder})
tar cJf ${WORKING_DIR}/domains/$(basename ${folder}).tar.xz $(basename ${folder}) | tee -a /root/megabackup.log
cd - > /dev/null
now=$(date +"%T")
echo "done at ${now}" | tee -a /root/megabackup.log
done
##################################
# Create base backup folder
[ -z "$(megals --reload /Root/backup_${SERVER})" ] && megamkdir /Root/backup_${SERVER} | tee -a /root/megabackup.log
# Remove old logs
while [ $(megals --reload /Root/backup_${SERVER} | grep -E "/Root/backup_${SERVER}/[0-9]{4}-[0-9]{2}-[0-9]{2}$" | wc -l) -gt ${DAYS_TO_BACKUP} ]
do
TO_REMOVE=$(megals --reload /Root/backup_${SERVER} | grep -E "/Root/backup_${SERVER}/[0-9]{4}-[0-9]{2}-[0-9]{2}$" | sort | head -n 1) | tee -a /root/megabackup.log
megarm ${TO_REMOVE} | tee -a /root/megabackup.log
done
# Create remote folder
curday=$(date +%F)
megamkdir /Root/backup_${SERVER}/${curday} | tee -a /root/megabackup.log
# Backup now!!!
megacopy --reload --no-progress -l ${WORKING_DIR} -r /Root/backup_${SERVER}/${curday} | tee -a /root/megabackup.log
now=$(date +"%T")
echo "done at ${now}" | tee -a /root/megabackup.log
# Clean local environment
rm -rf ${WORKING_DIR}
now=$(date +"%T")
echo "Finished backup at ${now}" | tee -a /root/megabackup.log
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment