Script bash: Backup remoto por FTP
Script bash: Backup remoto por FTP. Hoy vemos un sencillo script que nos ayudara a realizar un backup remoto por FTP de manera simple. Lo puedes usar en cualquier servidor Linux que necesite copias de seguridad, ya sea de los archivos, las carpetas del sistema, y de todas sus bases de datos MySQL o MariaDB. El script utiliza NcFTP como cliente de FTP, debemos tenerlo instalado. Algunas de sus características más importantes: elimina las copias de seguridad anteriores los días que definamos, también envía un correo electrónico al sysadmin indicándole si el proceso fue un éxito, o si por el contrario se produjo algún error.
Backup remoto por FTP
Antes de comenzar, revisa atentamente las indicaciones: Guarda el script en /bin/ftpbackup.sh y lo haces ejecutable. chmod +x /bin/ftpbackup.sh Recuerda que necesitas tener NCFTP instalado, por ejemplo: sudo apt-get install ncftp Por defecto el lunes se hace la copia de seguridad completa, puedes modificar estos valores en el script (de lo contrario se realizan copias de seguridad incrementales). Por defecto se borran los backups con más de 30 días, puedes modificar la configuración según tus necesidades. Creamos el Script bash. nano /bin/ftpbackup.sh Copia y pega lo siguiente, pero OJO!!!, inserta tus datos reales, tal vez incluso debas modificar las rutas. #!/bin/bash # File System Backups via FTP with MySQL Databases ## # Guardar en /bin/ftpbackup.sh y hacer ejecutable # chmod +x /bin/ftpbackup.sh ## Your System Settings ## DIRS="/bin /etc /home /var/local /usr/local/bin /usr/lib /var/www" BACKUP=/tmp/backup.$$ NOW=$(date +"%Y-%m-%d") INCFILE="/root/tar-inc-backup.dat" DAY=$(date +"%a") FULLBACKUP="Mon" ## Your MySQL Settings ## MUSER="root" MPASS="tupassword" MHOST="localhost" MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" GZIP="$(which gzip)" ## Your FTP server Settings ## FTPD="//backup-directory-on-ftp-server" FTPU="ftp-usuario" FTPP="ftp-password" FTPS="ftp.server.address" NCFTP="$(which ncftpput)" ## Your Email Address ## EMAILID="[email protected]" ## Backup our DPKG Software List ## dpkg --get-selections > /etc/installed-software-dpkg.log ## Start the Backup for the file system ## && mkdir -p $BACKUP || : ## Check if we want to make a full or incremental backup ## if ; then FTPD="//full-backups" FILE="MyServer-fs-full-$NOW.tar.gz" tar -zcvf $BACKUP/$FILE $DIRS else i=$(date +"%Hh%Mm%Ss") FILE="MyServer-fs-incremental-$NOW-$i.tar.gz" tar -g $INCFILE -zcvf $BACKUP/$FILE $DIRS fi ## Start the MySQL Database Backups ## ## Get all the MySQL databases names ## DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')" for db in $DBS do FILE=$BACKUP/mysql-$db.gz $MYSQLDUMP --single-transaction -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE done ## Check the Date for Old Files on FTP to Delete ## REMDATE=$(date --date="30 days ago" +%Y-%m-%d) ## Start the FTP backup using ncftp ## ncftp -u"$FTPU" -p"$FTPP" $FTPS>$T echo "Backup failed" >>$T mail -s "MYSERVER - BACKUP FAILED" "$EMAILID" Read the full article












