Created
January 6, 2019 09:54
-
-
Save farmad/0aff7550e6ec1ce7509680bebeaf95a4 to your computer and use it in GitHub Desktop.
Backup MySQL Databases, Web server Files to a FTP Server Automatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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") | |
INCFILE="/root/tar-inc-backup.dat" | |
DAY=$(date +"%a") | |
FULLBACKUP="Sun" | |
### MySQL Setup ### | |
MUSER="admin" | |
MPASS="mysqladminpassword" | |
MHOST="localhost" | |
MYSQL="$(which mysql)" | |
MYSQLDUMP="$(which mysqldump)" | |
GZIP="$(which gzip)" | |
### FTP server Setup ### | |
FTPD="/home/vivek/incremental" | |
FTPU="vivek" | |
FTPP="ftppassword" | |
FTPS="208.111.11.2" | |
NCFTP="$(which ncftpput)" | |
### Other stuff ### | |
EMAILID="admin@theos.in" | |
### Start Backup for file system ### | |
[ ! -d $BACKUP ] && mkdir -p $BACKUP || : | |
### See if we want to make a full backup ### | |
if [ "$DAY" == "$FULLBACKUP" ]; then | |
FTPD="/home/vivek/full" | |
FILE="fs-full-$NOW.tar.gz" | |
tar -zcvf $BACKUP/$FILE $DIRS | |
else | |
i=$(date +"%Hh%Mm%Ss") | |
FILE="fs-i-$NOW-$i.tar.gz" | |
tar -g $INCFILE -zcvf $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 $db | $GZIP -9 > $FILE | |
done | |
### Dump backup using FTP ### | |
#Start FTP backup using ncftp | |
ncftp -u"$FTPU" -p"$FTPP" $FTPS<<eof | |
mkdir $FTPD | |
mkdir $FTPD/$NOW | |
cd $FTPD/$NOW | |
lcd $BACKUP | |
mput * | |
quit | |
EOF | |
### Find out if ftp backup failed or not ### | |
if [ "$?" == "0" ]; then | |
rm -f $BACKUP/* | |
else | |
T=/tmp/backup.fail | |
echo "Date: $(date)">$T | |
echo "Hostname: $(hostname)" >>$T | |
echo "Backup failed" >>$T | |
mail -s "BACKUP FAILED" "$EMAILID" <$T | |
rm -f $T | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment