How to prevent sd card curruption when you unplug the PI?

Hello, I’ve had 2 bad experiences to freePBX so far. I unplug it and it currupts the entire install , then I have to start all over and reflash and then reconfigure the raspberry PI 2. Is there a better way to prevent this from corruption ?? How do you shut it down other than removing power?

man shutdown

then

shutdown -h now. .

Can I program a GPIO pin to run that command with a press of a button?

Also is this corruption normal when removing power?

Yes to both , call

sync

To flush the drive before power off or turn on joirnalling (big performance hit though)

What does sync do? Thanks for all your help in this matter. I’m quite frustrated and I need this functioning for our HAM MESH node for field day tomorrow morning. I’m reflash in it now and will configure it on site.

Most commands in linux are documented and exposed by “man” here

man sync

Thank you very much!!