Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
165 lines (148 sloc) 4.73 KB
#
# FILE DISCONTINUED HERE
# UPDATED VERSION AT
# https://gitlab.com/yeupou/stalag13/raw/master/etc/cron.weekly/stalag13-backups
#
# | |
# \_V_//
# \/=|=\/
# [=v=]
# __\___/_____
# /..[ _____ ]
# /_ [ [ M /] ]
# /../.[ [ M /@] ]
# <-->[_[ [M /@/] ]
# /../ [.[ [ /@/ ] ]
# _________________]\ /__/ [_[ [/@/ C] ]
# <_________________>>0---] [=\ \@/ C / /
# ___ ___ ]/000o /__\ \ C / /
# \ / /....\ \_/ /
# ....\||/.... [___/=\___/
# . . . . [...] [...]
# . .. . [___/ \___]
# . 0 .. 0 . <---> <--->
# /\/\. . . ./\/\ [..] [..]
#
#!/bin/bash
#
# Copyright (c) 2011-2014 Mathieu Roy <yeupou--gnu.org>
# http://yeupou.wordpress.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
# run only on server
if [ `hostname` != "moe" ]; then exit; fi
# available hard drives
drive1=wd4T
drive2=wd2T
# get opts
for i in "$@"; do
case $i in
--drive1=*)
drive1="${i#*=}"
shift
;;
--drive2=*)
drive2="${i#*=}"
shift
;;
--skip-user-data)
skipuserdata=1
shift
;;
*)
# unknown
;;
esac
done
## user data backing up
# not trying to be smart here, just put stuff in backups, not trying to
# understand on which drives we work on
# (the system clone will mess with /home but not part outside the system
# partitions)
if [ ! -z ${skipuserdata+x} ]; then
echo "Skip user datat";
else
for userhome in /storage/abstract/home.*; do
/usr/bin/rsync -a --delete $userhome /backups/
done
#outdated /usr/bin/rsync -a --delete /var/lib/wiki /backups/
/usr/bin/rsync -a --delete /storage/abstract/musique /backups/
/usr/bin/mysqldump --compact wiki2012 > /backups/mediawiki.sql && /bin/bzip2 -f /backups/mediawiki.sql
date > /backups/.lastrun
fi
## system cloning
sys=debian64
swap=swap
mount=/mnt/sysclone
ignore="backups dev lost+found media proc run sys tmp"
# determines which drive is currently / by reading /etc/fstab
orig=`cat /etc/fstab | grep $sys | cut -f 1 | cut -f 2 -d = | sed s/$sys//g;`
case $orig in
$drive1)
dest=$drive2
;;
$drive2)
dest=$drive1
;;
*)
echo "Unable to determine whether we are currently using $drive1 or $drive2, we found $orig. Exiting!"
exit
;;
esac
# then proceed
origdev=`blkid -L $orig$sys | tr -d [:digit:]`
destdev=`blkid -L $dest$sys | tr -d [:digit:]`
# easy reminder of the last cloning run
date > /etc/.lastclone
echo "$orig > $dest ($sys)" >> /etc/.lastclone
etckeeper commit "cloning $sys system from $orig ($origdev) to $dest ($destdev)" >/dev/null 2>/dev/null
# mount clone system
if [ ! -d $mount ]; then exit; fi
mount -L $dest$sys $mount
# set up ignore list
for dir in $ignore; do
touch /$dir.ignore
done
# do copy
for dir in /*; do
if [ -d $dir ]; then
if [ ! -e $dir.ignore ]; then
# update if not set to be ignored
/usr/bin/rsync --archive --one-file-system --delete $dir $mount/
else
# otherwise just make sure the directory actually exists
if [ ! -e $mount/$dir ]; then mkdir $mount/$dir; fi
rm $dir.ignore
fi
fi
done
# update filesystem data
sed -i s/^LABEL\=$orig$sys/LABEL\=$dest$sys/g $mount/etc/fstab
sed -i s/^LABEL\=$orig$swap/LABEL\=$dest$swap/g $mount/etc/fstab
# make system bootable (use --force: gpt partition table)
mount --bind /dev $mount/dev
mount --bind /sys $mount/sys
mount proc -t proc $mount/proc
chroot $mount /usr/sbin/grub-mkdevicemap 2>/dev/null
chroot $mount /usr/sbin/update-grub 2>/dev/null
chroot $mount /usr/sbin/grub-install --force $destdev >/dev/null 2>/dev/null
# (sleep to avoid weird timeout after rsync)
sleep 10s
# then cleanup
umount $mount/dev $mount/sys $mount/proc
umount $mount
fsck -a LABEL=$dest$sys > /dev/null
## EOF