Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted (edited)

I have a Ubuntu Server and am wanting to have automatic backups of:

 

1) MySQL Database

2) Web directory (/var/www)

 

These backups should run daily, automatically export the databases and then send the files to a SMB share on a NAS box I have.

 

Can anybody provide a bit assistance in achieving this?

 

EDIT: Preferably in a ZIP format of some kind and labelled with the date

Edited by Hightower
Added request
Posted

### System Setup ###
DIRS="/var/www/"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
INCFILE="/root/zip-inc-backup.dat"
DAY=$(date +"%a")
FULLBACKUP="Sat"

### MySQL Setup ###
MUSER="username"
MPASS="**************"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
ZIP="$(which zip)"

### SMB/CIFS Server Setup ###
SMBD="/backup"
SMBU="username"
SMBP="**************"
SMBS="terastation"
SMBCLIENT="$(which smbclient)"

### 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
 SMBD='echo "$SMBD/full"'
 FILE="fs-full-$NOW.zip"
 $ZIP $BACKUP/$FILE $DIRS
else
 i=$(date +"%Hh%Mm%Ss")
 FILE="fs-i-$NOW-$i.zip"
 zip $BACKUP/$FILE $DIRS -i @$INCFILE
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").zip
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $ZIP $FILE -@
done

### Dump backup using FTP ###
#Start FTP backup using ncftp
smbclient -U"$SMBU" -P"$SMBP" $SMBS<mkdir $SMBD
mkdir $SMBD/$NOW
cd $SMBD/$NOW
lcd $BACKUP
mput *
quit
EOF

### Find out if backup failed or not ###
if [ "$?" == "0" ]; then
rm -rf $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

 

The above is a conversion of the tarball/ftp upload script I have here. I've only done it in my head but it probably works. :)

  • Thanks 1
Posted

Thanks for that Geoff. Can I ask for a little more help. I've changed the code as follows:

 

### System Setup ###
DIRS="/var/www/"
BACKUP=/tmp/backup.$$
NOW=$(date +"%d-%m-%Y")
INCFILE="/root/zip-inc-backup.dat"    //IS THIS OK AS IS?
DAY=$(date +"%a")

### MySQL Setup ###
MUSER="username"
MPASS="**************"
MHOST="localhost"
MYSQL="$(which mysql)"    //NOT SURE?
MYSQLDUMP="$(which mysqldump)" //NOT SURE?
ZIP="$(which zip)"    //NOT SURE?

### SMB/CIFS Server Setup ###
SMBD="/backup"
SMBU="username"
SMBP="**************"
SMBS="terastation"
SMBCLIENT="$(which smbclient)"    //NOT SURE?

### Other stuff ###
EMAILID="MY EMAIL ADDRESS"

### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :

SMBD='echo "$SMBD/full"'
FILE="fs-full-$NOW.zip"
$ZIP $BACKUP/$FILE $DIRS

### 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").zip
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $ZIP $FILE -@
done

### Dump backup using SMB ###
#Start SMB backup using ncftp
smbclient -U"$SMBU" -P"$SMBP" $SMBS<mkdir $SMBD
mkdir $SMBD/$NOW
cd $SMBD/$NOW
lcd $BACKUP
mput *
quit
EOF

### Find out if backup failed or not ###
if [ "$?" == "0" ]; then
mail  -s "BACKUP SUCCESSFUL" "$EMAILID" <$T
rm -rf $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

 

I've changed it so (i think) it will do a full backup every night. I've also put a few comments next to things I'm not sure about (//NOT SURE?). I'm just not sure what to put there so any more help would be appreciated.

 

Also, what do I save this script as? A '.sh' extension?

Posted

That filename in the root home dir just tells zip which files have changed and it needs to backup for incrimental backups.

 

the $(which somecommand) things are just an easy way of finding the executables.

 

Yes, make it a .sh and executable.

Posted
That filename in the root home dir just tells zip which files have changed and it needs to backup for incrimental backups.

 

the $(which somecommand) things are just an easy way of finding the executables.

 

Yes, make it a .sh and executable.

 

Thanks for your help Geoff. I'm now sorted with a slightly adapted script. This version does a full backup each time (as opposed to a incremental), and also puts all files into an archive for easier storage.

 


### System Setup ###
DIRS="/var/www/"
BACKUP=/var/backupscripts/backups/backup.$$
NOW=$(date +"%d-%m-%Y")
INCFILE="/root/tar-inc-backup.dat"
DAY=$(date +"%a")

### MySQL Setup ###
MUSER="USER"
MPASS="PASSWORD"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

SMBCLIENT="$(which smbclient)"

### Start Backup for file system ###
[ ! -d $BACKUP ] && mkdir -p $BACKUP || :
SMBD="DIRECTORY ON SHARE"
FILE="fs-full-$NOW.tar.gz"
tar -zcvf $BACKUP/$FILE $DIRS

### Start MySQL Backup ###
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

### Add all files to one archive for storage on NAS ###
### Call it by day name so it is overwritten each week ###
tar -zcvf $DAY.tgz $BACKUP

### Dump backup using SMB ###
smbclient //xxx.xxx.xxx.xxx/ShareName -U NETWORKUSER%PASSWORD -c "put $DAY.tgz"

### Delete backup files from local machine as they are now on NAS box ###
rm -rf $BACKUP
rm -rf $DAY.tgz

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...