Skip to content

Instantly share code, notes, and snippets.

@MattLoyeD
Created July 18, 2013 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattLoyeD/6028246 to your computer and use it in GitHub Desktop.
Save MattLoyeD/6028246 to your computer and use it in GitHub Desktop.
Cron SH file to save your site and export the backup to an FTP. Script can remove files older than X (7 here) days on FTP and on localhost. All files and folder must be chmod 777 for convinience. Including this.
#!/bin/bash
echo "Backup in progress"
nowdate=`date '+%H%M-%m-%d-%Y'`
# your MySQL server's name
SERVER=domain.com
# directory to backup to
BACKDIR=/data/backups
# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`
# FTP server settings; should be self-explanatory
FTPHOST="ftp.domain2.com"
FTPUSER="ftp_login"
FTPPASS="ftp_pass"
# directory to backup to. if it doesn't exist, file will be uploaded to
# first logged-in directory
FTPDIR="./backups"
#-------------------Deletion Settings-------------------#
# delete old files?
DELETE=y
# how many days of backups do you want to keep?
DAYS=3
tar -czvPf "/data/backups/bc-${nowdate}.tgz" "/home/www/domain.com/httpdocs"
echo "Initiating FTP connection..."
cd $BACKDIR
ATTACH=`echo -n "put bc-${nowdate}.tgz"`
ftp -nv <<EOF
open $FTPHOST
user $FTPUSER $FTPPASS
cd $FTPDIR
$ATTACH
quit
EOF
echo "FTP transfer complete! \n"
if [ $DELETE = "y" ]
then
find $BACKDIR -name "*.tgz" -mtime $DAYS -exec rm {} \;
# work out our cutoff date
MM=`date --date="$DAYS days ago" +%b`
DD=`date --date="$DAYS days ago" +%d`
echo "Removing files older than $MM $DD"
# get directory listing from remote source
listing=`ftp -i -n $FTPHOST <<EOMYF
user $FTPUSER $FTPPASS
binary
cd $FTPDIR
ls
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)
# 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 $FTPHOST <<EOMYF2
user $FTPUSER $FTPPASS
binary
cd $FTPDIR
delete ${lista[`expr $FNO+8`]}
quit
EOMYF2
fi
fi
done
if [ $DAYS = "1" ]
then
echo "Yesterday's backup has been deleted."
else
echo "The backup from $DAYS days ago has been deleted."
fi
fi
echo "Backup done !"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment