Guest User

Untitled

a guest
Mar 5th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #!/bin/sh
  2. ##############################
  3. #Script by Anas V
  4. ##############################
  5.  
  6. #Substitute your postgresql root username in the below given line
  7. PGUSER=postgres
  8. #Substitute your postgresql root password in the below given line
  9. PGPASSWORD=stuffnthings
  10. export PGUSER PGPASSWORD
  11.  
  12. tdate=`date +%d-%b-%Y`
  13.  
  14. if [ $# -lt 1 ]
  15.  
  16. # Check if there is atleast one argument [i.e the database whose dump is to be taken]
  17. #First argument is mandatory - Databse name
  18. #Second argument is optional - Destination path to save dump
  19. then
  20.  
  21. echo "Bad Arguments"
  22. echo "-----------------------------------"
  23. echo "USAGE : pg_dmp.sh <databasename> [outputfile]"
  24. echo "-----------------------------------"
  25. exit 1
  26. else
  27.  
  28. #if one or more arguments were provided
  29. if [ $# -ge 2 ]
  30. #if arguments provided is greater than or equal to 2
  31. then
  32. #Comment out the below given file exist check and it's associated messages
  33. # if you want to run the pg_dmpsh script to run silently. i.e from a cron or at job
  34. # without any user interaction. Then the output dump file will be rewritten if a file
  35. # already exists.
  36. #if [ -f $2 ]
  37. # if destination file ie argument 2 is already existing
  38. #then
  39. # #Show confirmation message to confirm whether replace file with new one or exit
  40. # dialog --title "Confirm File Replace" --backtitle "pg_dmp.sh"\
  41. # --yesno "\nFile already exist, Do you want to replace it with '$2' file" 7 90
  42. # sel=$?
  43. # case $sel in
  44. # #if Yes then take dump and replace the existing file with new dump
  45. # 0) pg_dump $1 -f $2 -i -x -O -R;;
  46. # #if No then exit
  47. # 1) exit 1 ;;
  48. # #if escape then exit
  49. # 255) exit 1;;
  50. # esac
  51. #else
  52. # #if destination file does not exist then create and save the dump in destination path
  53. pg_dump $1 -f $2 -i -x -O -R
  54. #fi
  55. else
  56. if [ $# -eq 1 ]
  57. #if arguments provided is equal to 1
  58. then
  59. if [ -d $HOME/pg_backup_$1 ]
  60. #if folder name pg_backup_'databsename' exist in the current users home directory
  61. then
  62. #if [ -f $HOME/pg_backup_$1/$1_$tdate ]
  63. ##if destination file name exist in pg_backup_'databasename' folder in current users home dierectory
  64. #then
  65. ##Show confirmation message for replacing the file with new dump
  66. # dialog --title "Confirm File Replace" --backtitle "pg_dump.sh"\
  67. # --yesno "\nFile already exist, Do you want to replace it with '$HOME/pg_backup_$1/$1_$tdate' file" 7 90
  68. # sel=$?
  69. # case $sel in
  70. # #if Yes then replace the file with new dump file
  71. # 0) pg_dump $1 -f $HOME/pg_backup_$1/$1_$tdate -i -x -O -R;;
  72. # #if No then exit
  73. # 1) exit 1 ;;
  74. # #if escape thenexit
  75. # 255) exit 1;;
  76. # esac
  77. #else
  78. # #if destination file does not exist then create and save the dump
  79. pg_dump $1 -f $HOME/pg_backup_$1/$1_$tdate -i -x -O -R
  80. #fi
  81. else
  82. #if folder pg_backup_'databsename' does not exist in the current users home dierectory then
  83. #Create a new folder
  84. mkdir $HOME/pg_backup_$1
  85. #then create dump and save it
  86. pg_dump $1 -f $HOME/pg_backup_$1/$1_$tdate -i -x -O -R
  87. #if databse to take does not exist then Delete the folder created
  88. if [ $? -ne 0 ]
  89. then
  90. rmdir $HOME/pg_backup_$1
  91. fi
  92. fi
  93. fi
  94. fi
  95. if [ $# -gt 2 ]
  96. #if arguments passed where greater than 2 then show message
  97. then
  98. echo "Extra Arguments ignored"
  99. fi
  100. fi
  101.  
  102. #reset PGUSER and PGPASSWORD
  103. PGUSER=""
  104. PGPASSWORD=""
  105. export PGUSER PGPASSWORD
  106. #End
Add Comment
Please, Sign In to add comment