BASH   54

backup databases

Guest on 15th May 2022 04:49:17 PM

  1. #!/bin/bash
  2.  
  3. # Backup $FIND_PATH
  4. BACKUP_SITES=yes
  5.  
  6. # Backup databases
  7. BACKUP_DATABASES=yes
  8.  
  9. # Delete local backups older than x days (0 = don't delete)
  10. DELETE_OLDER_THAN=7
  11.  
  12. # Send backups to remote server
  13. SEND_REMOTE=yes
  14.  
  15. # Where all the domains are located
  16. FIND_PATH="/home/*/domains/*"
  17.  
  18. # Used to filter domain name from its full system path
  19. # E.g. (1)/home(2)/my(3)/domains(4)/mydomain.com(5)
  20. AWK_DOMAIN_POS="5"
  21. AWK_DOMAIN_OWNER_POS="3"
  22.  
  23. # Where all the local backups are stored
  24. BACKUP_FOLDER="/backups"
  25.  
  26. # Database credientals
  27. DATABASE_USER="{{DATABASE_USER}}"
  28. DATABASE_PASS="{{DATABASE_PASS}}"
  29.  
  30. REMOTE_HOST="{{REMOTE_HOST}}"
  31. REMOTE_USER="{{REMOTE_USER}}"
  32. REMOTE_FOLDER="~/backups"
  33. REMOTE_ADDRESS="$REMOTE_USER@$REMOTE_HOST:$REMOTE_FOLDER"
  34.  
  35.  
  36.  
  37. # This will be the name for the backup folder
  38. CURRENT_TIMESTAMP=$(date +"%Y-%m-%d")
  39.  
  40. # Backup files will be appended by this
  41. UNIXTIME=$(date +"%s")
  42.  
  43. # Check if rsync is installed
  44. if [ $SEND_REMOTE = "yes" ]; then
  45.     command -v rsync >/dev/null 2>&1 || { echo -e >&2 "\e[97mRsync is not installed. Install it or set SEND_REMOTE to \e[1mno\e[0m\e[39m"; exit 1; }
  46. fi
  47.  
  48. # Get list of domains
  49. find $FIND_PATH -maxdepth 0 > /tmp/domain-$UNIXTIME.txt
  50.  
  51. # Get list of databases
  52. mysql --user=$DATABASE_USER --password=$DATABASE_PASS -e "SHOW DATABASES;" | tr -d "| " | grep -Ev "(Database|information_schema|performance_schema|mysql)" > /tmp/database-$UNIXTIME.txt
  53.  
  54. # Create backup folder
  55. mkdir -p $BACKUP_FOLDER/$CURRENT_TIMESTAMP/{domains,databases}
  56.  
  57. # Compress all domains
  58. if [ $BACKUP_SITES = "yes" ]; then
  59.     for site in $(cat /tmp/domain-$UNIXTIME.txt)
  60.     do
  61.         DOMAIN_OWNER=$(echo $site | awk -F"/" '{ print $'${AWK_DOMAIN_OWNER_POS}' }')
  62.         DOMAIN_NAME=$(echo $site | awk -F"/" '{ print $'${AWK_DOMAIN_POS}' }')
  63.         DOMAIN_BACKUP_NAME="$DOMAIN_OWNER-$DOMAIN_NAME-$UNIXTIME.tar.gz"
  64.  
  65.         echo -en "\e[97mBackupping site \e[1m$DOMAIN_NAME\e[0m\e[97m ... \e[39m"
  66.  
  67.         tar -zcPf $BACKUP_FOLDER/$CURRENT_TIMESTAMP/domains/$DOMAIN_BACKUP_NAME $site
  68.  
  69.         if [ $? -eq 0 ]; then
  70.             echo -e "\e[97m[\e[92mOK\e[97m]\e[39m"
  71.         else
  72.             echo -e "\e[97m[\e[91mERROR\e[97m]\e[39m"
  73.         fi
  74.     done
  75. fi
  76.  
  77. # Compress all databases
  78. if [ $BACKUP_DATABASES = "yes" ]; then
  79.     for db in $(cat /tmp/database-$UNIXTIME.txt)
  80.     do
  81.         DATABASE_BACKUP_NAME="$db-$UNIXTIME.sql.gz"
  82.  
  83.         echo -en "\e[97mBackupping database \e[1m$db\e[0m\e[97m ... \e[39m"
  84.  
  85.         mysqldump -hlocalhost -u$DATABASE_USER -p$DATABASE_PASS $db | gzip -9 > $BACKUP_FOLDER/$CURRENT_TIMESTAMP/databases/$DATABASE_BACKUP_NAME
  86.  
  87.         if [ $? -eq 0 ]; then
  88.             echo -e "\e[97m[\e[92mOK\e[97m]\e[39m"
  89.         else
  90.             echo -e "\e[97m[\e[91mERROR\e[97m]\e[39m"
  91.         fi
  92.     done
  93. fi
  94.  
  95. # Delete old backups
  96. if [ $DELETE_OLDER_THAN -gt 0 ]; then
  97.     echo -en "\e[97mDeleting backups older than \e[1m$DELETE_OLDER_THAN days\e[0m\e[97m ... \e[39m"
  98.  
  99.     find $BACKUP_FOLDER -type d -ctime +$DELETE_OLDER_THAN | xargs rm -rf
  100.  
  101.     if [ $? -eq 0 ]; then
  102.         echo -e "\e[97m[\e[92mOK\e[97m]\e[39m"
  103.     else
  104.         echo -e "\e[97m[\e[91mERROR\e[97m]\e[39m"
  105.     fi
  106. fi
  107.  
  108. # Send files to remote server
  109. if [ $SEND_REMOTE = "yes" ]; then
  110.     echo -en "\e[97mSending backups to remote server ... \e[39m"
  111.  
  112.     rsync -a --ignore-existing $BACKUP_FOLDER/ $REMOTE_ADDRESS
  113.  
  114.     if [ $? -eq 0 ]; then
  115.         echo -e "\e[97m[\e[92mOK\e[97m]\e[39m"
  116.     else
  117.         echo -e "\e[97m[\e[91mERROR\e[97m]\e[39m"
  118.     fi
  119. fi
  120.  
  121. # Remove temp list files
  122. rm /tmp/domain-$UNIXTIME.txt
  123. rm /tmp/database-$UNIXTIME.txt

Raw Paste


Login or Register to edit or fork this paste. It's free.