Skip to content

Instantly share code, notes, and snippets.

@ghostdevv
Last active March 17, 2021 21:27
Show Gist options
  • Save ghostdevv/c287b7f0cfa6b5ea96e22767a964e1d4 to your computer and use it in GitHub Desktop.
Save ghostdevv/c287b7f0cfa6b5ea96e22767a964e1d4 to your computer and use it in GitHub Desktop.
Auto mysql backup bash script

Auto backup system

This script can be ran to backup a mysql database to cloud storage

Requirements

  • Node JS (Latest LTS is good, tested in 14.x)
  • MariaDB (MySQL)

Setup

I use nano, alternatives are avalible. Paste the script below into the file when nano or your editor of choice opens and save (after adding in your information in missing areas)

npm i -g linode-object-upload
mkdir /backups
nano /backups/mkbackup.sh

Cron

The below cron job will run every six hours, you are able to edit. Try searching for cron calculators if you don't know the syntax.
To open crontab do sudo crontab-e then add the following line

10 */6 * * * /bin/bash /backups/mkbackup.sh
DIR="/backups"
BUCKET=""
CLUSTER=""
KEY=""
SECRET=""
USER="root"
PASSWORD=""
FILE=$(date +"%d-%m-%Y-at-%H-backup.gz")
cd $DIR
sudo mysqldump -u $USER -p$PASSWORD --all-databases | gzip > $FILE
linode-object-upload $FILE $BUCKET $CLUSTER -key $KEY -secret $SECRET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment