Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

Export WP Posts and Sync to Sharepoint

Submitted by on November 14, 2017 – 12:24 pm

Long story short: old wiki is overloaded and broken; new wiki is not a wiki but SharePoint – a glorified file server; people are forced to improvise and put documentation on a WP wiki; bosses still want to see documentation in SharePoint; need a way to automatically sync new and updated posts from WP to SharePoint.

A couple of prerequisites to make this happen: the awesome wp2md utility and the pandoc package:

pip install git+https://github.com/dreikanter/wp2md
yum -y install pandoc
# Optional packages for converting to PDF
yum -y install texlive-latex pdflatex texlive

The default WP behavior when exporting posts is to prepend the filename with the date. Obviously, you want to remove this date prefix if you want exported posts to overwrite older files on SharePoint. The script does that.

This process is designed to run daily from cron and so it selects only those posts that have been created or modified in the past two days. You can adjust this as needed. You can download the script here.

#!/bin/bash
#                                      |
#                                  ___/"\___
#                          __________/ o \__________
#                            (I) (G) \___/ (O) (R)
#                                   Igor Os
#                           igor@comradegeneral.com
#                                 2017-11-14
configure() {
    this_time=$(date +'%Y-%m-%d_%H%M%S')
    # WP home folder
    wph=/opt/wp/html/wp
    # WP export target folder
    e=/opt/wp/wp_export
    # wp2md target folder
    w=/opt/wp/wp2md/${this_time}
    mkdir -p ${w}
    # Sharepoint upload URL
    sp_url="https://sharepoint_hsot/path/Shared%20Documents/wp/autosync"
    # your Sharepoint login credentials
    DOMAIN=COMPANY
    USERNAME=jdoe
    PASSWORD="yourP@ssw0rd"
}

wp_export() {
    wp export \
    --path=${wph} \
    --dir=${e}/ \
    --post_type=post \
    --post_status=publish \
    --filename_format={site}_{date}.{n}.xml \
    --start_date=$(date -d "-2 days" +'%Y-%m-%d') --end_date=$(date +'%Y-%m-%d')
}

convert_md() {
    # Convert exported XML into markdown format
    for i in $(find ${e} -type f -name "*\.xml"); do
        wp2md -url -d ${w}/ ${i}
        /bin/rm -f ${i}
    done
}

convert_sp() {
    # Convert markdown files into DOCX and HTML
    # Strip date from filename to make sure the new version overwrites old version on Sharepoint
    for i in $(find ${w} -mindepth 2 -type f -name "*\.md"); do
        i_new="$(echo ${i} | awk -F'/' '{print $NF}' | sed -r 's/^[0-9]{8}-//g'| sed "s/\.md$/\.md/g")"
        /bin/mv "${i}" "${w}/posts/${i_new}"
        j="$(echo ${i} | awk -F'/' '{print $NF}' | sed -r 's/^[0-9]{8}-//g'| sed 's/\.md$/\.docx/g')"
        k="$(echo ${i} | awk -F'/' '{print $NF}' | sed -r 's/^[0-9]{8}-//g'| sed 's/\.md$/\.html/g')"
        pandoc -s "${w}/posts/${i_new}" -o "${w}/posts/${k}"
        pandoc -s "${w}/posts/${k}" -o "${w}/posts/${j}"
    done
}

sync_sp() {
    # Sync files to Sharepoint
    for extension in md docx html; do
        for i in $(find ${w} -mindepth 2 -type f -name "*\.${extension}"); do
        j="$(echo ${i} | awk -F'/' '{print $NF}' | sed -r 's/^[0-9]{8}-//g'| sed "s/\.md$/\.${extension}/g")"
        curl --ntlm --user ${DOMAIN}\${USERNAME}:${PASSWORD} --upload-file "${w}/posts/${j}" "${sp_url}/${extension}/"
        done
    done
}

# RUNTIME
configure
wp_export
convert_md
convert_sp
sync_sp

 

Print Friendly, PDF & Email

Leave a Reply