backup.sh
Code:
#!/bin/bash
unset PATH
# USER VARIABLES
BACKUPDIR=/backup # Folder on the backup server where the backups shall be located
EXCLUDES=/backup/exclude_list # File containing the excluded patterns
DAYS=90 # The number of days after which old backups will be deleted
# PATH VARIABLES
SH=/bin/sh # Location of the bash bin
CP=/bin/cp; # Location of the cp bin
FIND=/usr/bin/find; # Location of the find bin
ECHO=/bin/echo; # Location of the echo bin
MK=/bin/mkdir; # Location of the mk bin
SSH=/usr/bin/ssh; # Location of the ssh bin
DATE=/bin/date; # Location of the date bin
RM=/bin/rm; # Location of the rm bin
GREP=/bin/grep; # Location of the grep bin
RSYNC=/usr/bin/rsync; # Location of the rsync bin
TOUCH=/bin/touch; # Location of the touch bin
DF=/bin/df;
## ##
## -- DO NOT EDIT BELOW THIS HERE -- ##
## ##
# CREATING NECESSARY FOLDERS
$MK $BACKUPDIR
CURRENT=$BACKUPDIR/current
OLD=$BACKUPDIR/old
$MK $CURRENT
$MK $OLD
# CREATING CURRENT DATE / TIME
NOW=`$DATE '+%Y-%m'-%d_%H:%M`
NOW=$OLD/$NOW
$MK $NOW
# RUN MYSQL BACKUP
# Comment this out if you don't have mysql dbs
$SH /backup/my.sh
# RUN RSYNC INTO CURRENT
$RSYNC \
-aplvz --delete --delete-excluded \
--exclude-from="$EXCLUDES" \
/ \
$CURRENT;
# UPDATE THE MTIME TO REFELCT THE SNAPSHOT TIME
$TOUCH $BACKUPDIR/current
# MAKE HARDLINK COPY
$CP -al $CURRENT/* $NOW
# REMOVE OLD BACKUPS
for FILE in "$( $FIND $OLD -maxdepth 1 -type d -mtime +$DAYS )"
do
$RM -Rf $FILE
# $ECHO $FILE
done
$DF
exit 0
my.sh
Code:
# Make MySQL Backups
#!/bin/bash
# Remove old file
mkdir /mysql_backup
rm -f /mysql_backup/*
#Dump new files
USER=root
PASSWORD=****************************
HOST=localhost
for i in $(echo 'SHOW DATABASES;' | mysql -u$USER -p$PASSWORD -h$HOST|grep -v '^Database$'); do
mysqldump \
-u$USER -p$PASSWORD -h$HOST \
-Q -e -C --add-drop-table --add-locks --quick --lock-tables \
-B $i \
$i > /mysql_backup/$i.sql;
done;
backup_exclude
Code:
/backup/current/
/backup/old/
/bin/
/boot/
/dev/
/lib/
/lost+found/
/mnt/
/opt/
/proc/
/sbin/
/sys/
/tmp/
/usr/
/var/cache/
/media/
/mnt/
make sure to not create loops in the backup... so I exculde /backup/current (where the current sync will be done to) and /backup/old (where the older backups are)... depending on how you setup your system, you want to backup /media also...
For more in filter/inculde/exclude rules, check out here: http://www.samba.org/ftp/rsync/rsync.html (about 3/4 down the page: FILTER RULES + INCLUDE/EXCLUDE PATTERN RULES).
basically it's very simple:
(1) define some vars
(2) define some system binaries (as I run an unset PATH first that is required)
(3) get current time and make a backup directory
[(4) backup the mysql dbs into a given folder]
(5) sync the real data now with the "current" folder
(6) hardlink the "current" folder to the created new backup directory in step (3)
(7) remove backups that are older than X days
(8) run df -l at the end (because I have cron mailing me the results of the backup script, so I will see how much diskspace is still empty)
Bookmarks