timescaledb/scripts/ts_dump.sh
2018-10-29 13:28:19 -04:00

46 lines
1.8 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
echo "-- Restore to hypertable" >> $PREFIX-schema.sql
echo "--" >> $PREFIX-schema.sql
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