Skip to content

Instantly share code, notes, and snippets.

@neo22s
Last active March 12, 2021 01:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neo22s/3c5201d349acfe3e1ba9 to your computer and use it in GitHub Desktop.
Save neo22s/3c5201d349acfe3e1ba9 to your computer and use it in GitHub Desktop.
Backup for AWS, uploads to FTP and deletes old files.
#################
# FTP AWS Backup Script
# Backup for AWS, uploads to FTP and deletes old files.
# chema@yclas.com
# You need to install AWS CLI read more here http://martinbuberl.com/blog/backup-amazon-s3-bucket-with-aws-cli/
# 2015-07-20
# GPLv3
#################
#################
####CONFIGS######
#################
#FTP details
FTP_SERVER=""
FTP_LOGIN=""
FTP_PASS=""
#FTP expires backup days
NDAYS=14
#GET DATE
DATE=$(date +"%Y-%m-%d-%H.%M")
#Where do we store the Backups - END POINT FOR FILES
EPF="/home/backups/aws"
#AWS Bucket name
FROM="YOURBUCKETNAME"
#backup name
TO="/home/backups/$DATE-AWS-backup.tgz"
#################
######START######
#################
#################
#1 - Download files from AWS bucket
#################
echo "Download files from $FROM to $EPF"
aws s3 sync s3://$FROM $EPF
#################
#2 - Compress Files
#################
echo "Compress files from $EPF to $TO"
tar czf $TO $EPF
#################
#3 - Delete Files
#################
echo "Delete files from $EPF"
rm -R $EPF/*
#################
#4 - removes old files from FTP
#################
# work out our cutoff date
MM=`date --date="$NDAYS days ago" +%b`
DD=`date --date="$NDAYS days ago" +%d`
echo "Removing files from FTP older than $MM $DD"
# get directory listing from remote source
listing=`ftp -i -n $FTP_SERVER <<EOMYF
user $FTP_LOGIN $FTP_PASS
binary
ls
quit
EOMYF
`
lista=( $listing )
# loop over our files
for ((FNO=0; FNO<${#lista[@]}; FNO+=9));do
# check the date stamp
if [ ${lista[`expr $FNO+5`]}=$MM ];
then
if [[ ${lista[`expr $FNO+6`]} -lt $DD ]];
then
# Remove this file
echo "Removing ${lista[`expr $FNO+8`]}"
ftp -i -n $FTP_SERVER <<EOMYF2
user $FTP_LOGIN $FTP_PASS
binary
delete ${lista[`expr $FNO+8`]}
quit
EOMYF2
fi
fi
done
#################
#5- move Backup to FTP
#################
echo "Moving to FTP"
ftp -n $FTP_SERVER <<END
user $FTP_LOGIN $FTP_PASS
put $TO /$TO
quit
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment