Initial commit.
[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
22 # intelligent features to avoid making superfluous copies of files
23 # which are already backuped.
24
25 set -e
26
27 [[ ${FREEZE_DIR} ]] || FREEZE_DIR=${HOME}/.backups
28
29 if [[ ! -d ${FREEZE_DIR} ]]; then
30     echo "Can not find directory ${FREEZE_DIR}" >&2
31     exit 1
32 fi
33
34 while [[ "$1" ]]; do
35
36     dir=$(basename "$1")
37     path=$(dirname "$1")
38     full_path="${path}/${dir}"
39
40     date=$(date +%Y_%b_%d_%H:%M:%S)
41
42     if [[ ! -d "${full_path}" ]]; then
43         echo "Can not find directory ${full_path}" >&2
44         exit 1
45     fi
46
47     if [[ $(find "${full_path}" -type f -name "core*" -o -name "vgcore.[0-9]*") ]]; then
48         # rm -i $(find "${full_path}" -type f -name "core" -o -name "vgcore.[0-9]*")
49         echo "There seems to be core files in ${full_path}" >&2
50         exit 1
51     fi
52
53     backup="${FREEZE_DIR}/${full_path}"
54
55     mkdir -p "${FREEZE_DIR}/${path}"
56
57     current_backup="${backup}:::current"
58     new_backup="${backup}:::${date}"
59
60     if [[ -h "${current_backup}" ]]; then
61         rsync --link-dest="${current_backup}/" -axz "${full_path}/" "${new_backup}/"
62         rm -f "${current_backup}"
63     else
64         if [[ -a ${current_backup} ]]; then
65             echo "${current_backup} exists and is not a symbolic link" >&2
66             exit 1
67         else
68             rsync -axz "${full_path}/" "${new_backup}/"
69         fi
70     fi
71
72     ln -s "${new_backup}" "${current_backup}"
73
74     sync
75
76     du -sh "${new_backup}"
77
78     shift
79
80 done