Страница 1 из 1

[MySQL] Снимаем дампы и заливаем на фтп сервер

Добавлено: 19 фев 2011, 11:09
Gen1us2k
Вот такой не большой скрипт, написан мной, путем взаимствования некоторых идей с 2х скриптов.
Снимает дампы и заливает на фтп сервак

Код: Выделить всё

#!/bin/bash
# Shell script to backup MySql database
# To backup Nysql databases file to /backup dir and later pick up by your
# script. You can skip few databases from backup too.
# 
# Linux bin paths, change this if it can not be autodetected via which command
# Requirements:
# ncftp 3 client

# Binaries
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
NCFTP="$(which ncftpput)"
TAR="/bin/tar"  # must be gnu tar

# Your Data
MUSER="username"     # USERNAME
MPASS="password"       # PASSWORD
MHOST="localhost"          # Hostname
FTPH="ftp.backup.com" # Remote ftp user name
FTPU="ftpusername" # Remote ftp user password
FTPP="ftppassword" # Remote directory, blank for default remote dir
# If dir does not exist it will be created automatically by ncftpput :)
FTPD="backup/$(date +"%d-%m-%Y")" # Backup Dest directory, change this if you have someother location
BACKUP="/srv/backup" # Backup Dest dir
# Get data in dd-mm-yyyy format
NOW="$(date +"%d-%m-%Y")"
DBFOLDER="${BACKUP}/${NOW}" # Maybe keep backups on $NOW dir?

# Get hostname
HOST="$(hostname)"
 
# File to store current backup file
FILE=""
# Store list of databases
DBS=""
 
[ ! -d $DBFOLDER ] && mkdir -p $DBFOLDER || :
 
# Get all database list first
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
# Backuping
for db in $DBS
do
 FILE=${DBFOLDER}/$db.$HOST.$(date +"%d-%m-%Y_%H").gz
 echo Backuping $db database... # Maybe need check for db backup?
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

# Uploading files to FTP server
FILES="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for file in $FILES
do
DEST=${DBFOLDER}/$file.$HOST.$(date +"%d-%m-%Y_%H").gz
echo Uploading $DEST to FTP Server...
$NCFTP -m -u "$FTPU" -p "$FTPP" "$FTPH" "$FTPD" "$DEST"
OSTAT="$?"
case $OSTAT in
	0) MESS="Success.";;
	1) MESS="Could not connect to remote host $FTPH.";;
        2) MESS="Could not connect to remote host $FTPH - timed out.";;
        3) MESS="Transfer failed.";;
        4) MESS="Transfer failed - timed out.";;
        5) MESS="Directory change failed.";;
        6) MESS="Directory change failed - timed out.";;
        7) MESS="Malformed URL.";;
        8) MESS="Usage error. May be your version of ncftpput ($NCFTP) is old";;
        9) MESS="Error in login configuration file.";;
        10)MESS="Library initialization failed.";;
        11) MESS="Session initialization failed.";;
	*) MESS="Unknown error, contact admin $ADMIN_INFO";;
esac
done

Re: [MySQL] Снимаем дампы и заливаем на фтп сервер

Добавлено: 24 фев 2011, 12:54
BaHbKa
хорошая штука, спасибо!

Re: [MySQL] Снимаем дампы и заливаем на фтп сервер

Добавлено: 24 фев 2011, 19:25
Gen1us2k
BaHbKa
да не за что :)
написал (что украл, что написал) и выложил :)