Advertisement
OldManRiver

Purge Bash Script

Sep 12th, 2021
142
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # Author: Nyle Davis Created 21-10-05
  3. # Purpose: 1.) Backup all MySQL DBs/Tables, 2.) Purge LAMP Stack,
  4. # 3.) Re-Install LAMP Stack & PostGres, 4.) Re-install Data.
  5. # File: lamp-replace.sh
  6. # Source: The source of this code is from other working backup scripts
  7. # and from the online source at:
  8. # codegrepper.com/code-examples/shell/how+to+uninstall+lamp+
  9. # server+in+ubuntu+18.04
  10. # Tester: The source of this code is tested with:
  11. # shellcheck.net
  12. # Run with command
  13. # CMD: bash ../Scripts/MySQL/lamp-replace.sh
  14. # Script is CRON capable
  15.  
  16. # Declare the functions:
  17. function mysql_bup () {
  18. # Backup MySQL
  19. # May have to additionally manually backup the phpmyadmin option DB as that
  20. # MySQL management DB sometime does not work/backup correctly.
  21. # This may require using PHPmyAdmin or MySQL Work Bench.
  22. echo "Backing up the DBs!";
  23. bkpath='/3T/backups/LAMP/'; # Declare the backup path
  24. dbuser=$USER; # Get the System user
  25. dbpass='nomened.1497'; # Get the DB password
  26. dbpath='/usr/bin'; # Declare the system path
  27. dbopts='--all-databases --events -A -C -f';
  28. mybupd="${bkpath}MySQL/"; # Declare MySQL backup path
  29. flname="mysql_dump_(`eval date +%Y-%m-%d`).tgz"; # Declare the backup file
  30. fullnm="${mybupd}${flname}"; # Declare full backup file
  31. echo "F=> ${fullnm}";
  32. mkdir -p ${bkpath}; # Create the backup path
  33. ls -al ${bkpath}; # Show the backup path
  34. if [[ -f $fullnm ]]; then # Remove old backup file(s)
  35. rm -f ${fullnm};
  36. else # Create the new backup file
  37. curdir=$(pwd); # Could not make this work outside
  38. cd ${bkpath}; # out of the if/else logic because
  39. touch ${flname}; # it has to be in the target dir.
  40. GZIP=$(which gzip); # Set GZIP and run backup
  41. ${dbpath}/mysqldump -u ${dbuser} -p${dbpass} ${dbopts} | $GZIP -9 > ${flname};
  42. cd ${curdir};
  43. fi
  44. } # end function nysql_bup
  45.  
  46. function confg_bup () {
  47. # Backup the config files
  48. echo "Backing up the CONFIG file!";
  49. bkcpth='/3T/backups/LAMP/Config/'; # Declare the def backup path
  50. apapth='/3T/backups/LAMP/Config/apache2'; # Declare apache2 backup path
  51. phppth='/3T/backups/LAMP/Config/php'; # Declare PHP backup path
  52. pmapth='/3T/backups/LAMP/Config/phpmyadmin'; # Declare PHPmyAdmin path
  53. myspth='/3T/backups/LAMP/Config/mysql'; # Declare mysql backup path
  54. pgapth='/3T/backups/LAMP/Config/postgres'; # Declare PostGres backup path
  55. mkdir -p ${bkcpth}; # Create the def backup path
  56. mkdir -p ${apapth}; # Create apache backup path
  57. mkdir -p ${phppth}; # Create php backup path
  58. mkdir -p ${pmapth}; # Create pma backup path
  59. mkdir -p ${myspth}; # Create mysql backup path
  60. mkdir -p ${pgapth}; # Create PostGres backup path
  61. cp '/etc/fstab' "${bkcpth}fstab"; # Copy FSTAB for drive defs
  62. cp -rf "/etc/apache2/" "${apapth}"; # Copy the Apache Configs
  63. cp -rf "/etc/php/" "${phppth}"; # Copy the PHP Configs
  64. cp -rf "/etc/phpmyadmin/" "${pmapth}"; # Copy the PHPmyADMIN Configs
  65. cp -rf "/etc/mysql/" "${myspth}"; # Copy the MySQL Configs
  66. cp -rf "/etc/mysql/" "${pgapth}"; # Copy the PostGres Configs
  67. # Pipe the "Show" result to a file and show with NANO
  68. resfil='/3T/Syncs/Files/configs_dump.txt'
  69. ls -al ${bkcpth} > ${resfil}; # Save the Def Configs
  70. ls -al ${apapth} >> ${resfil}; # Save the Apache Configs
  71. ls -al ${phppth} >> ${resfil}; # Save the PHP Configs
  72. ls -al ${pmapth} >> ${resfil}; # Save the PHPmyAdmin Configs
  73. ls -al ${myspth} >> ${resfil}; # Save the MySQL Configs
  74. ls -al ${pgapth} >> ${resfil}; # Save the PostGres Configs
  75. nano ${resfil}; # Show the Resilts File
  76. } # end function confg_bup
  77.  
  78. function prgap_prg () {
  79. # Remove Apache
  80. echo "Purging Apache2!";
  81. # service apache2 stop;
  82. # or
  83. pkill apache2;
  84. apt-get purge apache2 apache2-utils apache2.2-bin apache2-common
  85. apt remove apache2.*
  86. apt-get autoremove
  87. whereis apache2
  88. rm -rf /etc/apache2
  89. } # end function prgap_prg
  90.  
  91. function prgph_prg () {
  92. # Remove PHP
  93. echo "Purging PHP!";
  94. apt-get purge `dpkg -l | grep php7.2 | awk '{print $2}' | tr "\n" " "`
  95. apt-get purge php7.*
  96. apt-get autoremove --purge
  97. whereis php
  98. rm -rf /etc/php
  99. } # end function prgph_prg
  100.  
  101. function prgdb_prg () {
  102. # Remove MYSql
  103. echo "Purging MYSql!";
  104. # service mysql stop;
  105. # or
  106. pkill mysql;
  107. apt-get remove --purge *mysql\*
  108. apt-get remove --purge mysql-server mysql-client mysql-common -y
  109. #rm -rf /etc/mysql
  110. apt-get autoremove && apt-get autoclean
  111. } # end function prgdb_prg
  112.  
  113. function prgpg_prg () {
  114. # Remove PostGres
  115. echo "Purging PostGres!";
  116. # service postgres stop;
  117. # or
  118. pkill postgres;
  119. apt-get --purge remove postgresql
  120. apt-get purge postgresql*
  121. apt-get --purge remove postgresql postgresql-doc postgresql-common
  122. # The following lines should be a "do while" loop
  123. # while pkg in ??
  124. # apt-get --purge remove ${pkg}
  125. # > dpkg -l | grep postgres
  126. # Get the right var above before inacting the remain commands
  127. # rm -rf /var/lib/postgresql/
  128. # rm -rf /var/log/postgresql/
  129. # rm -rf /etc/postgresql/
  130. # apt-get autoremove && apt-get autoclean
  131. } # end function prgpg_prg
  132.  
  133. function ilamp_ins () {
  134. # Install LAMP
  135. echo "Re-Installing LAMP!";
  136. # There are conflicts in the "apt-get install" command line, which must
  137. # be resolved before inacting it. Notice it is covering: 1.) CURL,
  138. # 2.) MariaDB, 2.) MS-SQL, 4.) MongoDB, 5.) MySQL, 6.) MySQL Workbench,
  139. # 7.) PHP, 8) PHPmyAmin, 8.) PostGres, 10.) PGAdmin, 11.) SQLite,
  140. # 12.) Sybase.
  141. # These should be broken out and addressed individually. Probably as
  142. # individual functions.
  143. # Any conflicts between the "tasksel" and the "apt-get install" must be
  144. # found and corrected before execution.
  145. tasksel install lamp-server;
  146. mysql_upgrade;
  147. apt-get install -y mssql-server apache2 curl mysql-server mysql-client
  148. mysql-common mysql-workbench php phpmyadmin libapache2-mod-auth-mysql
  149. libapache2-mod-php php-mysql php-cli jam g++ gcc make libpam0g-dev
  150. libkrb5-dev pax ccache gcc-multilib g++-multilib flex suversion mongodb
  151. mongodb-client mongodb-server pg-backup-ctl pg-cloudconfig
  152. pg-rage-terminator-10 pgbackrest pgcharts pgcli pgdbf pgmodeler
  153. pgmodeler-common pgqd pgadmin4 pgadmin4-apache2 pgxnclient php-curl
  154. php-json php-cgi mariadb-server mariadb-client net-tools openssh-server
  155. tasksel sqlite3 sqlitebrowser libsqlite3-dev sybase
  156. mysql_upgrade;
  157. } # end function ilamp_ins
  158.  
  159. function ipgdb_ins () {
  160. # Install PostGres
  161. echo "Installing PostGres!";
  162. # See Notes from the function above
  163. } # end function ipgdb_ins
  164.  
  165. res=mysql_bup;
  166. res=confg_bup;
  167. #res=prgap_prg;
  168. #res=prgph_prg;
  169. #res=prgdb_prg;
  170. #res=prgpg_prg;
  171. #res=ilamp_ins;
  172. #res=ipgdb_ins;
  173. #reboot;
  174. exit;
  175.  
  176.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement