Update.
authorFrancois Fleuret <francois@fleuret.org>
Fri, 6 Nov 2015 09:01:02 +0000 (10:01 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Fri, 6 Nov 2015 09:01:02 +0000 (10:01 +0100)
freeze-dir.sh [new file with mode: 0755]

diff --git a/freeze-dir.sh b/freeze-dir.sh
new file mode 100755 (executable)
index 0000000..5ae7015
--- /dev/null
@@ -0,0 +1,75 @@
+#!/bin/bash
+
+#########################################################################
+# This program is free software: you can redistribute it and/or modify  #
+# it under the terms of the version 3 of the GNU General Public License #
+# as published by the Free Software Foundation.                         #
+#                                                                       #
+# 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, see <http://www.gnu.org/licenses/>.  #
+#                                                                       #
+# Written by and Copyright (C) Francois Fleuret                         #
+# Contact <francois@fleuret.org> for comments & bug reports             #
+#########################################################################
+
+set -e
+
+[[ ${FREEZE_DIR} ]] || FREEZE_DIR=${HOME}/.backups
+
+if [[ ! -d ${FREEZE_DIR} ]]; then
+    echo "Can not find directory ${FREEZE_DIR}" >&2
+    exit 1
+fi
+
+while [[ "$1" ]]; do
+
+    dir=$(basename "$1")
+    path=$(dirname "$1")
+    full_path="${path}/${dir}"
+
+    date=$(date +%Y_%b_%d_%H:%M:%S)
+
+    if [[ ! -d "${full_path}" ]]; then
+        echo "Can not find directory ${full_path}" >&2
+        exit 1
+    fi
+
+    if [[ $(find "${full_path}" -type f -name "core*" -o -name "vgcore.[0-9]*") ]]; then
+        # rm -i $(find "${full_path}" -type f -name "core" -o -name "vgcore.[0-9]*")
+        echo "There seems to be core files in ${full_path}" >&2
+        exit 1
+    fi
+
+    backup="${FREEZE_DIR}/${full_path}"
+
+    mkdir -p "${FREEZE_DIR}/${path}"
+
+    current_backup="${backup}:::current"
+    new_backup="${backup}:::${date}"
+
+    if [[ -h "${current_backup}" ]]; then
+        rsync --link-dest="${current_backup}/" -axz "${full_path}/" "${new_backup}/"
+        rm -f "${current_backup}"
+    else
+        if [[ -a ${current_backup} ]]; then
+            echo "${current_backup} exists and is not a symbolic link" >&2
+            exit 1
+        else
+            rsync -axz "${full_path}/" "${new_backup}/"
+        fi
+    fi
+
+    ln -s "${new_backup}" "${current_backup}"
+
+    sync
+
+    du -sh "${new_backup}"
+
+    shift
+
+done