Skip to content

Instantly share code, notes, and snippets.

@Cannonb4ll
Forked from Glennmen/ftpbackup.sh
Created November 28, 2019 08:16
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 Cannonb4ll/a99855e4f23919703e02733c5dac45dd to your computer and use it in GitHub Desktop.
Save Cannonb4ll/a99855e4f23919703e02733c5dac45dd to your computer and use it in GitHub Desktop.
System + MySQL backup script
#!/bin/sh
# System + MySQL backup script
# Full backup day - Sun (rest of the day do incremental backup)
# Copyright (c) 2005-2006 nixCraft <http://www.cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# Automatically generated by http://bash.cyberciti.biz/backup/wizard-ftp-script.php
# ---------------------------------------------------------------------
### System Setup ###
DIRS="/home /etc /var/www"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
REMOVAL_DATE=$(date -d "2 week ago" +"%d-%m-%Y")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sun"
### MySQL Setup ###
MUSER="xxxxx" # Replace with MySQL username
MPASS="xxxxx" # Replace with MySQL user password
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
### FTP server Setup ###
FTPD="/backup/incremental"
FTPU="xxxxx" # Replace with FTP backup server username
FTPP="xxxxx" # Replace with FTP backup server user password
FTPS="xxxxx" # Replace with FTP back up server IP
NCFTP="$(which ncftpput)"
### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
### See if we want to make a full backup ###
if [ "$DAY" = "$FULLBACKUP" ]; then
FTPD="/backup/full"
FILE="fs-full-$NOW.tar.gz"
tar -zcf $BACKUP/$FILE $DIRS
else
i=$(date +"%Hh%Mm%Ss")
FILE="fs-i-$NOW-$i.tar.gz"
tar -g $INCFILE -zcf $BACKUP/$FILE $DIRS
fi
### Start MySQL Backup ###
# Get all databases name
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS --skip-lock-tables $db | $GZIP -9 > $FILE
done
### Dump backup using FTP ###
#Start FTP backup using ncftp
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<EOF
rmdir -r $FTPD/$REMOVAL_DATE
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
quit
EOF
### Remove local backup files
rm -r /tmp/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment