Databases / mysql

Mysql DB create and Grant User
--------------
Four commands to create a mysql Database, create user and grant privileges on the created DB

**Replace dbname, username and host with your own values:**

1. login to mysql (entering root password)

mysql -u root -p

2. Create DB

create database dbname;

3. Create user and grant privileges on the new DB. Remember the single quotes! And change the host if required

GRANT ALL PRIVILEGES ON dbname.* To 'username'@'localhost' IDENTIFIED BY 'superstrongpassword';

4. Flush privileges

FLUSH PRIVILEGES;

Done :)

** Mysql commands all in one line:**

mysql> create database dbname; GRANT ALL PRIVILEGES ON dbname.* To 'username'@'host' IDENTIFIED BY 'superstrongpassword'; FLUSH PRIVILEGES;

Bash - Backup and restore local mysql DB
--------------

#Backup
mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql

#Restore
mysql -u root -p[root_password] [database_name] < dumpfilename.sql

Cron job to backup mysql DB at 3:30am every day
--------------

30 3 * * * mysqldump -u root -pPASSWORD dbname | gzip > /path/to/file/dbname_`date '+\%Y-\%m-\%d'`.sql.gz

Generic Bash script to create incremental and full mysql databases backups.
I have highlighted the lines that needs modifications according each case scenario.
--------------

#!/bin/sh
# System + MySQL backup script
# Full backup day - Sun (rest of the day do incremental backup)
# Copyright (c) 2005-2006 nixCraft <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# Automatically generated by http://bash.cyberciti.biz/backup/wizard-ftp-script.php
# ---------------------------------------------------------------------
### System Setup ###
DIRS="/path/to/bup/dir"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
INCFILE="/path/to/bup/dir/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sun"

### MySQL Setup ###
MUSER="mysqlUser"
MPASS="mysqlPassword"
MHOST="mysqlHost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

### FTP server Setup ###
FTPD="/path/to/incremental/bup/dir"
FTPU="ftpUser"
FTPP="ftpPassword"
FTPS="ftpHost"
NCFTP="$(which ncftp)"

### Other stuff ###
EMAILID="emailadress@emailhost"

### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
### See if we want to make a full backup ###
if [ "$DAY" == "$FULLBACKUP" ]; then
  FTPD="/path/to/full/bup/dir"
  FILE="fs-full-$NOW.tar.gz"
  tar -zcvf $BACKUP/$FILE $DIRS
else
  i=$(date +"%Hh%Mm%Ss")
  FILE="fs-i-$NOW-$i.tar.gz"
  tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS
fi

### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
 FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

### Dump backup using FTP ###
#Start FTP backup using ncftp
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF

### Find out if ftp backup failed or not ###
if [ "$?" == "0" ]; then
 rm -f $BACKUP/*
else
 T=/tmp/backup.fail
 echo "Date: $(date)">$T
 echo "Hostname: $(hostname)" >>$T
 echo "Backup failed" >>$T
 mail  -s "BACKUP FAILED" "$EMAILID" <$T
 rm -f $T
fi

 

Tags: