I am using Ubuntu 20.04.3 LTS and I am using a FTP clean up script for Windows OS from Graeme - https://stackoverflow.com/a/12552795/17189713
For months I am facing with an issue that I do not know how to resolve.
The issue is that the script is removing backup file generated on 01 to 09 of the month besides the required days.
Let me elaborate.
Right now is 1 November. So in the FTP server, I should have 7 copies from 26th to 31th October and 1st November but instead, I am only having 6 copies right now, 26th to 31th October.
So tomorrow, 2 November, I will have instead 5 copies (27th to 31th) instead in the FTP server because the script keeps deleting the backup file it generated as I suspect it is due to the number '0' at the front.
So on and so for.
I am using this to generate the backup for my netbox:
pg_dump -h localhost -U {removed} > /home/backup/backup_$(date +"%Y_%m_%d_%I_%M_%p").sql
Which the backup filename is
backup_2021_11_01_12_00_AM.sql
Below is the code for FTP clean up that I am using:
#!/bin/bash
# get a list of files and dates from ftp and remove files older than ndays
ftpsite=""
ftpuser=""
ftppass=""
putdir="/"
ndays=7
# work out our cutoff date
MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`
echo removing files older than $DD $MM
# get directory listing from remote source
listing=`ftp -pin $ftpsite <<EOMYF
user $ftpuser $ftppass
binary
cd $putdir
dir
quit
EOMYF
`
lista=( $listing )
# loop over our files
for ((FNO=0; FNO<${#lista[@]}; FNO+=9));do
# month (element 5), day (element 6) and filename (element 8)
#echo Date ${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]} File: ${lista[`expr $FNO+8`]}
# check the date stamp
if [ ${lista[`expr $FNO+5`]}=$MM ];
then
if [[ ${lista[`expr $FNO+6`]} -lt 10#$DD ]];
then
# Remove this file
echo "Removing ${lista[`expr $FNO+8`]}"
ftp -pin $ftpsite <<EOMYF2
user $ftpuser $ftppass
binary
cd $putdir
delete ${lista[`expr $FNO+8`]}
quit
EOMYF2
fi
fi
done
The Window server does not SFTP connection.
Does anyone know how to resolve the issue or has any alternative script to share?