{"id":786,"date":"2020-04-10T13:00:25","date_gmt":"2020-04-10T18:00:25","guid":{"rendered":"http:\/\/www.sqlfreelancer.com\/blog\/?p=786"},"modified":"2020-04-10T13:01:31","modified_gmt":"2020-04-10T18:01:31","slug":"backup-postgresql-to-aws-s3-in-linux-centos","status":"publish","type":"post","link":"https:\/\/www.sqlfreelancer.com\/blog\/backup-postgresql-to-aws-s3-in-linux-centos\/","title":{"rendered":"Backup PostgreSQL to AWS s3 in Linux CentOS"},"content":{"rendered":"\n<p>This post walks you through how to backup a PostgreSQL database to an AWS s3 bucket.<\/p>\n\n\n\n<p>There are a few installations we&#8217;ll need to make before allowing our on-prem Postgres server to communicate with AWS.<\/p>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">Install pip<\/span><\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Use the&nbsp;<code>curl<\/code>&nbsp;command to download the installation script. The following command uses the&nbsp;<code>-O<\/code>&nbsp;(uppercase &#8220;O&#8221;) parameter to specify that the downloaded file is to be stored in the current folder using the same name it has on the remote host:<br><br><code> <strong>curl -O https:\/\/bootstrap.pypa.io\/get-pip.py<\/strong> <\/code><br><\/li><li>Run the script with Python to download and install the latest version of&nbsp;<code>pip<\/code>&nbsp;and other required support packages:<br><br><strong><code>python36 get-pip.py --user<\/code><\/strong><br><br>When you include the&nbsp;<code>--user<\/code>&nbsp;switch, the script installs&nbsp;<code>pip<\/code>&nbsp;to the path&nbsp;<code>~\/.local\/bin<\/code>.<br><\/li><li>Ensure the folder that contains&nbsp;<code>pip<\/code>&nbsp;is part of your&nbsp;<code>PATH<\/code>&nbsp;variable.<br><br><strong><code>ls -a ~<\/code><\/strong><br><\/li><li>Add an export command at the end of your profile script that&#8217;s similar to the following example.<br><br><code><strong>source ~\/<\/strong>.bash_profile<\/code><br><\/li><li>Now you can test to verify that&nbsp;<code>pip<\/code>&nbsp;is installed correctly.<br><br><strong><code>pip3 --version<\/code><\/strong><br><\/li><\/ol>\n\n\n\n<p><strong><span style=\"text-decoration: underline;\">Install the AWS CLI with pip<\/span><\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Use&nbsp;<code>pip<\/code>&nbsp;to install the AWS CLI.<br><br><strong><code>pip3 install awscli --upgrade --user<\/code><\/strong><br><\/li><li>Verify that the AWS CLI installed correctly.<br><br><strong><code>aws --version<\/code><\/strong><\/li><\/ol>\n\n\n\n<p>Now that we have AWS CLI installed, we can configure our new client. You will need AWS Access Key ID, AWS Secret Access Key, Default Region Name and Default Output Format This information you can go to the IAM AWS Section.<\/p>\n\n\n\n<p><strong><code>aws configure<\/code><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"663\" height=\"91\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2020\/04\/image-1.png\" alt=\"\" class=\"wp-image-788\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2020\/04\/image-1.png 663w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2020\/04\/image-1-300x41.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/figure>\n\n\n\n<p>To view your s3 buckets use the following:<\/p>\n\n\n\n<p><strong><code>aws s3 ls<\/code><\/strong><\/p>\n\n\n\n<p>Now that AWS is configured and we can view our s3 buckets, let&#8217;s make a backup:<\/p>\n\n\n\n<p><strong><code>PGPASSWORD=\"password\" .\/pg_dump --no-owner -h localhost -U databasename &gt; ~\/databasename.sql<\/code><\/strong><\/p>\n\n\n\n<p>To view the backup file use the following:<\/p>\n\n\n\n<p><strong><code>cd \/root<br>dir<\/code><\/strong><\/p>\n\n\n\n<p>Now that we have a backup, let&#8217;s create an AWS s3 bucket to store them in:<\/p>\n\n\n\n<p><strong><code>aws s3api create-bucket --bucket postgres-backups --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2<\/code><\/strong><\/p>\n\n\n\n<p>Back in AWS, you can see the new bucket:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"710\" height=\"224\" src=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2020\/04\/image-2.png\" alt=\"\" class=\"wp-image-791\" srcset=\"https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2020\/04\/image-2.png 710w, https:\/\/www.sqlfreelancer.com\/blog\/wp-content\/uploads\/2020\/04\/image-2-300x95.png 300w\" sizes=\"auto, (max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p>Once the new bucket has been created, let&#8217;s push the backup we took earlier to this bucket.<\/p>\n\n\n\n<p><strong><code>aws s3 cp databasename.sql s3:\/\/postgres-backups\/<\/code><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post walks you through how to backup a PostgreSQL database to an AWS s3 bucket. There are a few installations we&#8217;ll need to make before allowing our on-prem Postgres server to communicate with AWS. Install pip Use the&nbsp;curl&nbsp;command to download the installation script. The following command uses the&nbsp;-O&nbsp;(uppercase &#8220;O&#8221;) parameter to specify that the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[129],"tags":[],"class_list":["post-786","post","type-post","status-publish","format-standard","hentry","category-aws"],"_links":{"self":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/786","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=786"}],"version-history":[{"count":4,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/786\/revisions"}],"predecessor-version":[{"id":793,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/786\/revisions\/793"}],"wp:attachment":[{"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}