Pirun Seng

Ruby on Rails Developer

Copy File and Entire Directory From Amazon S3 to Local Machine

First thing we need to do is to install Amazon Web Service Command Line Interface (AWS CLI).

Depending on your environment, there are different ways to install AWS CLI. I’m using Mac OSX, and I prefer to install it via Homebrew brew install awscli.

Then we need to configure awscli by running: aws configure. You will be prompted to put your AWS Access Key Id, Secret Key, Region and Output. The following is an example:

Access key : foo
Secret key : bar
Region     : us-west-1
Output     : json

We are all set. Now, I gonna show you how we copy a specific file and an entire directory from Amazon S3.

Copy a file : aws s3 cp s3://bucket_name/dir/file.png local_path

Copy a directory : aws s3 cp --recursive s3://bucket_name/dir local_path

The example above, I show you how to copy a file, ‘file.png’, and an entire directory, ‘dir’, by specifying --recursive option from Amazon S3 to your local machine or directory where you specify local_path.


Backup and Restore Postgres Database From Amazon RDS

There are two things might happen when we do the backup and restore database. We need to know whether the database is located in local server or in any separate server like RDS.

Here, I’d like to raise two ways of backup Postgres database. One is the local database, and other one is the separate database.

Local Database

Backup: pg_dump -n schema_name -d db_name > backup_filename.dump

Restore: psql db_name < backup_filename.dump

Host Database (Amazon RDS) (You will be prompted to type your database password.)

Backup: pg_dump -h db_host -p db_port -U db_user -n schema_name -d db_name > backup_filename.dump

Restore: psql db_name -h db_host -p db_port -U db_user < backup_filename.dump

You should notice that we need to specify database host, port and user if we have the database setup in a host server, Amazon RDS in my case. That is the important point that some developers might forget.

For further detail of the other backup and restore options, please refer to the PostgreSQL docs and look for database backup and that applies to the Postgres version you use.

Enjoy your DBA time!

Dump Postgres Data From Specific Table

If you’re in a hurry to make your code work as expected, and you don’t prefer to read the document much, we have something in common. And this article is right for you.

If we haven’t read the note of the document as the screenshot above, then we missed something. And what we missed is the prefix schema name on our table name. So, instead of -n schema_name -t table_name, we should use this -t schema_name.table_name.

Happy coding!