Skip to content

Instantly share code, notes, and snippets.

@tautomer
Created March 10, 2026 00:29
Show Gist options
  • Select an option

  • Save tautomer/3f23ac64155197e7b7f24a6ea2ec2f4b to your computer and use it in GitHub Desktop.

Select an option

Save tautomer/3f23ac64155197e7b7f24a6ea2ec2f4b to your computer and use it in GitHub Desktop.
#!/bin/bash
# Configuration
DEST_DIR="path_here"
TMP_PATH="/tmp/dsm_config_temp.dss"
HOSTNAME=$(hostname)
TIMESTAMP=$(date +%Y-%m-%d_%H%M)
# Version parsing
# Pulling from /etc/VERSION
PRODUCT_VER=$(grep "productversion" /etc/VERSION | cut -d'"' -f2)
BUILD_NUM=$(grep "buildnumber" /etc/VERSION | cut -d'"' -f2)
FIX_NUM=$(grep "smallfixnumber" /etc/VERSION | cut -d'"' -f2)
DSM_VER="${PRODUCT_VER}-${BUILD_NUM}-${FIX_NUM}"
# Filename format: hostname_7.3.2-86009-1_timestamp.dss
FINAL_NAME="${HOSTNAME}_${DSM_VER}_${TIMESTAMP}.dss"
# Ensure no stale temp file exists from a previous failed run
[ -f "$TMP_PATH" ] && rm -f "$TMP_PATH"
# Backup
mkdir -p "$DEST_DIR"
if /usr/syno/bin/synoconfbkp export --filepath="$TMP_PATH"; then
chown admin:users "$TMP_PATH"
chmod 644 "$TMP_PATH"
mv "$TMP_PATH" "${DEST_DIR}/${FINAL_NAME}"
echo "Backup successful: ${FINAL_NAME}"
else
# Send an entry to system log
/usr/syno/bin/synologset1 sys warn 0x11100000 "Critical: DSM Config Backup failed on ${HOSTNAME}"
exit 1
fi
# Retention policy
KEEP_LIST=$(mktemp)
ls -1t "$DEST_DIR"/*.dss 2>/dev/null | head -n 3 >> "$KEEP_LIST"
# Keep latest for every month found
ls -1 "$DEST_DIR"/*.dss 2>/dev/null | rev | cut -d'_' -f1 | rev | cut -c 1-7 | sort -u | while read -r month; do
ls -1 "$DEST_DIR"/*"$month"* 2>/dev/null | sort -r | head -n 1 >> "$KEEP_LIST"
done
# Keep latest for every year found
ls -1 "$DEST_DIR"/*.dss 2>/dev/null | rev | cut -d'_' -f1 | rev | cut -c 1-4 | sort -u | while read -r year; do
ls -1 "$DEST_DIR"/*"$year"* 2>/dev/null | sort -r | head -n 1 >> "$KEEP_LIST"
done
sort -u "$KEEP_LIST" -o "$KEEP_LIST"
for file in "$DEST_DIR"/*.dss; do
if ! grep -qxF "$file" "$KEEP_LIST"; then
rm "$file"
fi
done
rm "$KEEP_LIST"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment