Linux Backups
Introduction
The Linux servers are all backed up using the same script. With the exception of the MySQL script on the
monitor server. This MySQL instance is in a different location to the default.
There are a few components required for it to work.
If setting up a new backup you will need to install smbfs to get the drive mapping to work.
$ sudo apt-get install smbfs
Additionally there are a few linux components that need to be edited.
/etc/hostname
ensure that the hostname is correct, as this is the name that the backup script will use to name the backup
files.
Overview
The backup script basically maps the network drive \\wbl-backup-01\linux_backups, and zips everything to
the there. it then unmounts the drive
The MySQL backup script basically connects to MySQL and dumps everything to a temp file. It then maps
the network drive \\wbl-backup-01\linux_backups, and zips the SQL data to the there. it then unmounts the
drive and deletes the temp file.
The AD service account for this is zz_LinuxBackups.
Deploying the scripts
Create the Backup Directory to hold the backup files and logs
$ mkdir /backups/
Create the backup script
$ sudo nano /backups/mybackup.sh
Paste the contents of the script below
Crtl-X, y, Enter
Create the MySQL backup script
$ sudo nano /backups/mysqlbackup.sh
Paste the contents of the script below
''Edit the MySQL credentials for the Mysql Server"
Crtl-X, y, Enter
Make the scripts executable
$ sudo chmod +x /backups/*.sh
Ubuntu/Debian has an "allow" location for cron scripts. Allow the daily scripts by linking them to the "allow"
folder
$ sudo ln -s /backups/mybackup.sh /etc/cron.daily/mybackup
$ sudo ln -s /backups/mysqlbackup.sh /etc/cron.daily/mysqlbackup
Create the scheduled task for the backups to run. See the lines required below. Adjust the times as
neccesary
/etc/cron.d/backups
$ sudo nano /etc/cron.d/backups
Paste the contents of the script below
Crtl-X, y, Enter
/etc/cron.d/backups
# m h dom mon dow user command
30 17 * * * root /backups/mybackup.sh > /backups/mybackup.log
10 17 * * * root /backups/mysqlbackup.sh >
/backups/mysqlbackup.log
/backups/mybackup.sh
#!/bin/bash
varusr="zz_LinuxBackups"
vardom="domain"
varpass="zxcvbasdfg"
varRemoteServer="wbl-bkup-01"
varRemoteShare="Linux_Backups"
backupdir="/backups" # set the folder you backup to
#Set debug flag to work out what's going on if you have trouble
#debug=true
debug=false
#create incremental filename
mynew_dir()
{
local newdir="$*"
i=0
while [ -e $newdir ]; do
i=`expr $i + 1`
newdir="$*-$i"
done
echo $newdir
}
dostuff()
{
# Mount Remote share
sudo mkdir $varMountPoint
sudo mount -t smbfs -o
username=$varusr,domain=$vardom,password=$varpass
//$varRemoteServer/$varRemoteShare $varMountPoint
#exclude /mnt stuff
# had to do this after the mount or it backs up all the remote mount.
for i in `ls /mnt -A`; do
echo "/mnt/$i" >> $exclude
done
# Backup and gzip the directory
sudo tar cvzpf $varMountPoint/$myhostname-backup-$fecha.tgz --same-
owner --exclude-from=$exclude / 2>/backups/error-$myhostname-backup-
$fecha.log
# Rotate the logs, delete Local and remotefiles older than 7 days
find $varMountPoint/$myhostname-* -mtime +7 -exec rm {} \;
find $backupdir/*.log -mtime +7 -exec rm {} \;
#sudo find $varMountPoint/ -name "*$myhostname*" -mtime +1 -exec rm {}
\;
#find $varMountPoint/ -name '$myhostname*' -mtime +7 -exec rm {} \;
sudo cp /backups/$myhostname-backup-$fecha.tgz $varMountPoint
sudo umount $varMountPoint
sudo rmdir $varMountPoint
sudo rm $exclude
}
#################SETUP VARS#################
#create new exclude file
exclude=`mynew_dir /tmp/exclude`
# fecha has a formated date with unique number
fecha=`date +"%d-%m-%Y-%s"`
# Get hostname
myhostname=`sudo cat $tgt/etc/hostname`
#Create temporary mountpoint (unique ID so that multiple tasks can
overlap)
varMountPoint=`mynew_dir /mnt/remoteshare-$fecha`
#fixed exclusions - add any here if you want
echo $backupdir > $exclude
echo $bindingdir >> $exclude
echo /boot/grub >> $exclude
echo /etc/fstab >> $exclude
echo /etc/mtab >> $exclude
echo /etc/blkid.tab >> $exclude
echo /etc/udev/rules.d/70-persistent-net.rules >> $exclude
echo /lost+found >> $exclude
echo /boot/lost+found >> $exclude
echo /home/lost+found >> $exclude
echo /tmp/lost+found >> $exclude
echo /usr/lost+found >> $exclude
echo /var/lost+found >> $exclude
echo /srv/lost+found >> $exclude
echo /opt/lost+found >> $exclude
echo /usr/local/lost+found >> $exclude
echo $backupdir >> $exclude
echo
#no idea what this is for ?
for i in `swapon -s | grep file | cut -d " " -f 1`; do
echo "${i#/}" >> $exclude
done
# exclude tmp stuff
for i in `ls /tmp -A`; do
echo "/tmp/$i" >> $exclude
done
#exclude /media stuff
for i in `ls /media -A`; do
echo "/media/$i" >> $exclude
done
#exclude the local repository of retrieved package files in
/var/cache/apt/archives/
for i in /var/cache/apt/archives/*.deb; do
[ -e "$i" ] && echo "/${i#/}" >> $exclude
done
for i in /var/cache/apt/archives/partial/*; do
[ -e "$i" ] && echo "/${i#/}" >> $exclude
done
#Touch backup scripts so they are not deleted at with the cleanup
sudo touch /backups/*.sh
if [ "$debug" = "true" ]
then
echo "Hostname = " $myhostname
echo "date & filetag = "$fecha
echo "sudo mount -t smbfs -o
username=$varusr,domain=$vardom,password=$varpass
//$varRemoteServer/$varRemoteShare $varMountPoint"
#echo "echo exclude=`mynew_dir /tmp/exclude`"
echo "varMountPoint=mynew_dir /mnt/remoteshare-$fecha"
wait
more $exclude
else
dostuff
fi
/backups/mysqlackup.sh
#!/bin/sh
# User Vars
datum=`/bin/date +%Y%m%d-%H`
varMySQLusr=root
varMySQLpwd=password
varusr="zz_LinuxBackups"
vardom="domain"
varpass="zxcvbasdfg"
varRemoteServer="wbl-bkup-01"
varRemoteShare="Linux_Backups"
backupdir="/backups"
#Touch backup file so as it's not deleted
sudo touch $backupdir/*.sh
#Set debug flag to work out what's going on if you have trouble
#debug=true
debug=false
#create incremental filename
mynew_dir()
{
local newdir="$*"
i=0
while [ -e $newdir ]; do
i=`expr $i + 1`
newdir="$*-$i"
done
echo $newdir
}
# Main Prog
dostuff()
{
#/usr/bin/mysqladmin --user=$varMySQLusr --password=$varMySQLpwd
#stop-master
#Dump MySQL data
/usr/bin/mysqldump --user=$varMySQLusr --password=$varMySQLpwd --lock-
all-tables \
--all-databases > /backups/mysqlbackup-${datum}.sql
#/usr/bin/mysqladmin --user=$varMySQLusr --password=$varMySQLpwd
#start-master
# Mount Remote share
sudo mkdir $varMountPoint
sudo mount -t smbfs -o
username=$varusr,domain=$vardom,password=$varpass
//$varRemoteServer/$varRemoteShare $varMountPoint
# Backup and gzip the SQL data to the remote share
sudo tar cvzpf $varMountPoint/$myhostname-MySQLbackup-$datum.tgz
/backups/mysqlbackup-${datum}.sql 2>/backups/error-$myhostname-
MySQLbackup-$datum.log
# Cleanup
sudo umount $varMountPoint
sudo rmdir $varMountPoint
sudo rm /backups/mysqlbackup-${datum}.sql
}
#################SETUP DYNAMIC VARS#################
# Get hostname
myhostname=`sudo cat $tgt/etc/hostname`
#Create temporary mountpoint (unique ID so that multiple tasks can
overlap)
varMountPoint=`mynew_dir /mnt/remoteshare-$datum`
if [ "$debug" = "true" ]
then
echo $varMySQLpwd
echo $vaMySQLrusr
echo "Hostname = " $myhostname
echo "date & filetag = "$datum
echo "sudo mount -t smbfs -o
username=$varusr,domain=$vardom,password=$varpass
//$varRemoteServer/$varRemoteShare $varMountPoint"
wait
else
dostuff
fi
exit 0