Skip to content

Commit

Permalink
Redesign backup and restore
Browse files Browse the repository at this point in the history
  • Loading branch information
mreid-tt committed Oct 22, 2023
1 parent d05d631 commit 13d8fda
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 185 deletions.
191 changes: 110 additions & 81 deletions spk/owncloud/src/service-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ SQLITE="/bin/sqlite3"
JQ="/bin/jq"
SED="/bin/sed"
SYNOSVC="/usr/syno/sbin/synoservice"
VER_NUM=$(echo ${SYNOPKG_PKGVER} | cut -d'-' -f1)

if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
GROUP="http"
Expand Down Expand Up @@ -59,6 +60,50 @@ exec_occ() {
return $?
}

setup_owncloud_instance()
{
if [ "${SYNOPKG_PKG_STATUS}" = "INSTALL" ]; then
# Setup configuration file
exec_occ maintenance:install \
--database "sqlite" \
--database-name "${SYNOPKG_PKGNAME}" \
--data-dir "${DATA_DIR}" \
--admin-user "${wizard_owncloud_admin_username}" \
--admin-pass "${wizard_owncloud_admin_password}" 2>&1

# Get the trusted domains
DOMAINS="$(exec_occ config:system:get trusted_domains)"

# Fix trusted domains array
line_number=0
echo "${DOMAINS}" | while read -r line; do
if [ "$(echo "$line" | grep -cE ':5000|:5001')" -gt 0 ]; then
# Remove ":5000" or ":5001" from the line and update the trusted_domains array
new_line=$(echo "$line" | ${SED} -E 's/(:5000|:5001)//')
exec_occ config:system:set trusted_domains $line_number --value="$new_line"
fi
line_number=$((line_number+1))
done
# Add user specified trusted domains
line_number=$(( $(echo -ne "$DOMAINS" | wc -l) + 1 ))
for var in wizard_owncloud_trusted_domain_1 wizard_owncloud_trusted_domain_2 wizard_owncloud_trusted_domain_3; do
val="${!var}"
if [ -n "$val" ]; then
exec_occ config:system:set trusted_domains $line_number --value="$val"
line_number=$((line_number+1))
fi
done

# Add HTTP to HTTPS redirect to Apache configuration file
APACHE_CONF="${OCROOT}/.htaccess"
if [ -f "${APACHE_CONF}" ]; then
echo "RewriteEngine On" >> ${APACHE_CONF}
echo "RewriteCond %{HTTPS} off" >> ${APACHE_CONF}
echo "RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]" >> ${APACHE_CONF}
fi
fi
}

