timescaledb/scripts/ts_dump.sh
Alexander Kuzmenkov 4a17d4c402 Add shellcheck to CI
Writing a shell script correctly can be hard even for a skilled
programmer. shellcheck is a static analysis tool that helps catch
common errors in shell scripts. We now have 36 executable scripts in
our repository, for which shellcheck reports 126 errors (calculated
like find . -type f -executable -exec bash -c '[ "$(file --brief
--mime-type "$1")" == "text/x-shellscript" ]' sh {} \; -exec shellcheck
-f gcc --exclude=SC2086 {} \; | cut -d: -f1 | sort | uniq | wc -l).
This commit fixes these warnings and adds a GitHub actions workflow
that runs shellcheck on all the executable shell scripts in the
repository. The warning SC2086: Double quote to prevent globbing and
word splitting is disabled globally, because it has little practical
consequences, sometimes leads to false positives, and is general is too
widespread because people forget to quote.
2021-11-15 14:54:14 +03:00

46 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copyright (c) 2016-2018 Timescale, Inc. All Rights Reserved.
#
# This file is licensed under the Apache License, see LICENSE-APACHE
# at the top level directory of the timescaledb distribution.
# This script is used for backing up a single hypertable into an easy-to-restore
# tarball. The tarball contains two files: (1) a .sql file for recreating the
# hypertable and its indices and (2) a .csv file containing the data as CSV.
#
# Because pg_dump/pg_restore dump all of TimescaleDB's internal tables when
# used, this script is useful if you want a backup that can be restored
# regardless of TimescaleDB version running, or as part of a process where you
# do not want to always backup all your hypertables at once.
if [[ -z "$1" || -z "$2" ]]; then
echo "Usage: $0 hypertable output_name [pg_dump CONNECTION OPTIONS]"
echo " hypertable - Hypertable to backup"
echo " output_name - Output files will be stored in an archive named [output_name].tar.gz"
echo " "
echo "Any connection options for pg_dump/psql (e.g. -d database, -U username) should be listed at the end"
exit 1
fi
HYPERTABLE=$1
PREFIX=$2
shift 2
set -e
echo "Backing up schema as $PREFIX-schema.sql..."
pg_dump "$@" --schema-only -t $HYPERTABLE -f $PREFIX-schema.sql
echo >> $PREFIX-schema.sql "--
-- Restore to hypertable
--"
psql "$@" -qAtX -c "SELECT _timescaledb_internal.get_create_command('$HYPERTABLE');" >> $PREFIX-schema.sql
echo "Backing up data as $PREFIX-data.csv..."
psql "$@" -c "\COPY (SELECT * FROM $HYPERTABLE) TO $PREFIX-data.csv DELIMITER ',' CSV"
echo "Archiving and removing previous files..."
tar -czvf $PREFIX.tar.gz $PREFIX-data.csv $PREFIX-schema.sql
rm $PREFIX-data.csv
rm $PREFIX-schema.sql