Learn about Centmin Mod LEMP Stack today
Register Now

Wordpress How To Manually Backup Wordpress Installs Created Using centmin.sh menu option 22

Discussion in 'Centmin Mod User Tutorials & Guides' started by eva2000, Mar 19, 2022.

Thread Status:
Not open for further replies.
  1. eva2000

    eva2000 Administrator Staff Member

    54,362
    12,198
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,763
    Local Time:
    1:51 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Centmin Mod has a Wordpress auto installer via centmin.sh menu option 22. When you use this option to create your Wordpress installation, it has additional config/include files added and optimizations make to Nginx vhost and Wordpress database as outlined at https://community.centminmod.com/th...l-vs-centmin-sh-menu-option-22-install.15435/.

    When it comes time to needing to backup this Wordpress installation, you will need to ensure you get all the additionally created files backed up too. Below are manual commands you can use to backup your Wordpress installation using common Linux command line tools like tar, pigz (multi-threaded gzip) and mysqldump/mysqlimport.

    You can do test runs for this backup/restore method on test hourly billed VPS servers (Upcloud - signees also get US$25 credits to use as well, Linode, DigitalOcean and Vultr) with test Wordpress instances until you feel comfortable doing it on your live Wordpress instance's server.

    Important - ensure you have enough disk free space to save your backups!



    First thing is ensure you have enough disk free space
    Code (Text):
    df -hT
    Filesystem     Type      Size  Used Avail Use% Mounted on
    devtmpfs       devtmpfs   16G     0   16G   0% /dev
    tmpfs          tmpfs      16G  300K   16G   1% /dev/shm
    tmpfs          tmpfs      16G  2.8G   13G  18% /run
    tmpfs          tmpfs      16G     0   16G   0% /sys/fs/cgroup
    /dev/md1       ext4       69G   60G  6.2G  91% /
    tmpfs          tmpfs      16G   44K   16G   1% /tmp
    /dev/md2       ext4      151G  129G   15G  90% /home
    tmpfs          tmpfs     3.2G     0  3.2G   0% /run/user/0
    

    Check disk usage for Wordpress files/directory and MySQL database name
    Code (Text):
    domain=yourdomain.com
    
    du -sh /home/nginx/domains/$domain
    64M     /home/nginx/domains/yourdomain.com
    
    du -sh /var/lib/mysql/wp2722311399db_28687
    1.8M    /var/lib/mysql/wp2722311399db_28687
    


    Now to the manual Wordpress backup steps.

    Step 1. Create a list of files and directories you want to backup.

    For listed directories, tar will recursively backup directories within the listed directories, so no need to list subdirectories.

    In SSH session logged into your server populate the domain variable with your Wordpress domain name (minus www). The domain variable only persists for current SSH session, so if you get disconnected from SSH or need to re-login to SSH, you will need to populate the variable again.
    Code (Text):
    domain=yourdomain.com
    

    Create a backup of your Wordpress cronjobs at /home/nginx/domains/$domain/cronjobs/wpbackup-tar-cronjobs.txt where domain=yourdomain.com and will auto populate
    Code (Text):
    crontab -l | grep $domain| egrep 'wp-cron.php|wp_updater' > /home/nginx/domains/$domain/cronjobs/wpbackup-tar-cronjobs.txt
    

    Create Wordpress database, username, password info script which contains the addons/mysqladmin_shell.sh command to recreate your Wordpress database, username and password permissions as outlined at
    https://community.centminmod.com/threads/mysqladmin_shell-sh-shell-based-addon.543/. The saved command will be in file at /home/nginx/domains/$domain/backup/mysql_db_user_recreate.sh
    Code (Text):
    db=$(cat $(ls -rt /root/centminlogs/ | grep "wordpress_addvhost_$domain-logins.log" | awk '{print "/root/centminlogs/"$0}') | awk '/^DB / {print $3}' | sed -n 1p)
    user=$(cat $(ls -rt /root/centminlogs/ | grep "wordpress_addvhost_$domain-logins.log" | awk '{print "/root/centminlogs/"$0}') | awk '/^DB / {print $3}' | sed -n 2p)
    pass=$(cat $(ls -rt /root/centminlogs/ | grep "wordpress_addvhost_$domain-logins.log" | awk '{print "/root/centminlogs/"$0}') | awk '/^DB / {print $3}' | sed -n 3p)
    echo "/usr/local/src/centminmod/addons/mysqladmin_shell.sh createuserdb $db $user $pass" > /home/nginx/domains/$domain/backup/mysql_db_user_recreate.sh
    cat /home/nginx/domains/$domain/backup/mysql_db_user_recreate.sh
    

    Create the tar-files.txt file at /home/wpbackups which contains all the directories and files you will backup using tar command tool.
    Code (Text):
    backupdir=/home/wpbackups
    mkdir -p $backupdir
    cd $backupdir
    
    echo "/usr/local/nginx/conf/autoprotect/$domain/autoprotect-$domain.conf
    /usr/local/nginx/conf/conf.d/$domain.conf
    /usr/local/nginx/conf/conf.d/$domain.ssl.conf
    /home/nginx/domains/$domain
    /usr/local/nginx/conf/wpincludes/$domain
    /root/tools/wp_updater_$domain.sh
    /usr/local/nginx/conf/ssl/$domain
    /root/.acme.sh/$domain
    $(ls -rt /root/centminlogs/ | grep wordpress_addvhost_$domain | awk '{print "/root/centminlogs/"$0}')" > $backupdir/tar-files.txt
    

    contents of tar-files.txt will end up like below populating and replacing $domain instances with defined domain=yourdomain.com
    Code (Text):
    /usr/local/nginx/conf/autoprotect/yourdomain.com/autoprotect-yourdomain.com.conf
    /usr/local/nginx/conf/conf.d/yourdomain.com.conf
    /usr/local/nginx/conf/conf.d/yourdomain.com.ssl.conf
    /home/nginx/domains/yourdomain.com
    /usr/local/nginx/conf/wpincludes/yourdomain.com
    /root/tools/wp_updater_yourdomain.com.sh
    /usr/local/nginx/conf/ssl/yourdomain.com
    /root/.acme.sh/yourdomain.com
    /root/centminlogs/centminmod_123.09beta01.b655_270221-172205_wordpress_addvhost_yourdomain.com-logins-ftp.log
    /root/centminlogs/centminmod_123.09beta01.b655_270221-172205_wordpress_addvhost_yourdomain.com-logins.log
    

    Now you run a command check to see if non-HTTPS yourdomain.com.conf and /root/.acme.sh/yourdomain.com exist and if they don't exist, remove them from tar-files.txt
    Code (Text):
    # check if directory exists or not first
    if [ ! -d /root/.acme.sh/$domain ]; then sed -i "/\/root\/.acme.sh\/$domain/d" $backupdir/tar-files.txt; fi
    # check if non-ssl nginx vhost exists or not first
    if [ ! -d /usr/local/nginx/conf/conf.d/$domain.conf ]; then sed -i "/\/usr\/local\/nginx\/conf\/conf.d\/$domain.conf/d" $backupdir/tar-files.txt; fi
    

    In this example as both non-HTTPS yourdomain.com.conf and /root/.acme.sh/yourdomain.com don't exist, the tar-files.txt is updated to remove those entries now and looks like this
    Code (Text):
    /usr/local/nginx/conf/autoprotect/yourdomain.com/autoprotect-yourdomain.com.conf
    /usr/local/nginx/conf/conf.d/yourdomain.com.ssl.conf
    /home/nginx/domains/yourdomain.com
    /usr/local/nginx/conf/wpincludes/yourdomain.com
    /root/tools/wp_updater_yourdomain.com.sh
    /usr/local/nginx/conf/ssl/yourdomain.com
    /root/centminlogs/centminmod_123.09beta01.b655_270221-172205_wordpress_addvhost_yourdomain.com-logins-ftp.log
    /root/centminlogs/centminmod_123.09beta01.b655_270221-172205_wordpress_addvhost_yourdomain.com-logins.log
    

    You can now take this opportunity to add any additional directory or file full paths to tar-files.txt to include in your backup.

    Step 2. Run tar to backup Wordpress directories and files

    Now that step 1 created tar-files.txt has been created, you can run tar command to backup all the files listed in tar-files.txt excluding any log files from /home/nginx/domains/yourdomain.com/log/*.log* and save the tar backup archive at /home/wpbackups
    Code (Text):
    backupdir=/home/wpbackups
    mkdir -p $backupdir
    cd $backupdir
    
    # pigz multi-threaded gzip compressed tar backup to wpbackup.tar.gz
    tar "-I pigz -4" -cvpf $backupdir/wpbackup.tar.gz --exclude=*/log/*.log* --files-from tar-files.txt 2> $backupdir/tar-stderr.log && cat $backupdir/tar-stderr.log | grep -v 'Removing leading'
    

    You can test your backup archive using command below to list the backup's contents
    Code (Text):
    # test
    tar -tf $backupdir/wpbackup.tar.gz
    

    To restore from backup the command would be as follows. It will overwrite whatever existing data that exists on the server you run this command with
    Code (Text):
    tar xvf wpbackup.tar.gz -C /
    

    If you don't want to overwrite files and only want to extract to a directory to selective restore, use command
    Code (Text):
    tar xvf wpbackup.tar.gz -C /your-desired-temp-directory
    

    Then once restored, find the /home/nginx/domains/$domain/cronjobs/wpbackup-tar-cronjobs.txt backup file and you need to manually re-add those 2 listed cronjobs to your restored systems cronjobs.

    Step 3. Backup Wordpress MySQL database

    Next up is backing up Wordpress MySQL database. The below commands will populate variables for your SSH session, create the backup directory and update WP CLI command line tool and then use mysqldump to backup database name into tab delimited individual database table .txt and .SQL files in directory /home/wpbackups/timestamped
    Code (Text):
    domain=yourdomain.com
    wpinstall_dir=/home/nginx/domains/${domain}/public
    backupdir=/home/wpbackups
    dt=$(date +"%d%m%y-%H%M%S")
    mkdir -p $backupdir/$dt
    chown mysql:mysql $backupdir/$dt
    /usr/local/src/centminmod/addons/wpcli.sh update
    cd $backupdir/$dt
    pwd
    allvars=$(wp config get --path=$wpinstall_dir --format=json)
    dbname=$(wp config get DB_NAME --path=$wpinstall_dir)
    dbname=$(cat $(ls -rt /root/centminlogs/ | grep "wordpress_addvhost_$domain-logins.log" | awk '{print "/root/centminlogs/"$0}') | awk '/^DB / {print $3}' | sed -n 1p)
    dbsize=$(wp db size --path=$wpinstall_dir --size_format=b)
    
    backupopt="--default-character-set=utf8mb4 -Q -K --max_allowed_packet=768M --net_buffer_length=65536 --routines --events --triggers --hex-blob"
    mysqldump $backupopt --tab $backupdir/$dt $dbname
    chown -R mysql:mysql $backupdir/$dt
    

    Example for mysqldump backup command output - below is the normal output
    Code (Text):
    mysqldump $backupopt --tab $backupdir/$dt $dbname
    
    --
    -- Dumping events for database 'wp2722311399db_28687'
    --
    
    --
    -- Dumping routines for database 'wp2722311399db_28687'
    --
    

    Example individual MySQL database table files where .sql file is used to recreate the table structure and .txt is the actual MySQL database table's data.
    Code (Text):
    ls -lhArt /home/wpbackups/180322-161456
    total 116K
    -rw-r--r-- 1 mysql mysql 1.7K Mar 18 16:15 29240_commentmeta.sql
    -rw-r--r-- 1 mysql mysql 2.7K Mar 18 16:15 29240_comments.sql
    -rw-rw-rw- 1 mysql mysql    0 Mar 18 16:15 29240_commentmeta.txt
    -rw-rw-rw- 1 mysql mysql    0 Mar 18 16:15 29240_links.txt
    -rw-r--r-- 1 mysql mysql 2.3K Mar 18 16:15 29240_links.sql
    -rw-rw-rw- 1 mysql mysql  344 Mar 18 16:15 29240_comments.txt
    -rw-r--r-- 1 mysql mysql 1.8K Mar 18 16:15 29240_options.sql
    -rw-rw-rw- 1 mysql mysql   60 Mar 18 16:15 29240_postmeta.txt
    -rw-r--r-- 1 mysql mysql 1.7K Mar 18 16:15 29240_postmeta.sql
    -rw-rw-rw- 1 mysql mysql  32K Mar 18 16:15 29240_options.txt
    -rw-rw-rw- 1 mysql mysql 7.4K Mar 18 16:15 29240_posts.txt
    -rw-r--r-- 1 mysql mysql 3.1K Mar 18 16:15 29240_posts.sql
    -rw-rw-rw- 1 mysql mysql    6 Mar 18 16:15 29240_term_relationships.txt
    -rw-r--r-- 1 mysql mysql 1.7K Mar 18 16:15 29240_term_relationships.sql
    -rw-rw-rw- 1 mysql mysql   18 Mar 18 16:15 29240_term_taxonomy.txt
    -rw-r--r-- 1 mysql mysql 1.9K Mar 18 16:15 29240_term_taxonomy.sql
    -rw-rw-rw- 1 mysql mysql    0 Mar 18 16:15 29240_termmeta.txt
    -rw-r--r-- 1 mysql mysql 1.7K Mar 18 16:15 29240_termmeta.sql
    -rw-rw-rw- 1 mysql mysql   32 Mar 18 16:15 29240_terms.txt
    -rw-r--r-- 1 mysql mysql 1.7K Mar 18 16:15 29240_terms.sql
    -rw-rw-rw- 1 mysql mysql  464 Mar 18 16:15 29240_usermeta.txt
    -rw-r--r-- 1 mysql mysql 1.7K Mar 18 16:15 29240_usermeta.sql
    -rw-rw-rw- 1 mysql mysql  215 Mar 18 16:15 29240_users.txt
    -rw-r--r-- 1 mysql mysql 2.2K Mar 18 16:15 29240_users.sql
    

    If you want to restore the database from these individual files, use the mysqlimport command like below.

    First, if you restored this on a new system, you would need to find the restored file backup created for /home/nginx/domains/$domain/backup/mysql_db_user_recreate.sh where $domain = yourdomain.com and either manually run the command inside /home/nginx/domains/$domain/backup/mysql_db_user_recreate.sh or run it via bash
    Code (Text):
    domain=yourdomain.com
    bash /home/nginx/domains/$domain/backup/mysql_db_user_recreate.sh

    This will create the Wordpress database and MySQL username/password which will be same as your live Wordpress install so you won't need to edit wp-config.php after restoration.

    Then populate the variables below as appropriate for /home/wpbackups/180322-161456 backup directory
    Code (Text):
    # restore tab delimited format
    # 1st you concatenate all the mysqldump created *.sql table create sql files and pipe them into mysql database
    # 2nd you use mysqlimport set number of cpu threads for parallel import of all *.txt files which contain actual
    # mysqldump backed up data for each database table
    
    domain=yourdomain.com
    wpinstall_dir=/home/nginx/domains/${domain}/public
    dbname=$(wp config get DB_NAME --path=$wpinstall_dir)
    dbname=$(cat $(ls -rt /root/centminlogs/ | grep "wordpress_addvhost_$domain-logins.log" | awk '{print "/root/centminlogs/"$0}') | awk '/^DB / {print $3}' | sed -n 1p)
    cpus=$(nproc)
    dbbackupdir=/home/wpbackups/180322-161456
    time (mysql -e "drop database if exists $dbname;create database $dbname;"; (cat $dbbackupdir/*.sql) | mysql --default-character-set=utf8mb4 $dbname; mysqlimport --default-character-set=utf8mb4 --use-threads=$cpus $dbname $dbbackupdir/*.txt)
    

    Example mysqlimport restore output
    Code (Text):
    time (mysql -e "drop database if exists $dbname;create database $dbname;"; (cat $dbbackupdir/*.sql) | mysql --default-character-set=utf8mb4 $dbname; mysqlimport --default-character-set=utf8mb4 --use-threads=$cpus $dbname $dbbackupdir/*.txt)
    wp2722311399db_28687.29240_commentmeta: Records: 0  Deleted: 0  Skipped: 0  Warnings: 0
    wp2722311399db_28687.29240_comments: Records: 1  Deleted: 0  Skipped: 0  Warnings: 0
    wp2722311399db_28687.29240_termmeta: Records: 0  Deleted: 0  Skipped: 0  Warnings: 0
    wp2722311399db_28687.29240_term_relationships: Records: 1  Deleted: 0  Skipped: 0  Warnings: 0
    wp2722311399db_28687.29240_links: Records: 0  Deleted: 0  Skipped: 0  Warnings: 0
    wp2722311399db_28687.29240_term_taxonomy: Records: 1  Deleted: 0  Skipped: 0  Warnings: 0
    wp2722311399db_28687.29240_postmeta: Records: 2  Deleted: 0  Skipped: 0  Warnings: 0
    wp2722311399db_28687.29240_users: Records: 1  Deleted: 0  Skipped: 0  Warnings: 0
    wp2722311399db_28687.29240_usermeta: Records: 15  Deleted: 0  Skipped: 0  Warnings: 0
    wp2722311399db_28687.29240_terms: Records: 1  Deleted: 0  Skipped: 0  Warnings: 0
    wp2722311399db_28687.29240_posts: Records: 3  Deleted: 0  Skipped: 0  Warnings: 0
    wp2722311399db_28687.29240_options: Records: 130  Deleted: 0  Skipped: 0  Warnings: 0
    
    real    0m0.068s
    user    0m0.011s
    sys     0m0.011s
    



    If you want to backup /home/wpbackups/180322-161456 using tar to /home/wpbackups/wpbackup-mysql.tar.gz
    Code (Text):
    backupdir=/home/wpbackups
    tar "-I pigz -4" -cvpf $backupdir/wpbackup-mysql.tar.gz /home/wpbackups/180322-161456 2> $backupdir/tar-mysql-stderr.log && cat $backupdir/tar-mysql-stderr.log | grep -v 'Removing leading'
    

    Example output
    Code (Text):
    tar "-I pigz -4" -cvpf $backupdir/wpbackup-mysql.tar.gz /home/wpbackups/180322-161456 2> $backupdir/tar-mysql-stderr.log && cat $backupdir/tar-mysql-stderr.log | grep -v 'Removing leading'
    /home/wpbackups/180322-161456/
    /home/wpbackups/180322-161456/29240_links.txt
    /home/wpbackups/180322-161456/29240_term_taxonomy.sql
    /home/wpbackups/180322-161456/29240_users.txt
    /home/wpbackups/180322-161456/29240_term_relationships.sql
    /home/wpbackups/180322-161456/29240_options.sql
    /home/wpbackups/180322-161456/29240_commentmeta.txt
    /home/wpbackups/180322-161456/29240_postmeta.txt
    /home/wpbackups/180322-161456/29240_options.txt
    /home/wpbackups/180322-161456/29240_terms.txt
    /home/wpbackups/180322-161456/29240_termmeta.txt
    /home/wpbackups/180322-161456/29240_usermeta.txt
    /home/wpbackups/180322-161456/29240_usermeta.sql
    /home/wpbackups/180322-161456/29240_comments.txt
    /home/wpbackups/180322-161456/29240_terms.sql
    /home/wpbackups/180322-161456/29240_users.sql
    /home/wpbackups/180322-161456/29240_posts.txt
    /home/wpbackups/180322-161456/29240_term_taxonomy.txt
    /home/wpbackups/180322-161456/29240_comments.sql
    /home/wpbackups/180322-161456/29240_posts.sql
    /home/wpbackups/180322-161456/29240_links.sql
    /home/wpbackups/180322-161456/29240_commentmeta.sql
    /home/wpbackups/180322-161456/29240_postmeta.sql
    /home/wpbackups/180322-161456/29240_termmeta.sql
    /home/wpbackups/180322-161456/29240_term_relationships.txt
    


    You'll end up with /home/wpbackups directory looking like the below output. Where:
    • wpbackup.tar.gz contains all the file/directory backups
    • wpbackup-mysql.tar.gz contains all the MySQL database table file backups
    • 180322-161456 directory contains uncompressed MySQL database table file backups
    • tar-files.txt list of files/directories to backup
    • tar-stderr.log and tar-mysql-stderr.log logs are stderr output logs
    Code (Text):
    ls -lhArt /home/wpbackups/
    total 17M
    -rw-r--r-- 1 root  root   515 Mar 18 16:07 tar-files.txt
    -rw-r--r-- 1 root  root    93 Mar 18 16:07 tar-stderr.log
    -rw-r--r-- 1 root  root   17M Mar 18 16:07 wpbackup.tar.gz
    drwxr-xr-x 2 mysql mysql 4.0K Mar 18 16:36 180322-161456
    -rw-r--r-- 1 root  root    44 Mar 18 16:36 tar-mysql-stderr.log
    -rw-r--r-- 1 root  root   13K Mar 18 16:36 wpbackup-mysql.tar.gz
    

    contents of tar-stderr.log
    Code (Text):
    cat /home/wpbackups/tar-stderr.log
    tar: Removing leading `/' from member names
    tar: Removing leading `/' from hard link targets
    

    You can ignore the Removing leading / from messages as that is normal.

    That's basically how you backup and restore your Centmin Mod created Wordpress installation created via centmin.sh menu option 22. These commands outlined above and for Centmin Mod to Centmin Mod data migration at https://community.centminmod.com/threads/centmin-mod-site-data-migration-guide.10382/ are common commands which can be used to backup and transfer any Linux systems data and not just applicable to Centmin Mod. So read up on them and Google search for more tutorials and readings ;)
     
    Last edited: Mar 19, 2022
Thread Status:
Not open for further replies.