#!/bin/bash
set -u
declare ThisScript="${0##*/}"
declare Version=1.0.1
declare ThisUser=
declare ThisHost=${HOSTNAME%%.*}
declare PerforcePackageBase=/opt/perforce
declare SDPPackageBase="$PerforcePackageBase/helix-sdp"
declare BackupBase="/hxdepots/backup"
declare BackupDir="$BackupBase/opt_perforce_helix-sdp"
declare RsyncCmd=
declare -i Debug=${SDP_DEBUG:-0}
declare -i ErrorCount=0
#==============================================================================
# Local Functions
function msg () { echo -e "$*"; }
function dbg () { [[ "$Debug" -eq 0 ]] || msg "DEBUG: $*"; }
function errmsg () { msg "\\nError: ${1:-Unknown Error}\\n"; ErrorCount+=1; }
function bail () { errmsg "${1:-Unknown Error}"; exit "${2:-1}"; }
ThisUser=$(id -n -u)
msg "Started $ThisScript v$Version as $ThisUser@$ThisHost on $(date)."
dbg "Starting Preflight Checks."
if [[ "$ThisUser" == root ]]; then
dbg "Verified: Running as root"
else
errmsg "Run this as root, not $ThisUser."
fi
if [[ -d "$SDPPackageBase" ]]; then
dbg "Verified: $SDPPackageBase directory exists."
else
errmsg "SDP OS Package Structure [$SDPPackageBase] does not exists. Use this script only if that structure exists."
fi
if [[ -d "$BackupBase" && -w "$BackupBase" ]]; then
dbg "Verified: $BackupBase directory exists and is writable."
else
errmsg "The BackupBase directory [$BackupBase] does not exist or is not writable.."
fi
if [[ "$ErrorCount" -eq 0 ]]; then
dbg "Preflight checks OK."
else
bail "Aborting due to failed preflight checks."
fi
RsyncCmd="rsync -av $SDPPackageBase/ $BackupDir"
if eval "$RsyncCmd"; then
msg "SDP Package Backup to $BackupDir was successful."
else
bail "SDP Package Backup to $BackupDir failed."
fi
exit "$ErrorCount"
| # | Change | User | Description | Committed | |
|---|---|---|---|---|---|
| #25 | 32190 | C. Thomas Tyler |
In opt_perforce_sdp_backup.sh, a missing HomeDirBackupModeFile is now treated as a warning with a reasonable workaround, rather than resulting in an error. This avoids errors from being reported after an SDP Upgrade when using the new form of the SDP installation. |
||
| #24 | 32170 | C. Thomas Tyler |
Fixed issue where backups from prior installs, with root-owned dirs, will cause SDP upgrades to break due to root ownership of the BackupBase and BackupDir folders. This occurs on systems that do NOT have root squash enabled (and may not even be NFS), but do have installs done with prior versions new enough to operate the opt_perforce_sdp_backup.service. This change executes 'chown' commands as root on the /hxdepots volume which would fail if /hxdepots were mounted on NFS with root squash enabled. However, this should not be an issue because prior installs could never have occurred on systems with NFS with root squash enabled, because installs did not work at all with NFS and root squash enabled until v2.11.6. |
||
| #23 | 32160 | C. Thomas Tyler |
Enhanced opt_perforce_sdp_backup.sh to work with P4Depots volume on NFS with 'root squash' feature enabled. Fixes SDP-1236. PROBLEM: When the backup script runs as root and attempts to write to an NFS-mounted P4Depots volume with root squash enabled, the script fails because root gets squashed to nobody/nfsnobody and lacks write permissions to the NFS mount. SOLUTION: Implemented a staging and tarball approach to work around NFS root squash: 1. Staging Directory: - Created a temporary staging directory under /tmp where all backup operations write files as root (preserving proper ownership). - All rsync and cp operations now target the staging directory instead of directly writing to the NFS-mounted backup location. 2. Tarball Creation: - After successful staging, a tarball is created from the staged content. - The tarball uses --numeric-owner flag to preserve UIDs/GIDs. - Tarball ownership is changed to the SDP owner user (not root). 3. NFS-Compatible Backup: - BackupBase and BackupDir are now created as the SDP owner user using runuser, not as root. - The tarball (owned by SDP owner) is copied to the backup location as the SDP owner user, bypassing root squash restrictions. 4. Updated Recovery Procedure: - Recovery script now extracts the tarball locally before running recovery. - Tarball extraction as root restores all files with correct ownership. - Updated RECOVERY PROCEDURE documentation to reflect the new approach: * Copy tarball from backup location to local directory * Extract tarball as root (preserves ownership) * Run recovery script from extracted content 5. Documentation Updates: - Added explanation of tarball approach in DESCRIPTION - Updated RECOVERY PROCEDURE with step-by-step tarball extraction - Added LIMITATIONS section noting SELinux context handling is not currently automated ADDITIONAL CHANGES: - All directory operations for BackupBase/BackupDir use runuser to operate as SDP owner instead of root. - Recovery script paths adjusted to point to extracted tarball content. - Added cleanup of staging directory after successful backup. - Improved messaging to distinguish between "staging" and "backup" operations. TESTING: This version maintains ShellCheck compliance (v0.11.0) and is designed to be backward compatible with existing backup locations. The tarball approach ensures that recovery works identically to before, with the added benefit of supporting NFS mounts with root squash enabled. The SDP Regression Test Suite for testing install_sdp.sh (which installs and calls opt_perforce_sdp_backup.sh during installation) was updated for this change. (The SDP Regression Test Suite for testing install_sdp.sh is one of three SDP regression test suites). The change includes adpating the test suite to the changed recover procedure, as documented with 'opt_perforce_sdp_backup.sh -man' in the updated 'RECOVERY PROCEDURE' section. |
||
| #22 | 32145 | C. Thomas Tyler | Fix for case where groups with spaces in the name are used. | ||
| #21 | 32087 | C. Thomas Tyler |
Tweak to order of local vs. standard library function load order. |
||
| #20 | 32077 | C. Thomas Tyler | Internal refactoring. | ||
| #19 | 32074 | C. Thomas Tyler | Refined recovery script generation. | ||
| #18 | 32045 | C. Thomas Tyler | Added backup and recovery of sudoers file. | ||
| #17 | 32043 | C. Thomas Tyler |
Added logic to optionally backup and recover the 'perforce' user home directory. |
||
| #16 | 32042 | C. Thomas Tyler | Updated generated recovery script to create OS user/group if needed. | ||
| #15 | 32041 | C. Thomas Tyler | Refined generated recovery script to handle crontab and service recreatino. | ||
| #14 | 32040 | C. Thomas Tyler | Updated to more faithfully capture current state of all symlinks. | ||
| #13 | 32038 | C. Thomas Tyler | Added 'set -x' to recover script and needed mkdir commands. | ||
| #12 | 32037 | C. Thomas Tyler | Fixed chmod issues with generated recovery script. | ||
| #11 | 32036 | C. Thomas Tyler | Refined handling of recovery script backups. | ||
| #10 | 32035 | C. Thomas Tyler |
In backup script opt_perforce_sdp_backup.sh: * A recovery script is now generated: recover_opt_perforce_sdp.sh. * Added symlink in backup dir to new recovery script. * Removed obsolete debug comments. * Cosmetic fixes. Fixes: SDP-1291. |
||
| #9 | 31963 | C. Thomas Tyler | Tweak to allow docs to be generated on machine w/o SDP installed. | ||
| #8 | 31904 | C. Thomas Tyler | opt_perforce_sdp_backup.sh: Cosmetic refinements to usage(). | ||
| #7 | 31903 | C. Thomas Tyler |
Removed '-n' option that was not implemented and isn't needed for the backup script. Updated to reflect new name of the recovery script. |
||
| #6 | 31886 | C. Thomas Tyler |
Refactoring non-trivial functions into a library utils.lib. Added test script. |
||
| #5 | 31883 | C. Thomas Tyler |
Enhanced backup script to be all-inclusive. Added first cut at a test script. |
||
| #4 | 31846 | C. Thomas Tyler |
Extended SDP Backup: * Directory /p4/ssl is now backed up. * Instance Bin (/p4/N/bin) directories are no backed up. * Files in Instance Directories (e.g. /p4/N/.p4tickets) are backed up. * Files in /p4 are backed up. With this change, all necessary files that live on the OS Root Volume can be recovered from files on the backup volume, including that outside the /p4/common structure. Fixes SDP-1290. |
||
| #3 | 31394 | C. Thomas Tyler |
Tweaked backup to incorporate hostname into the backup directory, for safety in NFS environments. |
||
| #2 | 31051 | C. Thomas Tyler | Tweaked to create backup dir if needed. | ||
| #1 | 31050 | C. Thomas Tyler |
Added script to back SDP OS Package Structure. Modified sdp_upgrade.sh to install a systemd oneshot service to call this script if operating on a system using the SDP OS Package Structure (i.e. a systemd where /opt/perforce/helix-sdp exists). |