PDA

View Full Version : Root users should make at least mysql backups regularly



krakjoe
August 25th, 2007, 06:30
#!/bin/bash
############################################################ ###############
# Backup mySQL databases with mysqldump
# Set MYSQL_LIB_LOCATION and MYSQL_BACKUPS_LOCATION
# to proper locations, if you're not sure do
# ls /var/lib/mysql
# and see if you recognize any of the folder names as
# names of databases
# When the script is finished executing you will have a
# backup of every database on the server in your
# MYSQL_BACKUPS_LOCATION, backups are compressed with gzip
# BE SURE THAT MYSQL_BACKUPS_LOCATION ACTUALLY EXISTS BEFORE
# YOU ATTEMPT TO RUN THIS SCRIPT
# Written by krakjoe, in 20 seconds - to save me some time
############################################################ ###############

MYSQL_LIB_LOCATION="/var/lib/mysql"
MYSQL_BACKUPS_LOCATION="/backup/mysql"

############################################################ ###############

COUNTER=0

for DATABASE in `ls $MYSQL_LIB_LOCATION`;
do
if [ `stat -c %F $MYSQL_LIB_LOCATION/$DATABASE` = "directory" ] ; then
echo Backing up $DATABASE
`mysqldump --database $DATABASE | gzip > $MYSQL_BACKUPS_LOCATION/$DATABASE.gz`
echo Dumped $MYSQL_BACKUPS_LOCATION/$DATABASE.gz
COUNTER=$(($COUNTER + 1))
fi
done

echo Backed up $COUNTER databases to $MYSQL_BACKUPS_LOCATION

############################################################ ###############


Make sure you read the top part and do as it says
Upload this script as "runbackup.sh"
Login to shell and run
chmod a+x runbackup.sh

Whenever you want to backup run
./runbackup.sh or
sh runbackup.sh

If like me you login to shell every day then you might add something like



sh /backup/runbackup.sh


to /root/.bashrc so you backup onlogin ...

I'm not sure that a crontab will work, might be worth a go but I expect that crontab runs as a different user, I'm sorta drawing a blank ... to be sure, only use this code as root and create the directories that you're saving in as root ....

serverorigin
August 27th, 2007, 01:16
Nice work!

RadixHosting
August 29th, 2007, 17:58
Thanks, clipped it to my blog (http://www.securedminds.net/?p=17) if you don't mind. :)

Rugmuncher
September 2nd, 2007, 02:40
nice :) fixed a bug

RadixHosting
September 2nd, 2007, 09:23
nice :) fixed a bug

It would be great if you'd share that bugfix. :idea:

Gamer4u
September 2nd, 2007, 11:39
create the file in /etc/cron.daily/ for automagicness...

krakjoe
September 2nd, 2007, 12:50
does that work ?? I never got around to testing, because I think you would need to pass your root username and password to mysqldump - doesn't cron run as a different user ??

RadixHosting
September 2nd, 2007, 13:03
does that work ?? I never got around to testing, because I think you would need to pass your root username and password to mysqldump - doesn't cron run as a different user ??

You have to configure that in the /etc/crontab file. By default the cron.daily jobs get executed as root.

krakjoe
September 2nd, 2007, 13:13
sweet, there ya go then .... do that, no reason not too ...