Skip to content

Instantly share code, notes, and snippets.

@tomfanning
Last active January 18, 2017 15:16
Show Gist options
  • Save tomfanning/f41472090193637259cddf509d6f0578 to your computer and use it in GitHub Desktop.
Save tomfanning/f41472090193637259cddf509d6f0578 to your computer and use it in GitHub Desktop.
Script to relocate postgres 9.5 data dir on Ubuntu 14.04 to a compressed ZFS volume (single disk, /dev/sdb, assumed to be blank)
#!/bin/bash -e
# Script to relocate postgres 9.5 data dir on Ubuntu 14.04 to a compressed ZFS volume (single disk, /dev/sdb, assumed to be blank)
# install ZFS and tools
apt-get install software-properties-common rsync -y
echo "
" | add-apt-repository ppa:zfs-native/stable
apt-get update
apt-get install ubuntu-zfs -y
modprobe zfs
# create a compressed single disk ZFS volume mounted at /compressed
zpool create compressed -m /compressed /dev/disk/by-id/$(ls -l /dev/disk/by-id | grep sdb | grep scsi- | grep -v part | awk -F ' ' '{print $9}') -O compression=lz4 -O recordsize=128k -f
# expands out to the equivalent of
# ls -l /dev/disk/by-id
# zpool create compressed -m /compressed /dev/disk/by-id/scsi-3600224808c8b9c03e89b155933a3c8fd -O compression=lz4 -O recordsize=128k -f
# move the database
service postgresql stop
rsync -av /var/lib/postgresql /compressed
mv /var/lib/postgresql/9.5/main /var/lib/postgresql/9.5/main.bak
# reconfigure postgres
# set data_directory = '/compressed/postgresql/9.5/main'
sed -i -e '/^data_directory =/s/^.*$/data_directory = '"'\/compressed\/postgresql\/9.5\/main'"'/' /etc/postgresql/9.5/main/postgresql.conf
# start postgres
service postgresql start
# verify results
# check that directory returned from the following command starts with "/compressed"
sudo -u postgres psql -c "SHOW data_directory;"
# delete the backup
rm -Rf /var/lib/postgresql/9.5/main.bak
# verify that postgres still works
service postgresql restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment