Skip to content

Instantly share code, notes, and snippets.

@MattLoyeD
Created July 18, 2013 10:15
Show Gist options
  • Save MattLoyeD/6028264 to your computer and use it in GitHub Desktop.
Save MattLoyeD/6028264 to your computer and use it in GitHub Desktop.
Cron SH file to save your SQL and export the backup to an FTP. Script can remove files older than X (7 here) days on localhost. All files and folder must be chmod 777 for convinience. Including this.
#!/bin/bash
echo "SQL Backup in Progress"
#Date
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_log"
FTPPASS="ftp_pass"
# directory to backup to. if it doesn't exist, file will be uploaded to
# first logged-in directory
FTPDIR="/backups"
#----------------------MySQL Settings--------------------#
# your MySQL server's location (IP address is best)
HOST=localhost
# MySQL username
USER=mysql_username
# MySQL password
PASS=mysql_password
# List all of the MySQL databases that you want to backup in here, each separated by a space
DBS="db1 db2 db3"
#-------------------Deletion Settings-------------------#
# how many days of backups do you want to keep?
DAYS=3
mysqldump -u$USER -p$PASS --databases $DBS > $BACKDIR/bc-$NOWDATE.sql
echo "Initiating FTP connection..."
cd $BACKDIR
ATTACH=`echo -n "put bc-${NOWDATE}.sql"`
ftp -nv <<EOF
open $FTPHOST
user $FTPUSER $FTPPASS
cd $FTPDIR
$ATTACH
quit
EOF
echo -e "FTP transfer complete! \n"
find $BACKDIR -name "*.sql" -mtime $DAYS -exec rm {} \;
echo "SQL Backup done !"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment