-
-
Save Nomy/50d4fb373509bd51d829 to your computer and use it in GitHub Desktop.
Mega server backup using megatools
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 | |
# Script edited to make it work on Ubuntu 14.04 (Trusty) | |
# Original Author: Matteo Mattei http://www.matteomattei.com/backup-your-server-on-mega-co-nz-using-megatools/ | |
# | |
# Remember to Install megatools (sudo apt-get install megatools) after adding megous ppa | |
# You can do so by: sudo nano /etc/apt/sources.list | |
# And adding the following repositories: | |
# deb http://ppa.launchpad.net/megous/ppa/ubuntu precise main | |
# deb-src http://ppa.launchpad.net/megous/ppa/ubuntu precise main | |
# | |
# apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 77242944F711A9FD | |
# | |
# Create config with Mega account details in the root folder. e.g. /root/.megarc | |
# You can do so by: sudo nano /root/.megarc | |
# And adding the following: | |
# [Login] | |
# Username = [email protected] | |
# Password = YourMegaPassword | |
# | |
# Setup a CronJob that runs daily | |
# You can do so by: sudo crontab -e | |
# And adding the following: | |
# 0 4 * * * /root/megabackup.sh | |
# | |
SERVER="servername" | |
DAYS_TO_BACKUP=7 | |
WORKING_DIR="/root/backup_tmp_dir" | |
BACKUP_MYSQL="true" | |
MYSQL_USER="root" | |
MYSQL_PASSWORD="MyRootPassword" | |
DOMAINS_FOLDER="/var/www" | |
################################## | |
# Create local working directory and collect all data | |
rm -rf ${WORKING_DIR} | |
mkdir ${WORKING_DIR} | |
cd ${WORKING_DIR} | |
# Backup /etc folder | |
cd / | |
tar czpf ${WORKING_DIR}/etc.tgz etc | |
cd - > /dev/null | |
# Backup MySQL | |
if [ "${BACKUP_MYSQL}" = "true" ] | |
then | |
mkdir ${WORKING_DIR}/mysql | |
for db in $(mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -e 'show databases;' | grep -Ev "^(Database|mysql|information_schema|performance_schema|phpmyadmin)$") | |
do | |
#echo "processing ${db}" | |
mysqldump --opt -u${MYSQL_USER} -p${MYSQL_PASSWORD} "${db}" | gzip > ${WORKING_DIR}/mysql/${db}_$(date +%F_%T).sql.gz | |
done | |
#echo "all db now" | |
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 | |
fi | |
# Backup domains | |
mkdir ${WORKING_DIR}/domains | |
for folder in $(find ${DOMAINS_FOLDER} -mindepth 1 -maxdepth 1 -type d) | |
do | |
cd $(dirname ${folder}) | |
tar czpf ${WORKING_DIR}/domains/$(basename ${folder}).tgz $(basename ${folder}) | |
cd - > /dev/null | |
done | |
################################## | |
# Create base backup folder on Mega | |
[ -z "$(megals --reload /Root/backup_${SERVER})" ] && megamkdir /Root/backup_${SERVER} | |
# Remove older backups | |
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) | |
megarm ${TO_REMOVE} | |
done | |
# Create remote folder | |
curday=$(date +%F) | |
megamkdir /Root/backup_${SERVER}/${curday} 2> /dev/null | |
# Backup now!!! | |
megacopy --reload --no-progress -l ${WORKING_DIR} -r /Root/backup_${SERVER}/${curday} > /dev/null | |
# Clean local environment | |
rm -rf ${WORKING_DIR} | |
# Backup Script Ended. | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment