Een poos terug heb ik mijn backup scriptje aangepast dat het universeler te gebruiken werd over meerdere machines. Er werd te moeilijk gedaan, meerdere rsync commando’s (per map 1 rsync), geen variabelen enzovoorts.
Even een korte situatie schets: Er zijn een 3-tal Linux (CentOS 5/6) VM’s die gebackupped moeten worden. Kort gezged, die zijn als de bekende LAMP stacks opgebouwd. Deze backup gaat het datacenter uit naar de QNAP TS-412 thuis genaamd “vrieskist”. Elke dag loopt een backup: deze is ‘daily’ en op zondag draait de ‘weekly’. Redelijk simpel toch?
De map structuur word door de juiste rsync parameter ook duidelijk; /var/www/html word opgeslagen in de map /share/Backups/servernaam/daily/var/www/html
Er zijn een tweetal invoer bestanden die naast het script zelf staan:
– to_backup
– to_backup_sql
Hierin staat de mappen en bestanden die je mee wil nemen in de backup. Het SQL bestand mag duidelijk zijn; de databases, en in het bestand de database-naam.
Het resultaat; slechts een tweetal Rsync acties, en een veel duidelijkere logfile met een voor ‘platte data’ 1 eindoverzicht van statistieken.
Er zal vast nog wel even een nieuwe IF-conditie (if [[ -f to_backup_sql ]])bij komen zodat je bij een kale webserver,dus geen MySQL, het bestand ’to_backup_sql’ niet hoeft te plaatsen en het script zelf niet hoeft aan te passen.
# Backup script for the daily and weekly copys
# created by bartjan@pc-mania.nl, date 16-06-2013
# redone script (simplified, improved, universal, use of input files) 03-09-2016
if [[ "$(date +%a)" != "Sun" ]]; then
servername="$(hostname -s)"
destination="vrieskist:/share/Backups/$servername/$btype"
done < /root/scripts/to_backup
printf "\n========== $btype backup started at $(date) ==========\n" >> $log
printf "\nCopying Webcontent and Webserver configuration files...\n\n" >> $log
rsync -avphR --delete --log-file="$log" $data $destination
# Do the backup of databases
printf "\nMaking the MySQL dumps...\n" >> $log
if [ ! -d "/tmp/mysqlbackup_tmp" ]; then
mkdir /tmp/mysqlbackup_tmp
mysqldump --defaults-extra-file=/root/.my.cnf $line > /tmp/mysqlbackup_tmp/$line.sql
done < /root/scripts/to_backup_sql
printf "\nCopying Databases and MySQL configuration...\n" >> $log
rsync -avph --delete --log-file="$log" /tmp/mysqlbackup_tmp/* $destination/mysql/
printf "\nCleanup after backup...\n" >> $log
rm -rf /tmp/mysqlbackup_tmp
printf "\nAll done!\n\n" >> $log
#!/bin/bash
#
# Backup script for the daily and weekly copys
#
# created by bartjan@pc-mania.nl, date 16-06-2013
# redone script (simplified, improved, universal, use of input files) 03-09-2016
HOME=/root/
# Variables to work with
if [[ "$(date +%a)" != "Sun" ]]; then
btype="daily"
else
btype="weekly"
fi
servername="$(hostname -s)"
log="/var/log/backuplog"
destination="vrieskist:/share/Backups/$servername/$btype"
# Do the backup of files
while read line
do
data="$data $line"
done < /root/scripts/to_backup
printf "\n========== $btype backup started at $(date) ==========\n" >> $log
printf "\nCopying Webcontent and Webserver configuration files...\n\n" >> $log
rsync -avphR --delete --log-file="$log" $data $destination
# Do the backup of databases
printf "\nMaking the MySQL dumps...\n" >> $log
if [ ! -d "/tmp/mysqlbackup_tmp" ]; then
mkdir /tmp/mysqlbackup_tmp
fi
while read line
do
mysqldump --defaults-extra-file=/root/.my.cnf $line > /tmp/mysqlbackup_tmp/$line.sql
done < /root/scripts/to_backup_sql
printf "\nCopying Databases and MySQL configuration...\n" >> $log
rsync -avph --delete --log-file="$log" /tmp/mysqlbackup_tmp/* $destination/mysql/
printf "\nCleanup after backup...\n" >> $log
rm -rf /tmp/mysqlbackup_tmp
printf "\nAll done!\n\n" >> $log
#HOME=/
#!/bin/bash
#
# Backup script for the daily and weekly copys
#
# created by bartjan@pc-mania.nl, date 16-06-2013
# redone script (simplified, improved, universal, use of input files) 03-09-2016
HOME=/root/
# Variables to work with
if [[ "$(date +%a)" != "Sun" ]]; then
btype="daily"
else
btype="weekly"
fi
servername="$(hostname -s)"
log="/var/log/backuplog"
destination="vrieskist:/share/Backups/$servername/$btype"
# Do the backup of files
while read line
do
data="$data $line"
done < /root/scripts/to_backup
printf "\n========== $btype backup started at $(date) ==========\n" >> $log
printf "\nCopying Webcontent and Webserver configuration files...\n\n" >> $log
rsync -avphR --delete --log-file="$log" $data $destination
# Do the backup of databases
printf "\nMaking the MySQL dumps...\n" >> $log
if [ ! -d "/tmp/mysqlbackup_tmp" ]; then
mkdir /tmp/mysqlbackup_tmp
fi
while read line
do
mysqldump --defaults-extra-file=/root/.my.cnf $line > /tmp/mysqlbackup_tmp/$line.sql
done < /root/scripts/to_backup_sql
printf "\nCopying Databases and MySQL configuration...\n" >> $log
rsync -avph --delete --log-file="$log" /tmp/mysqlbackup_tmp/* $destination/mysql/
printf "\nCleanup after backup...\n" >> $log
rm -rf /tmp/mysqlbackup_tmp
printf "\nAll done!\n\n" >> $log
#HOME=/