5ae7015e46436a31e8f6de12dd29462337a1add5
[scripts.git] / freeze-dir.sh
1 #!/bin/bash
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois@fleuret.org> for comments & bug reports             #
18 #########################################################################
19
20 set -e
21
22 [[ ${FREEZE_DIR} ]] || FREEZE_DIR=${HOME}/.backups
23
24 if [[ ! -d ${FREEZE_DIR} ]]; then
25     echo "Can not find directory ${FREEZE_DIR}" >&2
26     exit 1
27 fi
28
29 while [[ "$1" ]]; do
30
31     dir=$(basename "$1")
32     path=$(dirname "$1")
33     full_path="${path}/${dir}"
34
35     date=$(date +%Y_%b_%d_%H:%M:%S)
36
37     if [[ ! -d "${full_path}" ]]; then
38         echo "Can not find directory ${full_path}" >&2
39         exit 1
40     fi
41
42     if [[ $(find "${full_path}" -type f -name "core*" -o -name "vgcore.[0-9]*") ]]; then
43         # rm -i $(find "${full_path}" -type f -name "core" -o -name "vgcore.[0-9]*")
44         echo "There seems to be core files in ${full_path}" >&2
45         exit 1
46     fi
47
48     backup="${FREEZE_DIR}/${full_path}"
49
50     mkdir -p "${FREEZE_DIR}/${path}"
51
52     current_backup="${backup}:::current"
53     new_backup="${backup}:::${date}"
54
55     if [[ -h "${current_backup}" ]]; then
56         rsync --link-dest="${current_backup}/" -axz "${full_path}/" "${new_backup}/"
57         rm -f "${current_backup}"
58     else
59         if [[ -a ${current_backup} ]]; then
60             echo "${current_backup} exists and is not a symbolic link" >&2
61             exit 1
62         else
63             rsync -axz "${full_path}/" "${new_backup}/"
64         fi
65     fi
66
67     ln -s "${new_backup}" "${current_backup}"
68
69     sync
70
71     du -sh "${new_backup}"
72
73     shift
74
75 done