Over a million developers have joined DZone.

A Simple MySQL Daily Backup Script

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

Using the MySQL export script I've showed from the last post (assuming you saved it in a file named "$HOME/database-export.sh"), you may now perform a daily backup with your crontab service like this.

#file: mysql-backups.sh
DIR=`dirname $0`
echo "Backup DB with export script."
$DIR/database-export.sh

echo "Moving exported files into backup dir."
DB_BAK_DIR=$HOME/mysql-backups/`date +%a`
echo "Removing old file (if exists) and saving new backup into $DB_BAK_DIR"
if [[ -e $DB_BAK_DIR ]]; then
  rm -fv $DB_BAK_DIR/*
else
  mkdir -p $DB_BAK_DIR
fi
cp -v $DIR/*.sql $DB_BAK_DIR

This script should create a daily folder under your home directory, for example like "$HOME/mysql-backups/Mon", "Tue", "Wed" etc. It should save up to 7 days in a week if you run this with a daily cron job.

# crontab -e
@daily $HOME/mysql-backups.sh > /dev/null 2>&1

This is not the most robust way of backing up your DB, but it's a simple solution if you just want something quick up and running without worry too much.


Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Zemian Deng, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}