Knowledgebase:
Linux GFS backup script
Posted by Jared Thomas on 16 December 2015 03:06 PM

Below is a script for backing up a Linux server to a local folder. This script will back up the folders specified in the $src variable along with all MySQL databases on a daily, weekly and monthly basis and automatically purges each after a set time period defined in the "-mtime +" argument. This script doesn't export backups to external storage, nor does it log or notify. 

#!/bin/bash
# Linux local backup script
# Variables
src="/etc /var/www /var/log /home /root"
dst="/opt/backup"
today=$(/bin/date +%F)
DOW=$(/bin/date +%u)
DOM=$(/bin/date +%d)
# Daily backup
/bin/tar czf $dst/daily/$today.tar.gz $src
/usr/bin/mysqldump -u root -ppassword --all-databases --events | gzip > $dst/daily/$today.sql.gz
/bin/find $dst/daily -type f -mtime +7 -print0 | xargs -0 rm -rf
# Weekly backup
if [ $DOW -eq 6 ] ; then
/bin/tar czf $dst/weekly/$today.tar.gz $src
/usr/bin/mysqldump -u root -ppassword --all-databases --events | gzip > $dst/weekly/$today.sql.gz
/bin/find $dst/weekly -type f -mtime +28 -print0 | xargs -0 rm -rf
fi
if [ $DOM -eq 1 ] ; then
/bin/tar czf $dst/monthly/$today.tar.gz $src
/usr/bin/mysqldump -u root -ppassword --all-databases --events | gzip > $dst/monthly/$today.sql.gz
/bin/find $dst/monthly -type f -mtime +92 -print0 | xargs -0 rm -rf
fi
exit 0

(1 vote(s))
Helpful
Not helpful

Comments (0)
Post a new comment
 
 
Full Name:
Email:
Comments:
CAPTCHA Verification 
 
Please enter the text you see in the image into the textbox below (we use this to prevent automated submissions).