Get the most out of your Centmin Mod LEMP stack
Become a Member

Wordpress create an array for wordpress installed

Discussion in 'Blogs & CMS usage' started by ahmed, Apr 12, 2017.

  1. ahmed

    ahmed Member

    116
    8
    18
    Feb 21, 2017
    Ratings:
    +13
    Local Time:
    5:50 PM
    Hello

    I have this script that backup the wordpress to google drive

    Code:
    #path to WordPress installations
    SITESTORE="/nginx/domains"
    
    #date prefix
    DATEFORM=$(date +"%Y-%m-%d")
    
    #Days to retain
    DAYSKEEP=7
    
    #calculate days as filename prefix
    DAYSKEPT=$(date +"%Y-%m-%d" -d "-$DAYSKEEP days")
    
    #create array of sites based on folder names
    SITELIST=($(ls -lh $SITESTORE | awk '{print $9}'))
    

    THE PROBLEM in the SITESTORE="/nginx/domains"

    the script catches the underlying domains and backup them perfectly
    but because the wordpress public folder lies below the domain folder WP-CLI dosnt understand it is a wordpress so it dosnt backup its SQL

    how to solve this please?
     
  2. eva2000

    eva2000 Administrator Staff Member

    26,465
    6,078
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +8,896
    Local Time:
    1:50 AM
    Nginx 1.11.x
    MariaDB 5.5
    not /home/nginx/domains ?

    clue for you

    Code (Text):
    SITESTORE='/home/nginx/domains'
    SITELIST=$(ls $SITESTORE)
    for s in ${SITELIST[0]}; do echo $s; done
    


    Code (Text):
    for s in ${SITELIST[0]}; do echo $s; done
    demodomain.com
    domain1.com
    domain.com
    


    Code (Text):
    for s in ${SITELIST[0]}; do echo -n "backup $s: "; echo "/home/nginx/${s}/public";done
    backup demodomain.com: /home/nginx/demodomain.com/public
    backup domain1.com: /home/nginx/domain1.com/public
    backup domain.com: /home/nginx/domain.com/public
     
  3. ahmed

    ahmed Member

    116
    8
    18
    Feb 21, 2017
    Ratings:
    +13
    Local Time:
    5:50 PM
    Code:
     #back up the WordPress folder
        tar -czf $BACKUPPATH/$s/$DATEFORM-$s.tar.gz .
        #back up the WordPress database, compress and clean up
        wp db export $BACKUPPATH/$s/$DATEFORM-$s.sql --allow-root --skip-themes --skip-plugins
        cat $BACKUPPATH/$s/$DATEFORM-$s.sql | gzip > $BACKUPPATH/$s/$DATEFORM-$s.sql.gz
        rm $BACKUPPATH/$s/$DATEFORM-$s.sql
    
    looks like I have a typo in this block, can u help please
     
  4. ahmed

    ahmed Member

    116
    8
    18
    Feb 21, 2017
    Ratings:
    +13
    Local Time:
    5:50 PM
    looks like I'm missing /public will check now
     
  5. ahmed

    ahmed Member

    116
    8
    18
    Feb 21, 2017
    Ratings:
    +13
    Local Time:
    5:50 PM
    it worked just fine,

    do I need to fix permission?

    Code:
    
    #Fix permissions
    sudo chown -R www-data:www-data $SITESTORE
    sudo find $SITESTORE -type f -exec chmod 644 {} +
    sudo find $SITESTORE -type d -exec chmod 755 {} +
    
     
  6. ahmed

    ahmed Member

    116
    8
    18
    Feb 21, 2017
    Ratings:
    +13
    Local Time:
    5:50 PM
    thanks, it worked ;)
     
  7. eva2000

    eva2000 Administrator Staff Member

    26,465
    6,078
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +8,896
    Local Time:
    1:50 AM
    Nginx 1.11.x
    MariaDB 5.5
    centmin mod files/directories are owned by nginx user and group
     
  8. ahmed

    ahmed Member

    116
    8
    18
    Feb 21, 2017
    Ratings:
    +13
    Local Time:
    5:50 PM
    hello again

    I'm adapting this script

    Code:
    #!/usr/bin/env bash
    # Source: https://guides.wp-bullet.com
    # Author: Mike
    
    #define local path for backups
    BACKUPPATH="/tmp/backups"
    
    #define remote backup path
    BACKUPPATHREM="WP Bullet Backups"
    
    #path to WordPress installations
    SITESTORE="/var/www"
    
    #date prefix
    DATEFORM=$(date +"%Y-%m-%d")
    
    #Days to retain
    DAYSKEEP=7
    
    #calculate days as filename prefix
    DAYSKEPT=$(date +"%Y-%m-%d" -d "-$DAYSKEEP days")
    
    #create array of sites based on folder names
    SITELIST=($(ls -lh $SITESTORE | awk '{print $9}'))
    
    #make sure the backup folder exists
    mkdir -p $BACKUPPATH
    
    #check remote backup folder exists on gdrive
    BACKUPSID=$(gdrive list --no-header | grep $BACKUPPATHREM | grep dir | awk '{ print $1}')
       if [ -z "$BACKUPSID" ]; then
           gdrive mkdir $BACKUPPATHREM
           BACKUPSID=$(gdrive list --no-header | grep $BACKUPPATHREM | grep dir | awk '{ print $1}')
       fi
    
    #start the loop
    for SITE in ${SITELIST[@]}; do
       #delete old backup, get folder id and delete if exists
       OLDBACKUP=$(gdrive list --no-header | grep $DAYSKEPT-$SITE | grep dir | awk '{ print $1}')
       if [ ! -z "$OLDBACKUP" ]; then
           gdrive delete $OLDBACKUP
       fi
    
       # create the local backup folder if it doesn't exist
       if [ ! -e $BACKUPPATH/$SITE ]; then
           mkdir $BACKUPPATH/$SITE
       fi
    
       #entire the WordPress folder
       cd $SITESTORE/$SITE
     
       #back up the WordPress folder
       tar -czf $BACKUPPATH/$SITENAME/$SITE/$DATEFORM-$SITE.tar.gz .
       #back up the WordPress database, compress and clean up
       wp db export $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql --allow-root --skip-themes --skip-plugins
       cat $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql | gzip > $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql.gz
       rm $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql
     
       #get current folder ID
       SITEFOLDERID=$(gdrive list --no-header | grep $SITE | grep dir | awk '{ print $1}')
    
       #create folder if doesn't exist
       if [ -z "$SITEFOLDERID" ]; then
           gdrive mkdir --parent $BACKUPSID $SITE
           SITEFOLDERID=$(gdrive list --no-header | grep $SITE | grep dir | awk '{ print $1}')
       fi
    
       #upload WordPress tar
       gdrive upload --parent $SITEFOLDERID --delete $BACKUPPATH/$SITE/$DATEFORM-$SITE.tar.gz
       #upload wordpress database
       gdrive upload --parent $SITEFOLDERID --delete $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql.gz
    
    done
    
    to centminmod

    Code:
    #!/usr/bin/env bash
    
    
    #define local path for backups
    BACKUPPATH="/tmp/backups"
    
    #define remote backup path
    BACKUPPATHREM="wp-gce-backups"
    
    #path to WordPress installations
    SITESTORE="/home/nginx/domains"
    
    #date prefix
    DATEFORM=$(date +"%Y-%m-%d")
    
    #Days to retain
    DAYSKEEP=7
    
    #calculate days as filename prefix
    DAYSKEPT=$(date +"%Y-%m-%d" -d "-$DAYSKEEP days")
    
    #create array of sites based on folder names
    SITELIST=$(ls $SITESTORE)
    for s in ${SITELIST[0]}; do echo -n "backup $s: "; echo "/home/nginx/${s}/public";done
    
    #SITELIST=($(ls -lh $SITESTORE | awk '{print $9}'))
    
    #make sure the backup folder exists
    mkdir -p $BACKUPPATH
    
    #check remote backup folder exists on gdrive
    BACKUPSID=$(gdrive list --no-header | grep $BACKUPPATHREM | grep dir | awk '{ print $1}')
        if [ -z "$BACKUPSID" ]; then
            gdrive mkdir $BACKUPPATHREM
            BACKUPSID=$(gdrive list --no-header | grep $BACKUPPATHREM | grep dir | awk '{ print $1}')
        fi
    
    #start the loop
    for s in ${SITELIST[@]}; do
        #delete old backup, get folder id and delete if exists
        OLDBACKUP=$(gdrive list --no-header | grep $DAYSKEPT-$s | grep dir | awk '{ print $1}')
        if [ ! -z "$OLDBACKUP" ]; then
            gdrive delete $OLDBACKUP
        fi
    
        # create the local backup folder if it doesn't exist
        if [ ! -e $BACKUPPATH/$s ]; then
            mkdir $BACKUPPATH/$s
        fi
    
        #entire the WordPress folder
        cd $SITESTORE/$s/public
     
        #back up the WordPress folder
        tar -czf $BACKUPPATH/$s/$DATEFORM-$s.tar.gz .
        #back up the WordPress database, compress and clean up
        wp db export $BACKUPPATH/$s/$DATEFORM-$s.sql --allow-root --skip-themes --skip-plugins
        cat $BACKUPPATH/$s/$DATEFORM-$s.sql | gzip > $BACKUPPATH/$s/$DATEFORM-$s.sql.gz
        rm $BACKUPPATH/$s/$DATEFORM-$s.sql
     
        #get current folder ID
        SITEFOLDERID=$(gdrive list --no-header | grep $s | grep dir | awk '{ print $1}')
    
        #create folder if doesn't exist
        if [ -z "$SITEFOLDERID" ]; then
            gdrive mkdir --parent $BACKUPSID $s
            SITEFOLDERID=$(gdrive list --no-header | grep $s | grep dir | awk '{ print $1}')
        fi
    
        #upload WordPress tar
        gdrive upload --parent $SITEFOLDERID --delete $BACKUPPATH/$s/$DATEFORM-$s.tar.gz
        #upload wordpress database
        gdrive upload --parent $SITEFOLDERID --delete $BACKUPPATH/$s/$DATEFORM-$s.sql.gz
    
    done
    [/CDOE]
    
    I managed to get it done with the help of @eva above
    
    however, I'm getting errors: mailnly to delete the old backups
    
    [CODE]
    Success: Exported to '/tmp/backups/DOMAIN.COM/2017-04-15-DOMAIN.COM.sql'.
    
    No valid arguments given, use 'gdrive help' to see available commands
    
    Failed to get file: googleapi: Error 404: File not found: --delete., notFound
    
    Failed to get file: googleapi: Error 404: File not found: --delete., notFound
    
    

    -also sometimes the WP-CLI dB backup is not complete when running clone

    Dropbox - Screenshot 2017-04-15 13.09.09.png


    please help
     
  9. eva2000

    eva2000 Administrator Staff Member

    26,465
    6,078
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +8,896
    Local Time:
    1:50 AM
    Nginx 1.11.x
    MariaDB 5.5
    you mean
    Code (Text):
    wp db export
    

    ??
    Depends on size of backups and server specs. That would be up to you.
     
  10. ahmed

    ahmed Member

    116
    8
    18
    Feb 21, 2017
    Ratings:
    +13
    Local Time:
    5:50 PM
    I made many improvement on the script, it backs up fine when run manually

    but backs up the db with 20 Byte size when run from the cron, very weird :)
     
  11. joshuaharradence

    joshuaharradence Member

    53
    4
    8
    Apr 3, 2017
    Ratings:
    +5
    Local Time:
    1:50 AM
    @ahmed share your script please.
     
  12. ahmed

    ahmed Member

    116
    8
    18
    Feb 21, 2017
    Ratings:
    +13
    Local Time:
    5:50 PM
    The script is above, but the WP-CLI DB export dosnt work in cron