Automated Backup on Linux using cron jobs (Postgresql)

in #utopian-io5 years ago

What Will I Learn?

This post explains how to set up a crontab job that automatically Backup a database (PostgreSQL) in time slots specific.

Requirements

  • Operative system based Linux
  • PostgreSQL
  • cron

Difficulty

  • Basic

Tutorial Contents

  • What is Cron
  • PostgreSQL
  • Step by Step

What is Cron

Cron is the name of the program that allows Linux / Unix users to automatically execute commands or scripts at a specific time or date. It is normally used for administrative task commands, in this case we will use it for a simple backup task.

PostgreSQL

It is a very popular database engine in the web environment, especially on the open source side, official website https://www.postgresql.org/.

Step by Step

Create file with nano backup.sh

  1. Add interpreter for the script
    #!bin/bash

  2. Define the password in the variable used by Postgresql
    export PGPASSWORD="user1"

  3. Variable for the File Name with current system date
    FILENAME="/DB/Backup_$(date +%Y%m%d%H%M%S).backup"

  4. Command pg_dump to create the backup file.
    pg_dump -U user --format custom --verbose --file $FILENAME DB_NAME
    4.1 Parameters:

    • -U => Username
    • --fromat => File Format
    • --verbose => Show log
    • --file => File name to export
    • DB_NAME => Database to backup
  5. delete content of PGPASSWORD
    unset PGPASSWORD

Result

#!bin/bash
export PGPASSWORD="user1" 
FILENAME="/DB/Backup_$(date +%Y%m%d%H%M%S).backup"

pg_dump -U user1 --format custom --blobs --verbose --file $FILENAME DB_NAME

unset PGPASSWORD

Add file on cron
Execute in console: crontab -e
This will allow you to edit or define cron

# m h  dom mon dow   command
0 22 * * * sh /backup.sh > /backup.log  

Cron Definition

  • m=> minute
  • h=> hour
  • Sun=> day of the week
  • mon=> month
  • dow=> day of the month
  • command => command to execute

To show your cron must execute the command
crontab -l

Finally, with this configuration you will have a daily backup every day at 22 hours.

Proof of Work Done

https://gist.github.com/Raul-mz/c25ba589371acde1619ee568e9ba14fb

Sort:  

Thank you for your contribution @raulmz.
After reviewing your tutorial we suggest the following points listed below:

  • Your tutorial is quite short for a good tutorial. We recommend you aim for capturing at least 2-3 concepts.

  • In the next tutorial try to further detail the steps you are teaching the reader so that the reader will understand what you are explaining.

  • There are already several tutorials to talk about cron jobs in linux with postgresql. Bring something more innovative to the open source community.

Thank you for your work in developing this tutorial.
Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @portugalcoin! Keep up the good work!

Thank you raulmz! You've just received an upvote of 57% by @ArtTurtle!


Learn how I will upvote each and every one of your art and music posts

Please come visit me as I've updated my daily report with more information about my upvote value and how to get the best upvote from me.

Hi @raulmz!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your UA account score is currently 0.000 which ranks you at #114581 across all Steem accounts.
Your rank has not changed in the last three days.

In our last Algorithmic Curation Round, consisting of 330 contributions, your post is ranked at #302.

Evaluation of your UA score:
  • Only a few people are following you, try to convince more people with good work.
  • Your contribution has not gone unnoticed, keep up the good work!
  • Try to improve on your user engagement! The more interesting interaction in the comments of your post, the better!

Feel free to join our @steem-ua Discord server

Congratulations @raulmz! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Are you a DrugWars early adopter? Benvenuto in famiglia!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.25
TRX 0.11
JST 0.032
BTC 61830.08
ETH 2986.99
USDT 1.00
SBD 3.73