Skip to content

Commit 6fd2124

Browse files
author
Aleksei Shpakovskii
committed
ENT-4242 First copy, then remove when creating backups
If we run out of space in the middle of `mv` command, we'll have "some files here, some files there" situation. To avoid it, we first do `cp`, and only if it succeeds - `rm` old dir.
1 parent ecf64a8 commit 6fd2124

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

packaging/common/cfengine-hub/preinstall.sh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,28 @@ if is_upgrade && egrep '^3\.([6-9]|1[01])\.' "$PREFIX/UPGRADED_FROM.txt" >/dev/n
104104
# Now that PostgreSQL is shut down, move the old data out of the way.
105105
mkdir -p "$BACKUP_DIR/lib"
106106
mkdir -p "$BACKUP_DIR/share"
107-
mv "$PREFIX/state/pg/data" "$BACKUP_DIR"
107+
108+
# instead of `mv`, do `cp && rm` - in case `cp` operation fails, we won't
109+
# have "some files here, some files there" situation
110+
# First, try copying files creating hardlinks
111+
if cp -al "$PREFIX/state/pg/data" "$BACKUP_DIR"; then
112+
# Copy succeeded - so we can delete old dir.
113+
rm -rf "$PREFIX/state/pg/data"
114+
else
115+
# Copy creating hardlinks failed, so remove partially-copied data and try simple copying
116+
rm -rf "$BACKUP_DIR/data"
117+
if cp -a "$PREFIX/state/pg/data" "$BACKUP_DIR"; then
118+
# Copy succeeded - so we can delete old dir.
119+
rm -rf "$PREFIX/state/pg/data"
120+
else
121+
# Copy failed, so remove partially-copied data and abort
122+
rm -rf "$BACKUP_DIR/data"
123+
cf_console echo "Creating of backup failed"
124+
cf_console echo "Please fix it before upgrading or disable upgrade by removing/renaming $PREFIX/state/pg/data prior to upgrade."
125+
exit 1
126+
fi
127+
fi
128+
108129
if ! diff "$BACKUP_DIR/data/postgresql.conf" "$PREFIX/share/postgresql/postgresql.conf.cfengine" > /dev/null; then
109130
# diff exits with 0 if the files are the same
110131
# the postgresql.conf file was modified, we should try to use it after migration

0 commit comments

Comments
 (0)