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

Sysadmin PHPBU CentMinMod Backup Tutorial

Discussion in 'System Administration' started by Benjamin74, Aug 19, 2019.

  1. Benjamin74

    Benjamin74 Premium Member Premium Member

    46
    4
    8
    May 2, 2016
    Ratings:
    +10
    Local Time:
    2:24 PM
    Hello guys,

    I've discovered a pretty nice backup tool to backup everything I need : mysql databases, sites files, vhosts, cmm custom files, cron, etc...

    It's called PHPBU:

    sebastianfeldmann/phpbu

    Since the tool is well documented but connecting the dots is not that easy, here is a quick tutorial.

    Install the tool in the root/tools directory of your VPS:

    Code:
    cd /root/tools
    wget https://phar.phpbu.de/phpbu.phar
    php phpbu.phar –version
    Create an Amazon SES IAM username with policy "AmazonSesSendingAccess" attached to it.

    Create a verified sender in amazon SES.

    Maybe you then need to configure something to use SMTP for this username, I can't remember.

    => Now you should have a verified amazon SES sender, and a username and password for the IAM user.

    Go to Dropbox: Login - Dropbox click on: "Create App" > "Dropbox API" > "App folder – Access to a single folder created specifically for your app." > Give it a name, e.g. CMMPhpBuBackup > You'll end up on the App settings page, there click "generate" under "Generated access token", you'll end up with a veryyyyylooooonnngggggggsssssttttrrrriiinnnng.

    On the vps, create a phpbu.xml file in the /root/tools folder:

    Code:
    nano phpbu.xml
    Now you can modify my config file below to match your:
    - amazon ses settings
    - dropbox settings
    - hostname (MYHOST in sample)
    - the domain to backup (yourdomain.com in sample), you can easily add as many domains and mysql as you want by just adding multiple <backup ...> blocks

    HTML:
    <?xml version="1.0" encoding="UTF-8"?>
    <phpbu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="http://schema.phpbu.de/5.2/phpbu.xsd">
      <logging>
        <log type="mail">
         <option name="recipients" value="[email protected]"/>
         <!-- set next line to true once you've verified that mailing works OK so you'll only get errors -->
         <option name="sendOnlyOnError" value="false"/>
         <option name="transport" value="smtp"/>
         <option name="sender.mail" value="[email protected]"/>
         <option name="smtp.port" value="587"/>
         <!-- change next line depending on your amazon SES SMTP region -->
         <option name="smtp.host" value="email-smtp.eu-west-1.amazonaws.com"/>
         <option name="smtp.username" value="AMAZONSESIAMUSERNAME"/>
         <option name="smtp.password" value="AMAZONSESIAMPASSWORDTHATISQUITELONGTOO"/>
         <option name="smtp.encryption" value="tls"/>
        </log>
      </logging>
      <backups>
        <!-- ============================================================== -->
        <!--        PART 1 - BACKUP CENTMINMOD RELATED FILED                -->
        <!-- ============================================================== -->
        <!-- ============================================== -->
        <!-- FILES BACKUP FOR CMM CONFIG.   .               -->
        <!-- ============================================== -->
        <backup name="CMM-MYHOST">
          <!-- backup source -->
          <source type="tar">
            <option name="path" value="/etc/centminmod"/>
          </source>
          <!-- where should the backup be stored -->
          <target dirname="/root/tools/backup/cmm"
                  filename="BACKUP-CMM-MYHOST-%Y%m%d-%H%i.sql"
                  compress="bzip2"/>
          <sync type="dropbox">
            <option name="token" value="yoursuperlongdropboxappapikeywillgohere"/>
            <option name="path" value="/backups-cmm"/>
            <option name="cleanup.type" value="stepwise" />
            <!-- Days to keep all backups you made. -->
            <option name="cleanup.daysToKeepAll" value="2" />
            <!-- Days to keep at least one backup you made a day -->
            <option name="cleanup.daysToKeepDaily" value="3" />
            <!-- Amount of weeks to keep at least one backup for each week. -->
            <option name="cleanup.weeksToKeepWeekly" value="4" />
            <!-- Amount of month to keep at least one backup for each month. -->
            <option name="cleanup.monthToKeepMonthly" value="12" />
            <!-- Amount of years to keep at least one backup for each year. -->
            <option name="cleanup.yearsToKeepYearly" value="10" />
          </sync>
          <!-- cleanup the backup location
               as soon as all created backups exceed 10MB
               remove oldest backups -->
          <cleanup type="Capacity">
            <option name="size" value="10M"/>
          </cleanup>
        </backup>
        <!-- ============================================== -->
        <!-- FILES BACKUP FOR CRON CONFIG.   .             -->
        <!-- ============================================== -->
        <backup name="CRON-MYHOST">
          <!-- backup source -->
          <source type="tar">
            <option name="path" value="/var/spool/cron"/>
          </source>
          <!-- where should the backup be stored -->
          <target dirname="/root/tools/backup/cron"
                  filename="BACKUP-CRON-MYHOST-%Y%m%d-%H%i.sql"
                  compress="bzip2"/>
          <sync type="dropbox">
            <option name="token" value="yoursuperlongdropboxappapikeywillgohere"/>
            <option name="path" value="/backups-cron"/>
            <option name="cleanup.type" value="stepwise" />
            <!-- Days to keep all backups you made. -->
            <option name="cleanup.daysToKeepAll" value="2" />
            <!-- Days to keep at least one backup you made a day -->
            <option name="cleanup.daysToKeepDaily" value="3" />
            <!-- Amount of weeks to keep at least one backup for each week. -->
            <option name="cleanup.weeksToKeepWeekly" value="4" />
            <!-- Amount of month to keep at least one backup for each month. -->
            <option name="cleanup.monthToKeepMonthly" value="12" />
            <!-- Amount of years to keep at least one backup for each year. -->
            <option name="cleanup.yearsToKeepYearly" value="10" />
          </sync>
          <!-- cleanup the backup location
               as soon as all created backups exceed 10MB
               remove oldest backups -->
          <cleanup type="Capacity">
            <option name="size" value="10M"/>
          </cleanup>
        </backup>
        <!-- ============================================== -->
        <!-- FILES BACKUP FOR VHOST CONFIG.   .             -->
        <!-- ============================================== -->
        <backup name="VHOSTS-MYHOST">
          <!-- backup source -->
          <source type="tar">
            <option name="path" value="/usr/local/nginx/conf/conf.d"/>
          </source>
          <!-- where should the backup be stored -->
          <target dirname="/root/tools/backup/vhost"
                  filename="BACKUP-VHOST-MYHOST-%Y%m%d-%H%i.sql"
                  compress="bzip2"/>
          <sync type="dropbox">
            <option name="token" value="yoursuperlongdropboxappapikeywillgohere"/>
            <option name="path" value="/backups-vhost"/>
            <option name="cleanup.type" value="stepwise" />
            <!-- Days to keep all backups you made. -->
            <option name="cleanup.daysToKeepAll" value="2" />
            <!-- Days to keep at least one backup you made a day -->
            <option name="cleanup.daysToKeepDaily" value="3" />
            <!-- Amount of weeks to keep at least one backup for each week. -->
            <option name="cleanup.weeksToKeepWeekly" value="4" />
            <!-- Amount of month to keep at least one backup for each month. -->
            <option name="cleanup.monthToKeepMonthly" value="12" />
            <!-- Amount of years to keep at least one backup for each year. -->
            <option name="cleanup.yearsToKeepYearly" value="10" />
          </sync>
          <!-- cleanup the backup location
               as soon as all created backups exceed 10MB
               remove oldest backups -->
          <cleanup type="Capacity">
            <option name="size" value="10M"/>
          </cleanup>
        </backup>
        <!-- ============================================================== -->
        <!--        PART 2 - BACKUP MYSQL & FILES FOR SITES/APPS            -->
        <!-- ============================================================== -->
        <!-- ============================================== -->
        <!-- MYSQL BACKUP FOR yourdomain.com.             -->
        <!-- ============================================== -->
        <backup name="MYSQL-yourdomain.com">
          <!-- backup source -->
          <source type="mysqldump">
            <option name="databases" value="wp9836738627db_1686"/>
            <option name="user" value="wpdb8872u32119"/>
            <option name="password" value="jghvsgfsgjlzizgzfsgyskuzyz76562"/>
          </source>
          <!-- where should the backup be stored -->
          <target dirname="/home/nginx/domains/yourdomain.com/backup/mysql"
                  filename="BACKUP-yourdomain.com-MYSQL-%Y%m%d-%H%i.sql"
                  compress="bzip2"/>
          <sync type="dropbox">
            <option name="token" value="yoursuperlongdropboxappapikeywillgohere"/>
            <option name="path" value="/backups-mysql"/>
            <option name="cleanup.type" value="stepwise" />
            <!-- Days to keep all backups you made. -->
            <option name="cleanup.daysToKeepAll" value="2" />
            <!-- Days to keep at least one backup you made a day -->
            <option name="cleanup.daysToKeepDaily" value="7" />
            <!-- Amount of weeks to keep at least one backup for each week. -->
            <option name="cleanup.weeksToKeepWeekly" value="8" />
            <!-- Amount of month to keep at least one backup for each month. -->
            <option name="cleanup.monthToKeepMonthly" value="12" />
            <!-- Amount of years to keep at least one backup for each year. -->
            <option name="cleanup.yearsToKeepYearly" value="10" />
          </sync>
          <!-- cleanup the backup location
               as soon as all created backups exceed 100MB
               remove oldest backups -->
          <cleanup type="Capacity">
            <option name="size" value="100M"/>
          </cleanup>
        </backup>
        <!-- ============================================== -->
        <!-- FILES BACKUP FOR yourdomain.com.             -->
        <!-- ============================================== -->
        <backup name="FILES-yourdomain.com">
          <!-- backup source -->
          <source type="tar">
            <option name="path" value="/home/nginx/domains/yourdomain.com"/>
            <option name="exclude" value="backup"/>
          </source>
          <!-- where should the backup be stored -->
          <target dirname="/home/nginx/domains/yourdomain.com/backup/files"
                  filename="BACKUP-yourdomain.com-FILES-%Y%m%d-%H%i.sql"
                  compress="bzip2"/>
          <sync type="dropbox">
            <option name="token" value="yoursuperlongdropboxappapikeywillgohere"/>
            <option name="path" value="/backups-files"/>
            <option name="cleanup.type" value="stepwise" />
            <!-- Days to keep all backups you made. -->
            <option name="cleanup.daysToKeepAll" value="2" />
            <!-- Days to keep at least one backup you made a day -->
            <option name="cleanup.daysToKeepDaily" value="7" />
            <!-- Amount of weeks to keep at least one backup for each week. -->
            <option name="cleanup.weeksToKeepWeekly" value="8" />
            <!-- Amount of month to keep at least one backup for each month. -->
            <option name="cleanup.monthToKeepMonthly" value="12" />
            <!-- Amount of years to keep at least one backup for each year. -->
            <option name="cleanup.yearsToKeepYearly" value="10" />
          </sync>
          <!-- cleanup the backup location
               as soon as all created backups exceed 600MB
               remove oldest backups -->
          <cleanup type="Capacity">
            <option name="size" value="600M"/>
          </cleanup>
        </backup>
      </backups>
    </phpbu>
    Now you can run phpbu to see if it works :

    Code:
    php phpbu.phar --verbose
    If it's all fine you can then set a cronjob to run it :

    Code:
    crontab -e
    and add a line like that:

    Code:
    17 2 * * * /usr/local/bin/php /root/tools/phpbu.phar --configuration=/root/tools/phpbu.xml
    If everything runs fine, you should get some clean folders in your Dropbox/Applications/YourDropboxAppName :

    Code:
    backups-cmm
    backups-cron
    backups-files
    backups-mysql
    backups-vhost
    and receive some emails about your backup routine status.

    Else phpbu is well documented so with this sample and their documentation, you should be able to do whatever you want.

    PHPBU Manual – Chapter 1. Installing PHPBU

    Have fun!
     
    • Informative Informative x 1
  2. eva2000

    eva2000 Administrator Staff Member

    41,350
    9,279
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,236
    Local Time:
    10:24 PM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    thanks for sharing. Never heard or used that script. But just be aware with PHP based backup scripts as they can be retricted by server's PHP max timeout settings etc, there can be possibility of large backups ending up incomplete as PHP may timeout before completing the backup runs.
     
    • Agree Agree x 2
  3. Benjamin74

    Benjamin74 Premium Member Premium Member

    46
    4
    8
    May 2, 2016
    Ratings:
    +10
    Local Time:
    2:24 PM
    Apparently it's working even with very large (I tested with 4.2GB random files) backup.

    Took 43 minutes but worked well.

    I guess it's probably coded similarly to the "All in One WP migration" plugin, i.e. it does work no matter what's the max execution time of php.
     
  4. pamamolf

    pamamolf Premium Member Premium Member

    3,443
    329
    83
    May 31, 2014
    Ratings:
    +627
    Local Time:
    3:24 PM
    Nginx-1.17.x
    MariaDB 10.3.x
    I prefer a bash script for that job :)
     
    • Agree Agree x 1
  5. Benjamin74

    Benjamin74 Premium Member Premium Member

    46
    4
    8
    May 2, 2016
    Ratings:
    +10
    Local Time:
    2:24 PM
    I would too if I had found a good **and** easy one with :

    - backup to dropbox
    - rotation of backups / cleaning
    - backup files and databases

    Unfortunately I haven't been able to find anything really easy to use.
     
  6. pamamolf

    pamamolf Premium Member Premium Member

    3,443
    329
    83
    May 31, 2014
    Ratings:
    +627
    Local Time:
    3:24 PM
    Nginx-1.17.x
    MariaDB 10.3.x
    Because maybe Dropbox will not be a good solution?

    Not very fast or limited free space or both?
     
  7. pamamolf

    pamamolf Premium Member Premium Member

    3,443
    329
    83
    May 31, 2014
    Ratings:
    +627
    Local Time:
    3:24 PM
    Nginx-1.17.x
    MariaDB 10.3.x