This page looks best with JavaScript enabled

FTP Script Backups

 ·  🎃 kr0m

The FTP service is still active after 33 years since its invention and it is still a good way to make our backups. In this occasion, I will show how to perform these backups automatically through a bash script.

The script is designed to be run on a Raspberry Pi, hence the time is synchronized with the external RTC before proceeding with the backup. The steps followed by the script are:

  • Sync time with RTC
  • Check that the files are not still open by any process
  • Move the files from that day to the directory with that name
  • Create the directory on the FTP and upload the files
  • Delete the oldest files of 20 days locally
  • Delete the oldest files of 20 days remotely
#!/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
If you liked the article, you can treat me to a RedBull here