service_postinst ()
{
if [ ${SYNOPKG_DSM_VERSION_MAJOR} -lt 7 ]; then
Expand Down Expand Up @@ -145,43 +190,47 @@ service_postinst ()
set_owncloud_permissions ${OCROOT} ${DATA_DIR}
fi

# Setup configuration file
exec_occ maintenance:install \
--database "sqlite" \
--database-name "${SYNOPKG_PKGNAME}" \
--data-dir "${DATA_DIR}" \
--admin-user "${wizard_owncloud_admin_username}" \
--admin-pass "${wizard_owncloud_admin_password}" 2>&1

# Get the trusted domains
DOMAINS="$(exec_occ config:system:get trusted_domains)"

# Fix trusted domains array
line_number=0
echo "${DOMAINS}" | while read -r line; do
if [ "$(echo "$line" | grep -cE ':5000|:5001')" -gt 0 ]; then
# Remove ":5000" or ":5001" from the line and update the trusted_domains array
new_line=$(echo "$line" | ${SED} -E 's/(:5000|:5001)//')
exec_occ config:system:set trusted_domains $line_number --value="$new_line"
fi
line_number=$((line_number+1))
done
# Add user specified trusted domains
line_number=$(( $(echo -ne "$DOMAINS" | wc -l) + 1 ))
for var in wizard_owncloud_trusted_domain_1 wizard_owncloud_trusted_domain_2 wizard_owncloud_trusted_domain_3; do
val="${!var}"
if [ -n "$val" ]; then
exec_occ config:system:set trusted_domains $line_number --value="$val"
line_number=$((line_number+1))
# Check backup file path
if [ -n "${wizard_backup_file}" ] && [ -f "${wizard_backup_file}" ]; then
filename=$(basename "${wizard_backup_file}")
expected_prefix="${SYNOPKG_PKGNAME}_backup_v"
# Check backup file prefix
if [[ $filename == ${expected_prefix}* ]]; then
echo "The backup filename starts with the expected prefix, performing restore."
# Extract archive to temp folder
TEMPDIR="${SYNOPKG_PKGTMP}/${SYNOPKG_PKGNAME}"
${MKDIR} "${TEMPDIR}"
/bin/tar -xzvf "${wizard_backup_file}" -C "${TEMPDIR}" 2>&1

# Restore configuration files and directories
rsync -Aax --update -I "${TEMPDIR}/configs/root/.user.ini" "${TEMPDIR}/configs/root/.htaccess" "${OCROOT}/" 2>&1
rsync -Aax --update -I "${TEMPDIR}/configs/config" "${TEMPDIR}/configs/data" "${TEMPDIR}/configs/apps" "${TEMPDIR}/configs/apps-external" "${OCROOT}/" 2>&1

# Restore user data
echo "Restoring user data to ${DATA_DIR}"
rsync -Aax --update -I "${TEMPDIR}/data" "${DATA_DIR}/" 2>&1

# Place server in maintenance mode
exec_occ maintenance:mode --on

# Restore the Database
[ -f "${DATA_DIR}/${SYNOPKG_PKGNAME}.db" ] && ${RM} "${DATA_DIR}/${SYNOPKG_PKGNAME}.db"
${SQLITE} "${DATA_DIR}/${SYNOPKG_PKGNAME}.db" < "${TEMPDIR}/database/${SYNOPKG_PKGNAME}.db" 2>&1

# Update the systems data-fingerprint after a backup is restored
exec_occ maintenance:data-fingerprint

# Disable maintenance mode
exec_occ maintenance:mode --off

# Clean-up temporary files
${RM} "${TEMPDIR}"
else
echo "The backup filename does not start with the expected prefix, performing new install."
setup_owncloud_instance
fi
done

# Add HTTP to HTTPS redirect to Apache configuration file
APACHE_CONF="${OCROOT}/.htaccess"
if [ -f "${APACHE_CONF}" ]; then
echo "RewriteEngine On" >> ${APACHE_CONF}
echo "RewriteCond %{HTTPS} off" >> ${APACHE_CONF}
echo "RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]" >> ${APACHE_CONF}
else
setup_owncloud_instance
fi

# Fix permissions
Expand All @@ -205,52 +254,37 @@ service_preuninst ()
fi

# Prepare archive structure
TEMPDIR="${SYNOPKG_PKGTMP}/${SYNOPKG_PKGNAME}-backup_$(date +"%Y%m%d")"
TEMPDIR="${SYNOPKG_PKGTMP}/${SYNOPKG_PKGNAME}_backup_v$(VER_NUM)_$(date +"%Y%m%d")"
${MKDIR} "${TEMPDIR}"

# Check database export
if [ "${wizard_export_database}" = "true" ]; then
echo "Copying previous database from ${DATADIR}"
${MKDIR} "${TEMPDIR}/database"
${SQLITE} "${DATADIR}/${SYNOPKG_PKGNAME}.db" ".backup '${TEMPDIR}/database/${SYNOPKG_PKGNAME}.db'" 2>&1
fi
# Place server in maintenance mode
exec_occ maintenance:mode --on

# Check configuration export
if [ "${wizard_export_configs}" = "true" ]; then
echo "Copying previous configuration from ${OCROOT}"
${MKDIR} "${TEMPDIR}/configs"
${CP} "${OCROOT}/.user.ini" "${TEMPDIR}/configs/"
${CP} "${OCROOT}/.htaccess" "${TEMPDIR}/configs/"
${MKDIR} "${TEMPDIR}/configs/config"
source="${OCROOT}/config"
patterns=(
"*config.php"
"*.json"
)
target="${TEMPDIR}/configs/config"
# Process each pattern of files in the source directory
for pattern in "${patterns[@]}"; do
files=$(find "$source" -type f -name "$pattern")
if [ -n "$files" ]; then
for file in "${files[@]}"; do
${CP} "$file" "$target/"
done
fi
done
fi
# Backup the Database
echo "Copying previous database from ${DATADIR}"
${MKDIR} "${TEMPDIR}/database"
${SQLITE} "${DATADIR}/${SYNOPKG_PKGNAME}.db" ".backup '${TEMPDIR}/database/${SYNOPKG_PKGNAME}.db'" 2>&1

# Check user data export
if [ "${wizard_export_userdata}" = "true" ]; then
echo "Copying previous user data from ${DATADIR}"
${CP} "${DATADIR}" "${TEMPDIR}/"
fi
# Backup Directories
echo "Copying previous configuration from ${OCROOT}"
${MKDIR} "${TEMPDIR}/configs/root"
rsync -Aax "${OCROOT}/.user.ini" "${OCROOT}/.htaccess" "${TEMPDIR}/configs/root/" 2>&1
rsync -Aax "${OCROOT}/config" "${OCROOT}/data" "${OCROOT}/apps" "${OCROOT}/apps-external" "${TEMPDIR}/configs/" 2>&1

# Backup user data
echo "Copying previous user data from ${DATADIR}"
rsync -Aax "${DATADIR}" "${TEMPDIR}/" 2>&1

# Disable maintenance mode
exec_occ maintenance:mode --off

# Create backup archive
archive_name="$(basename "$TEMPDIR").tar.gz"
echo "Creating compressed archive of ownCloud data in file $archive_name"
/bin/tar -czvf "${SYNOPKG_PKGTMP}/$archive_name" "$TEMPDIR" 2>&1
/bin/tar -C "$TEMPDIR" -czvf "${SYNOPKG_PKGTMP}/$archive_name" . 2>&1

# Move archive to export directory
${MKDIR} "${wizard_export_path}"
if ${RSYNC} "${SYNOPKG_PKGTMP}/$archive_name" "${wizard_export_path}/"; then
echo "Backup file copied successfully to ${wizard_export_path}."
else
Expand Down Expand Up @@ -378,9 +412,8 @@ service_restore ()
if [ -f ${SYNOPKG_TEMP_UPGRADE_FOLDER}/.datadirectory ]; then
DATAPATH="$(cat ${SYNOPKG_TEMP_UPGRADE_FOLDER}/.datadirectory)"
# Data directory inside owncloud directory and needs to be restored
[ -d ${OCROOT}/${DATAPATH} ] && ${RM} ${OCROOT}/${DATAPATH}
echo "Restore previous data directory from ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/${DATAPATH}"
rsync -aX ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/${DATAPATH} ${OCROOT}/ 2>&1
rsync -aX --update -I ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/${DATAPATH} ${OCROOT}/ 2>&1
${RM} ${SYNOPKG_TEMP_UPGRADE_FOLDER}/.datadirectory
fi

Expand All @@ -397,19 +430,15 @@ service_restore ()
files=$(find "$source" -type f -name "$pattern")
if [ -n "$files" ]; then
for file in "${files[@]}"; do
file_name=$(basename "$file")
[ -f $target/$file_name ] && ${RM} $target/$file_name
rsync -aX "$file" "$target/" 2>&1
rsync -aX --update -I "$file" "$target/" 2>&1
done
fi
done
if [ -f ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/.user.ini ]; then
[ -f ${OCROOT}/.user.ini ] && ${RM} ${OCROOT}/.user.ini
rsync -aX ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/.user.ini ${OCROOT}/ 2>&1
rsync -aX --update -I ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/.user.ini ${OCROOT}/ 2>&1
fi
if [ -f ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/.htaccess ]; then
[ -f ${OCROOT}/.htaccess ] && ${RM} ${OCROOT}/.htaccess
rsync -aX ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/.htaccess ${OCROOT}/ 2>&1
rsync -aX --update -I ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}/.htaccess ${OCROOT}/ 2>&1
fi

echo "Restore manually installed apps from ${SYNOPKG_TEMP_UPGRADE_FOLDER}/${SYNOPKG_PKGNAME}"
Expand Down
Loading

0 comments on commit 13d8fda

Please sign in to comment.