Esta pagina se ve mejor con JavaScript habilitado

Script backups por FTP

 ·  🎃 kr0m

El servicio FTP sigue activo tras 33 años de su invención y sigue siendo una buena manera de hacer nuestros backups, en esta ocasión voy a mostrar como realizar dichos backups de forma automática mediante un script en bash.

El script está pensado para ser corrido en una Raspberrypi, de ahí que se sincronice la hora con el RTC externo antes de proceder con el backup, los pasos que sigue el script son:

  • Sync hora con RTC
  • Comprueba que los ficheros no sigan abiertos por algún proceso
  • Mueve los ficheros de ese día al directorio con dicho nombre
  • Crea el directorio en el ftp y sube los ficheros
  • Elimina los ficheros mas antiguos de 20 días en local
  • Elimina los ficheros mas antiguos de 20 días en remoto
#!/bin/bash
clear
echo -e " "
echo -e " "
echo -e "-----------------------------------"
echo -e "|  Script Move-FTP files by Kr0m  |"
echo -e "| Web: http://www.alfaexploit.com |"
echo -e "-----------------------------------"
echo -e " "

LOG="1"
DIR="/home/pi/camara/actual/"
DIR_HOME="/home/pi/camara/"
DATE=$(date +%d_%m_%Y)

echo -e "-- Syncying date via RTC"
if [ $LOG == "1" ]; then
    echo -e "-- Syncying date via RTC" > $DIR_HOME/$DATE.log
fi
hwclock -s

echo -e "-- Date Synced"
if [ $LOG == "1" ]; then
    echo -e "-- Date Synced" >> $DIR_HOME/$DATE.log
fi
echo -e " "

echo -e "-- Searching for FTP files"
if [ $LOG == "1" ]; then
    echo -e "-- Searching for FTP files" >> $DIR_HOME/$DATE.log
fi

if [ "$(ls -A $DIR)" ]; then
    mkdir $DIR_HOME/$DATE
    echo -e "-- FTP files found"
    if [ $LOG == "1" ]; then
        echo -e "-- FTP files found" >> $DIR_HOME/$DATE.log
    fi
    echo -e " "
        echo -e "---------- Checking that files are not accessed by a running process ----------"
    if [ $LOG == "1" ]; then
        echo -e "-- Checking that files are not accessed by a running process" >> $DIR_HOME/$DATE.log
    fi
        ls -la $DIR*|awk -F ' ' '{print$9}' > /tmp/ftp_files_list
        for file in $(</tmp/ftp_files_list)
        do
                echo -e "-- Checking file: $file"
        if [ $LOG == "1" ]; then
            echo -e "-- Checking file: $file" >> $DIR_HOME/$DATE.log
        fi
        N=$(lsof $file|wc -l)
        if [ $N = '0' ]; then
            echo -e "-- Moving file: $file"
            if [ $LOG == "1" ]; then
                echo -e "-- Moving file: $file" >> $DIR_HOME/$DATE.log
            fi
            mv $file $DIR_HOME/$DATE/
        else
            echo -e "++ File opened, skipping file: $file"
            if [ $LOG == "1" ]; then
                echo -e "++ File opened, skipping file: $file" >> $DIR_HOME/$DATE.log
            fi
        fi
        echo -e " "
        done
    chown -R pi:pi $DIR_HOME*
else
    echo -e "-- Files not found, is ftp service up?"
    if [ $LOG == "1" ]; then
        echo -e "-- Files not found, is ftp service up?" >> $DIR_HOME/$DATE.log
    fi
    echo -e " "
    exit
fi

echo -e " "
# UPLOAD FILES TO INET SERVER
echo -e "-- Uploading files to remote server"
if [ $LOG == "1" ]; then
    echo -e "-- Uploading files to remote server" >> $DIR_HOME/$DATE.log
    echo -e " "
fi

IP_address="FTPSERVER_IP"
username="FTP_USER"
password=FTP_PASS

ls -la $DIR_HOME/$DATE/*.jpg|awk -F '/' '{print$7}' > /tmp/upload_files

echo -e "-- Generating remote dir"
if [ $LOG == "1" ]; then
    echo -e "-- Generating remote dir" >> $DIR_HOME/$DATE.log
fi
ftp -ivn $IP_address > $DIR_HOME/$DATE/ftp_$DATE.log <<FINFTP
       user $username $password
       binary
       mkdir $DATE
       bye
FINFTP

cd $DIR_HOME/$DATE
for file in $(</tmp/upload_files)
do
echo -e "-- Uploading file: $file"
if [ $LOG == "1" ]; then
    echo -e "-- Uploading file: $file" >> $DIR_HOME/$DATE.log
fi
ftp -ivn $IP_address >> $DIR_HOME/$DATE.log <<FINFTP
       user $username $password
       binary
       cd $DATE
       put $file
       bye
FINFTP
echo -e " "
echo -e "-- Done"
done

echo -e " "
# REMOVING OLD FILES OLDER THAN 20DAYS
echo -e "-- Removing old files"
if [ $LOG == "1" ]; then
    echo -e "-- Removing old files" >> $DIR_HOME/$DATE.log
fi

find $DIR_HOME -type d -mtime +19 > /tmp/to_remove

for directory in $(</tmp/to_remove)
do
    if [ $LOG == "1" ]; then
        echo -e "-- Removing Dir: $directory"
    fi
    rm -rf $directory
done
echo -e " "

# REMOVING FTP OLD FILES
if [ $LOG == "1" ]; then
    echo -e "-- Removing FTP old files"
fi

for directory in $(</tmp/to_remove)
do
    dir=$(basename $directory)
        if [ $LOG == "1" ]; then
        echo -e "-- Removing FTP Dir: $dir"
    fi

ftp -ivn $IP_address >> $DIR_HOME/$DATE.log <<FINFTP
       user $username $password
       binary
       prompt
       prompt
       cd $dir
       mdelete *
       cd ..
       rmdir $dir
       bye
FINFTP
done
Si te ha gustado el artículo puedes invitarme a un RedBull aquí