--execute="ALTER DATABASE defaultdb RENAME TO defaultdb_backup;"
if[[$? > 0]];then
echo"Failed to rename existing CockroachDB database. Exiting."
exit$?
fi
docker exec cockroach \
cockroach sql \
--host cockroach:26257 \
--certs-dir=/certs \
--execute="RESTORE DATABASE defaultdb FROM '$S3_BACKUP_PATH/$BACKUP/cockroach?AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID&AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY';"
if[[$?==0]];then
# Restoration succeeded, drop the backup.
docker exec cockroach \
cockroach sql \
--host cockroach:26257 \
--certs-dir=/certs \
--execute="DROP DATABASE defaultdb_backup;"
echo"CockroachDB restoration succeeded."
else
# Restoration failed, drop the new DB and put back the old one.
echo"CockroachDB restoration failed, rolling back."
docker exec cockroach \
cockroach sql \
--host cockroach:26257 \
--certs-dir=/certs \
--execute="DROP DATABASE defaultdb;"
docker exec cockroach \
cockroach sql \
--host cockroach:26257 \
--certs-dir=/certs \
--execute="ALTER DATABASE defaultdb_backup RENAME TO defaultdb;"