Posts Tagged .sql

Backup all mysql databases to individual files

Thanks go out to Sonia Hamilton, as her blog is where I found the original version of this script, however it did need a fair bit of tweaking to work properly in BSD so I went ahead and redid parts of it  as you can see below.

This script will back up each of your databases separates into their handy dandy own .sql files and then gzip them up for you.  Enjoy

#!/bin/bash
# sonia 16-nov-05 & Ryan 16-feb-11
# backup each mysql db into a different file, rather than one big file
# as with –all-databases – will make restores easier

USER=”"
PASSWORD=”"
OUTPUTDIR=”/usr/mysqlbackup”
MYSQLDUMP=”/usr/local/bin/mysqldump”
MYSQL=”/usr/bin/mysql”

#Clean out yesterdays news
rm -rf $OUTPUTDIR/*

#Get a list of databases
databases=$(/usr/local/bin/mysql -u$USER -p$PASSWORD -e “SHOW DATABASES;” | grep
-Ev “(Database|information_schema)”)

#Dump each database in turn
for db in $databases; do
echo “Now Dumping $db”
$MYSQLDUMP -f -u$USER -p$PASSWORD $db | gzip > $OUTPUTDIR/$db.gz
done

Tags: , , , ,

Importing / Loading a Mysql backup / database from a .sql file – In Shell

I was appalled when the first page of search results for this came up with instructions on how to do this for a Windows server.  So I felt it my duty to add to the tutorials out there for people actually running a real web server (BSD/Linux/Etc)

Parse in the .sql file into an already existing DB

In shell simply issue the following command

$ mysql -u root -p db-name < backup-file.sql

To create a new DB and then import

Login to Mysql:
$ mysql -u root -p

Now create database called sales using SQL statement:

mysql> CREATE DATABASE myDB;
mysql> quit;

Now restore database, enter:
$ mysql -u root -p myDB < /path/to/your-DB-file.sql
Easy Peasy isn’t it?

Tags: , , , , , , , ,

Page optimized by WP Minify WordPress Plugin