Skip to content

Instantly share code, notes, and snippets.

@mjmckinnon
Last active August 29, 2015 14:24
Show Gist options
  • Save mjmckinnon/1d82a067a3f8fbb9fa9a to your computer and use it in GitHub Desktop.
Save mjmckinnon/1d82a067a3f8fbb9fa9a to your computer and use it in GitHub Desktop.
Ubuntu Backup Script
#!/bin/sh
##############################################################
# Ubuntu Backup Script
# Michael J. McKinnon
#
# date ver comment
# ------------ ------ --------------------------------------
# 5/2/2009 1.0 Initial version
# 9/2/2009 1.1 Added mySQL Database Name to output
# 29/3/2013 1.2 Revised and extra comments added
# 20/7/2015 1.3 Published in https://gist.github.com/mjmckinnon/1d82a067a3f8fbb9fa9a
# ------------ ------ --------------------------------------
#
##############################################################
### This file should be owned by root and readable only by root,
### and placed into crontab to execute as root
### i.e.
### chown root.root /backup.sh
### chmod 600 /backup.sh
### Samba Share for Backup ###
SMBSHARE="//n.n.n.n/share"
SMBUSER=">>username<<"
SMBPASS=">>password<<"
### MySQL Setup ###
# On an typical ubuntu server with mysql you can copy
# the credentials from /etc/mysql/debian.cnf, it is
# usually the debian-sys-maint user that has equivalent
# root privilege - in a future version of this script we
# could just read and parse that file instead
MYSQLBACKUP=yes
MUSER="debian-sys-maint"
MPASS=">>password<<"
MHOST="localhost"
##############################################################
# DO NOT EDIT ANYTHING BELOW THIS LINE #
##############################################################
NOW="$(date +%Y%m%d-%H%M%S)"
TAR="$(which tar)"
MYSQL="$(which mysql)"
GZIP="$(which gzip)"
MYSQLDUMP="$(which mysqldump)"
if [ $MYSQLBACKUP -eq "yes" ];
### Make tmp/yyyymmdd
mkdir /tmp/$NOW
### Get all databases name ###
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
### Create dir for each databases, backup tables in individual files ###
mkdir /tmp/$NOW/$db
logger "Script /backup.sh backing up DB: $db"
for i in `echo "show tables" | $MYSQL -u $MUSER -h $MHOST -p$MPASS $db|grep -v Tables_in_`;
do
FILE=/tmp/$NOW/$db/$i.sql.gz
echo $db $i; $MYSQLDUMP --add-drop-table --allow-keywords -q -c -u $MUSER -h $MHOST -p$MPASS $db $i | $GZIP -9 > $FILE
done
done
fi
# Make our mount point if not already
mkdir -p /media/backup
# Mount to the backup share
mount -t cifs -o username="$SMBUSER",password="$SMBPASS" "$SMBSHARE" /media/backup
# TODO: Script should probably check for success of SMB mount, if it fails it
# can start consuming space on the local filesystem instead
if [ $MYSQLBACKUP -eq "yes" ];
# Archive the locally dumped mySQL database data
$TAR cvzf /media/backup/$NOW-$(hostname)-mysql-backup.tgz -C /tmp/$NOW .
# Remove the mySQL tmp files
rm -rf /tmp/$NOW
fi
# Backup entire system
$TAR cvpzf /media/backup/$NOW-$(hostname)-backup.tgz \
--exclude=/proc --exclude=/lost+found \
--exclude=/media/backup --exclude=/mnt \
--exclude=/sys --exclude=/tmp /
# Dismount the SMB share
umount /media/backup
# Leave a syslog entry to say we're done
logger "Script /backup.sh is complete. Backup for $NOW stored in $SMBSHARE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment