Skip to content

Instantly share code, notes, and snippets.

@dado-prateek
Last active August 12, 2016 11:48
Show Gist options
  • Save dado-prateek/4b8a39ae7720e52420c1 to your computer and use it in GitHub Desktop.
Save dado-prateek/4b8a39ae7720e52420c1 to your computer and use it in GitHub Desktop.
Backup postgres users dbs to email
#!/bin/bash
export PATH=$PATH:/usr/bin:/usr/sbin
export RECIPIENTS='example@mail.org, example2@mail.org'
export FROM='admin'
# Setup passwords in /root/.pgpass
export BACKUP_USERS='django postgres'
if [ $UID != 0 ]
then
echo need to be root
exit 1
fi
for user in ${BACKUP_USERS}; do
fname=${user}-dump-$(date +%F_%H-%M).sql.gz
fpath=/tmp/$fname
pg_dump -h localhost -U ${user} | gzip > $fpath
sendmail "${RECIPIENTS}" <<END_MESSAGE
Content-Type: multipart/mixed; boundary="===============8024379111860202730=="
MIME-Version: 1.0
From: ${FROM}
To: ${RECIPIENTS}
Subject: =?utf-8?q?${user}_bd_dump_$(date +%F)?=
--===============8024379111860202730==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
0JTQsNC80L8g0LHQsNC30Ysg0LLQviDQstC70L7QttC10L3QuNC4IAoKLS0KYWRtaW4=
--===============8024379111860202730==
Content-Type: binary/octet-stream
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename*=utf-8''$fname
$(base64 $fpath)
--===============8024379111860202730==--
END_MESSAGE
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment