@@ -28,37 +28,56 @@ touch "incomplete"
28
28
GHE_MAINTENANCE_MODE_ENABLED=false
29
29
30
30
# To prevent multiple backup runs happening at the same time, we create a
31
- # in-progress symlink pointing to the snapshot directory. This will fail if
32
- # another backup is already in progress, giving us a form of locking.
31
+ # in-progress file with the timestamp and pid of the backup process,
32
+ # giving us a form of locking.
33
33
#
34
- # Set up a trap to remove the in-progress symlink if we exit for any reason but
35
- # verify that we own the in-progress symlink before doing so.
34
+ # Set up a trap to remove the in-progress file if we exit for any reason but
35
+ # verify that we are the same process before doing so.
36
36
#
37
37
# The cleanup trap also handles disabling maintenance mode on the appliance if
38
38
# it was automatically enabled.
39
39
cleanup () {
40
- if [ " $( readlink ../in-progress) " = " $GHE_SNAPSHOT_TIMESTAMP " ]; then
41
- unlink ../in-progress
40
+ if [ -f ../in-progress ]; then
41
+ progress=$( cat ../in-progress)
42
+ snapshot=$( echo " $progress " | cut -d ' ' -f 1)
43
+ pid=$( echo " $progress " | cut -d ' ' -f 2)
44
+ if [ " $snapshot " = " $GHE_SNAPSHOT_TIMESTAMP " -a " $$ " = $pid ]; then
45
+ unlink ../in-progress
42
46
fi
47
+ fi
43
48
44
- if $GHE_MAINTENANCE_MODE_ENABLED ; then
45
- ghe-maintenance-mode-disable " $GHE_HOSTNAME "
46
- fi
49
+ if $GHE_MAINTENANCE_MODE_ENABLED ; then
50
+ ghe-maintenance-mode-disable " $GHE_HOSTNAME "
51
+ fi
47
52
}
48
53
49
54
# Setup exit traps
50
55
trap ' cleanup' EXIT
51
56
trap ' exit $?' INT # ^C always terminate
52
57
53
- # Mark the snapshot as in-progress by creating the symlink. If this fails, it
54
- # means another ghe-backup run is already in progress and we should exit.
55
- # NOTE: The -n argument to ln is non-POSIX but widely supported.
56
- if ! ln -sn " $GHE_SNAPSHOT_TIMESTAMP " ../in-progress 2> /dev/null; then
57
- snapshot=" $( readlink ../in-progress) "
58
- echo " Error: backup of $GHE_HOSTNAME already in progress in snapshot $snapshot . Aborting." 1>&2
58
+ if [ -h ../in-progress ]; then
59
+ echo " Error: detected a backup already in progress from a previous version of ghe-backup." 1>&2
60
+ echo " If there is no backup in progress anymore, please remove" 1>&2
61
+ echo " the $GHE_DATA_DIR /in-progress symlink." 1>&2
62
+ exit 1
63
+ fi
64
+
65
+ if [ -f ../in-progress ]; then
66
+ progress=$( cat ../in-progress)
67
+ snapshot=$( echo " $progress " | cut -d ' ' -f 1)
68
+ pid=$( echo " $progress " | cut -d ' ' -f 2)
69
+ if ! ps -p $pid -o command= | grep ghe-backup; then
70
+ # We can safely remove in-progress, ghe-prune-snapshots
71
+ # will clean up the failed backup.
72
+ unlink ../in-progress
73
+ else
74
+ echo " Error: backup process $pid of $GHE_HOSTNAME already in progress in snapshot $snapshot . Aborting." 1>&2
59
75
exit 1
76
+ fi
60
77
fi
61
78
79
+ echo " $GHE_SNAPSHOT_TIMESTAMP $$ " > ../in-progress
80
+
62
81
echo " Starting backup of $GHE_HOSTNAME in snapshot $GHE_SNAPSHOT_TIMESTAMP "
63
82
64
83
# Perform a host connection check and establish the remote appliance version.
0 commit comments