Jump to content
Sign in to follow this  
TTV_RANDARON

Copia de seguridad de MySQL + FTP

Recommended Posts

prerrequisitos

  • mysqldump
  • gzip
  • ncftp
  • servidor ftp configurado correctamente

¿Cómo funciona el guión? ¿Cómo usar?

  • Vuelca y comprime las bases de datos mysql deseadas
  • Transfiere archivos comprimidos al host remoto
  • Elimina archivos comprimidos locales

Este script solo toma un argumento (modo).

El modo define qué bases de datos deben volcarse.

Ejecutarlo como:

 

sh backupmysql.sh <mode>

Compruebe los comentarios de origen para ver los modos disponibles.

 

Se ejecuta automáticamente

También puede definirlo para que se ejecute automáticamente con un cronjob e incluso ejecutar diferentes modos en diferentes momentos.

 

Para agregar un nuevo cronjob, ejecute:

 

crontab -e

Ejemplo de cronjob (una vez al día):

 

0 0 * * * sh /usr/home/someuser/backupmysql.sh all

Fuente del script:

 

#!/bin/sh

# Get mode from user input
#	Allowed modes:
#		all:		backups account, common, log, player, webserver
#		game:		backups account, common, player
#		gameLog:	backups log
#		web:		backups webserver
#		exceptLog:	backups account, common, player, webserver
MODE=$1

if [ "$MODE" == "" ]; then
	MODE="exceptLog"
fi

# Bins
MYSQLDUMP=`which mysqldump`
GZIP=`which gzip`
NCFTP=`which ncftp`

# Date for folders and filenames
DAY=$(date +"%Y-%m-%d")
FILETIME=$(date +"%Y-%m-%d.%T")

# Local backup folder (no trailing slash)
LOCAL_FOLDER="/tmp/backup"

# FTP Configuration
REMOTE_HOST="x.x.x.x"
REMOTE_USER="ftp_user"
REMOTE_PASS="ftp_pass"
REMOTE_FOLDER="/" # With trailing slash

# MySQL Configuration
MYSQL_USER="mysql_user"
MYSQL_PASS="mysql_pass"

# Which databases shall we backup?
# Databases should be separated with a space
DATABASES=""
if [ "$MODE" == "all" ]; then
	DATABASES="account common log player webserver"
elif [ "$MODE" == "game" ]; then
	DATABASES="account common player"
elif [ "$MODE" == "gameLog" ]; then
	DATABASES="log"
elif [ "$MODE" == "web" ]; then
	DATABASES="webserver"
elif [ "$MODE" == "exceptLog" ]; then
	DATABASES="account common player webserver"
fi

# Check if DATABASES var is set...
if [ "$DATABASES" == "" ]; then
	echo -e "033[31mThe specified mode doesn't exist...033[0m"
	exit 1
fi

# Dump and compress
for db in $DATABASES
do
	FILE=$db.$FILETIME.gz
	echo -e "033[32mDumping $db!033[0m"
	$MYSQLDUMP -u $MYSQL_USER -p$MYSQL_PASS $db | $GZIP -9 > $LOCAL_FOLDER/$FILE
done

# Transfer all backup files to remote host
echo -e "033[32mnTransfering files!033[0m"
$NCFTP -u$REMOTE_USER -p$REMOTE_PASS $REMOTE_HOST<<EOF
mkdir $REMOTE_FOLDER$DAY
cd $REMOTE_FOLDER$DAY
lcd $LOCAL_FOLDER
mput *
quit
EOF

# Delete local dump files
rm -f $LOCAL_FOLDER/*

Créditos de : 

Debes iniciar sesión para ver el contenido del enlace en esta publicación.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...