Skip to content

Instantly share code, notes, and snippets.

@ajaxray
Last active May 21, 2023 23:49
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ajaxray/8719fe9d9f4dd4e7a37ad5f5624dd4c1 to your computer and use it in GitHub Desktop.
Save ajaxray/8719fe9d9f4dd4e7a37ad5f5624dd4c1 to your computer and use it in GitHub Desktop.
Backup MySQL database to MEGA.nz cloud storage
#!/bin/bash
# ----------------------------------------------------
# A Shell script to take MySql database backup
# and upload to MEGA cloud storage using megatools
# ----------------------------------------------------
# REQUIREMENTS : You should have -
# 1. Registered and verified MEGA account - https://mega.nz/
# 2. Installed megatools - https://github.com/megous/megatools
# ----------------------------------------------------
# This script is part of this tutorial - http://wp.me/p2nCeQ-a2
# ----------------------------------------------------
# CONFIG - Set you information here
db_user=root
db_pass=yourDbPassword
db_name=databaseName
mega_user=megaEmail@ddress
mega_pass=megaPasswrd
mega_path=/Root/DB_BACKUP
# NOTE: MEGA remote path should start with /Root/
# Create a temporary file
tmpfile=$(mktemp)
printf "Created temporary file: ${tmpfile} \n"
# Create Database dump and write to tmp file
printf "Writing database dump to tmp file..."
mysqldump -u ${db_user} -p${db_pass} ${db_name} > ${tmpfile}
printf " Done. \n"
# Upload dump file to MEGA cloud storage
printf "Uploading at ${mega_path}/${db_name}_$(date '+%Y-%m-%d-%H-%M').sql \n"
# Comment out next line if directory is already created for mega_path
megamkdir -u ${mega_user} -p ${mega_pass} ${mega_path}
megaput --no-progress -u ${mega_user} -p ${mega_pass} --path ${mega_path}/${db_name}_$(date '+%Y-%m-%d-%H-%M').sql ${tmpfile}
printf "Cleanup... "
rm ${tmpfile}
printf " Done. \n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment