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 ....
############################################################ ###############
# 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 ....