Update.
[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 # This command makes a backup copy of a given directory into a backup
21 # directory, tags it with the date and time, and uses rsync smart use
22 # of hard links to avoid unnecessary duplicates of files already
23 # backuped.
24
25 set -e
26
27 # The default directory does not start with a period, as it may be
28 # huge and should not be "hidden" from the user.
29
30 [[ ${FREEZE_DIR} ]] || FREEZE_DIR=${HOME}/freezer
31
32 if [[ ! -d ${FREEZE_DIR} ]]; then
33     echo "Can not find directory ${FREEZE_DIR}" >&2
34     exit 1
35 fi
36
37 while [[ "$1" ]]; do
38     real_path="$(realpath "$1")"
39     dir=$(basename "${real_path}")
40     path=$(dirname "${real_path}")
41     full_path="${path}/${dir}"
42
43     date=$(date +%Y_%b_%d_%H:%M:%S)
44
45     if [[ ! -d "${full_path}" ]]; then
46         echo "Can not find directory ${full_path}" >&2
47         exit 1
48     fi
49
50     if [[ $(find "${full_path}" -type f -name "core*" -o -name "vgcore.[0-9]*") ]]; then
51         # rm -i $(find "${full_path}" -type f -name "core" -o -name "vgcore.[0-9]*")
52         echo "There seems to be core files in ${full_path}" >&2
53         exit 1
54     fi
55
56     backup="${FREEZE_DIR}/${full_path}"
57
58     mkdir -p "${FREEZE_DIR}/${path}"
59
60     current_backup="${backup}:::current"
61     new_backup="${backup}:::${date}"
62
63     if [[ -h "${current_backup}" ]]; then
64         rsync --progress --link-dest="${current_backup}/" -axz "${full_path}/" "${new_backup}/"
65         rm "${current_backup}"
66     else
67         if [[ -a ${current_backup} ]]; then
68             echo "${current_backup} exists and is not a symbolic link" >&2
69             exit 1
70         else
71             rsync --progress -axz "${full_path}/" "${new_backup}/"
72         fi
73     fi
74
75     ln -vs "${new_backup}" "${current_backup}"
76
77     sync
78
79     du -sh "${new_backup}"
80
81     shift
82
83 done