Reply to topic
Automatic Back-Up? /MySQL
discogeek


Joined: 13 Apr 2007
Posts: 4
Location: Washington, DC
Reply with quote
Anyone know of a utility that would automatically back-up the MySQL databases on my dedicated server? I've been doing it by hand with phpAdmin whenever I remember...
dbodner


Joined: 21 Dec 2005
Posts: 112
Location: Philadelphia, Pa
Reply with quote
You could setup a cron job that runs the following every day:
#!/bin/sh

mysqldump -Q -u username -ppassword --all-databases > alldbbackup.sql


That'll dump all databases in one .sql file, so it's not the easiest to restore (it's also not compressed, although I'm pretty sure that's an option in mysqldump, or you could just gzip the alldbbackup.sql).

I wrote a quick and dirty script that does the job. It'll go through, dump all databases to their own .sql file, then tar and gzip them all up into one .tar.gz. So that this way you can easily extract the one .sql file to restore it.

Code:

#!/bin/sh

# MySQL Hostname
DBHOST='localhost'

# MySQL Username
DBUSER='root'

# MySQL Password
DBPASSWD='yourpass'

# Local Directory for Dump Files
LOCALDIR=/backups/sql
DUMPDIR=/backups/tmp
# user/group backups should be owned by
USERCHMOD='backups'

cd $LOCALDIR
SUFFIX=`date +%Y-%m-%d`
FILENAMETAR=$SUFFIX.sqlbackups.tar.gz

if [ ! -e $DUMPDIR]; then
mkdir $DUMPDIR
fi

DBS=`mysql -u$DBUSER -p$DBPASSWD -h$DBHOST -e"show databases"`

for DATABASE in $DBS
do
if [ $DATABASE != "Database" ]; then
FILENAMESQL=$SUFFIX-mysql-$DATABASE.sql
mysqldump -u$DBUSER -h$DBHOST $DATABASE > $DUMPDIR/$FILENAMESQL
fi
done

cd $DUMPDIR
tar -czf $LOCALDIR/$FILENAMETAR *
rm -f $DUMPDIR/*
chmod 600 $LOCALDIR/$FILENAMETAR
chown -R $USERCHMOD.$USERCHMOD $LOCALDIR
exit 0


*notes*
$LOCALDIR is the directory the backups will be stored in (the final .tar.gz's)
$DUMPDIR is the temporary directory the .sql files will be in. When the script is done, this will be empty.
$USERCHMOD is the user that will own the backups when they've been completed. This user must exist on the system.

If it's a cPanel system, you can likely keep the password blank.
If it's a Plesk system, the root user should be changed to admin, and the password can be `cat /etc/psa/.psa.shadow`

I'm not saying this is the best or only way to do it. This isn't a script I wrote to be mass-distributed and used on all systems. It's literally just something I wrote for my personal system
tedjtw


Joined: 08 Feb 2005
Posts: 81
Location: Connecticut
Reply with quote
Try MySQLYOG, the commercial version. It is inexpensive. You can setup schedules on your local PC or server and have the backups run as needed. You can also have a separate file per database table. We use this with HMS for a client who requires a nightly backup to their own server.

HTH
Automatic Back-Up? /MySQL
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic