Created
May 3, 2013 15:40
-
-
Save yourdesigncoza/5510031 to your computer and use it in GitHub Desktop.
It's essential to periodically BU your DB, having this done while your sitting at the beach is a bonus, fortunately it's relatively simple even for server newbie like myself.
This file contains hidden or 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
# It's essential to periodically BU your DB, having this done while your sitting at the beach is a bonus, fortunately it's relatively simple even for server newbie like myself | |
# I also wanted to keep a BU for at least 6 days, so I went about setting up a cron to BU my "Time Stamped" DB, on the eight day I start removing date -7 day's , leaving me with a couple of days DB stock ::: | |
# IMPORTANT : add your own data or parameters, I make use of double segments [[ your variable ]]. eg. ssh root@[[ 96.172.44.11 ]] should be replaced with ssh [email protected] where "888.88.88.88" is your value, variable etc. I have a habit of using ":::" to indicate line ending and end of paragraph, crazy I know but be warned its just how I write ::: All notes are for my own use & should you use any it's at your own risk, it's NOT a Tutorial ::: | |
# Resources | |
# BIG THANKS TO @chrishough >> http://noconformity.com/blog/2013/04/15/linux-server-automation-scripts/ | |
# https://help.ubuntu.com/12.04/serverguide/backup-shellscripts.html | |
# http://www.mikerubel.org/computers/rsync_snapshots/#Incremental | |
# http://www.cyberciti.biz/faq/ubuntu-linux-mysql-nas-ftp-backup-script/ | |
### Start SH Script | |
#!/bin/bash | |
#verify directory structure exists prior to running this job | |
BackUpDIR="/var/[[ yourdir ]]/databases/"; | |
Now=$(date +"%Y-%m-%d"); | |
SevenDays=$(date --date="7 day ago" +"%Y-%m-%d") | |
#Format of DBList="db1 db2 db3 db4" | |
DBList="[[ yourDB ]]"; | |
#I have a server system administrator account with access to all dbs, typically named sysadmin | |
DBUser="[[ yourUser ]]"; | |
DBPwd="[[ yourPass ]]"; | |
for DB in $DBList; | |
do | |
mysqldump --opt -u$DBUser -p$DBPwd --databases $DB > $BackUpDIR$Now.$DB.sql; | |
tar zcf "$BackUpDIR$Now.$DB.tar.gz" -P $BackUpDIR$Now.$DB.sql; | |
#No need to keep a copy of .sql as its now GZIP'd | |
rm $BackUpDIR$Now.$DB.sql; | |
#Check to see if we have a DB with todays time stamp -7 days, if true remove it | |
if [ -d $BackUpDIR$SevenDays.$DB.tar.gz ] ; then | |
rm $BackUpDIR$SevenDays.$DB.tar.gz; | |
fi; | |
done | |
### End SH Script | |
# Example Cron Schedule | |
# Backup databases @ 12:30 AM | |
# 30 00 * * * bash /var/[[ yourdir ]]/[[ thisfilename ]].sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment