Created
January 8, 2011 14:39
-
-
Save atma/770887 to your computer and use it in GitHub Desktop.
This file contains hidden or 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) | |
# --------------------------------------------------------------------- | |
### System Setup ### | |
DIRS="/home/www1 /home/www2" | |
BACKUP=/tmp/backup.$$ | |
NOW=$(date +"%d-%m-%Y") | |
INCFILE="/root/tar-inc-backup.dat" | |
DAY=$(date +"%a") | |
FULLBACKUP="Sun" | |
### MySQL Setup ### | |
MUSER="root" | |
MPASS="pass" | |
MHOST="localhost" | |
MYSQL="$(which mysql)" | |
MYSQLDUMP="$(which mysqldump)" | |
GZIP="$(which gzip)" | |
### FTP server Setup ### | |
FTPD="/backups/incremental" | |
FTPU="atma" | |
FTPP="pass" | |
FTPS="44.55.66.77" | |
NCFTP="$(which ncftpput)" | |
### Other stuff ### | |
EMAILID="[email protected]" | |
### Start Backup for file system ### | |
[ ! -d $BACKUP ] && mkdir -p $BACKUP || : | |
### See if we want to make a full backup ### | |
if [ "$DAY" == "$FULLBACKUP" ]; then | |
FTPD="/backups/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 | |
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