Ich musste meine DS, nach einem Plattenausfall, neu aufsetzten. Nun möchte ich den alten Zustand (Alben,etc..) wieder herstellen jedoch finde ich den Ordner nicht in dem Synology Photos die Einstellungen/Datenbanken speichert.
root@DS1522:/volume1# su - postgres
postgres@DS1522:~$ psql -c "select oid, datname from pg_database"
oid | datname
-------+-------------
12155 | postgres
1 | template1
12154 | template0
16387 | autoupdate
16385 | synoindex
16815 | ong
19312 | synofoto
18863 | mediaserver
(8 rows)
postgres@DS1522:~$ ls -ld $PGDATA/base/19312
drwx------ 1 postgres postgres 8532 Apr 5 10:46 /var/services/pgsql/base/19312
postgres@DS1522:~$
#./script -b [-d /path/to/backup-dir]
#./script -r /path/to/backup-dir/dump.sql
#!/bin/bash
####################################################################################
# Set your backup directory or set it on run with -d
####################################################################################
backupDir=/volume1/backup/synology-photos
####################################################################################
# Set the number of latest backups you want to keep
####################################################################################
maxBackups=3
sourceDb=synofoto
restoreFile=
if [ ! -z "$(sudo -v)" ]; then
echo "Permission denied: current user has no sudo permissions"
exit 1
fi
helpMessage() {
echo "create backup: $0 -b [ -d /path/to/backup-dir ]"
echo "restore backup: $0 -r /path/to/backup-dump.sql"
}
createBackup() {
# create backup-directory if not exists
if [ ! -d ${backupDir} ]; then
mkdir -p "${backupDir}"
fi
# check directory exists and is writable
if [ ! -d ${backupDir} ] || [ ! -w ${backupDir} ]; then
echo "Backup-Directory (${backupDir}) does not exists or is not writeable"
exit 1
fi
# check database exists
if [ -z "${sourceDb}" ] || [ $(sudo -iu postgres psql -t -c "select datname from pg_database where datname = '${sourceDb}'") != ${sourceDb} ]; then
echo "Database '${sourceDb}' does not exist or exist check failed"
exit 1
fi
postgresHome=$(sudo -iu postgres pwd)
backupFile="${sourceDb}-backup-$(date '+%Y.%m.%d_%H-%M').sql"
sudo -iu postgres pg_dump ${sourceDb} >${postgresHome}/${backupFile}
if [ ! $? -eq 0 ]; then
echo "Creating dump failed"
exit 1
fi
# move the backup to backupDir
mv ${postgresHome}/${backupFile} ${backupDir}
if [ ! -e ${backupDir}/${backupFile} ]; then
echo "Move backup failed"
exit 1
fi
echo "Create backup success: ${backupDir}/${backupFile}"
count=$((${maxBackups} + 1))
ls -1t ${backupDir}/${sourceDb}-backup-*.sql | tail -n +${count} | xargs rm > /dev/null 2>&1
exit 0
}
terminateDbConnections() {
dataBase=$1
sudo -iu postgres psql -t -c "select pg_terminate_backend(pid) from pg_stat_activity where pid <> pg_backend_pid() AND datname = '${dataBase}'"
}
##############################################################
# if the current installation has another db version
# exit 1
##############################################################
restoreBackupDbVersionCheck() {
tempDB=$1
if [ ! -f /var/packages/SynologyPhotos/etc/data_version.last ]; then
return 0
fi
installedDbVersion="$(cat /var/packages/SynologyPhotos/etc/data_version.last | sed -En "s/db_version=(.*)/\1/p")"
restoreDbVersion=$(sudo -iu postgres psql -t -d "${tempDB}" -c "select value from config where key = 'version'")
if [ -n "${installedDbVersion}" ] && [ "${installedDbVersion}" -ne "${restoreDbVersion}" ]; then
WARN="\033[1;33m"
NC="\033[0m"
echo -e "${WARN}Your current database version of SynologyPhoto does not match the backup db version!${NC}"
echo -e "${WARN}installedDbVersion: ${NC}${installedDbVersion}${NC}"
echo -e "${WARN}restoreDbVersion: ${NC}${restoreDbVersion}${NC}"
echo "Uninstall SynologyPhoto and try it again. Then reinstall SynologyPhoto to run into db migrations"
exit 1
fi
}
##############################################################
# may be the user ids in the dump does not match system user ids
# it will adjust them in the database by username
# i dont know if its necessary but many be ...
##############################################################
restoreBackupAdjustSystemUsers() {
tempDB=$1
dbUsers=$(sudo -iu postgres psql -t -d "${tempDB}" -c "select uid,name from user_info where name != ''")
while IFS="" read -r line; do
userName=$(expr "$line" : '^.*|\s*\([a-zA-Z0-9]*\)')
userId=$(expr "$line" : '^\(.[0-9]*\)\s*|')
if [ -z "${userName}" ]; then
continue
fi
uId=$(id -u "${userName}")
WARN="\033[1;33m"
NC="\033[0m"
if [ -z "${uId}" ]; then
echo -e "${WARN}The db user \"${userName}\" is not present as system user${NC}"
continue
fi
if [ "${userId}" -ne "${uId}" ]; then
echo "Update '${userName}' uid from ${userId} to ${uId}"
sudo -iu postgres psql -t -d "${tempDB}" -c "update user_info set uid = '${uId}' where name = '${userName}'"
fi
done \
< <(echo -e "${dbUsers}")
}
restoreBackup() {
backupFile=$1
if [ -z "${backupFile}" ] || [ ! -f ${backupFile} ]; then
echo "Backup file does not exists: $backupFile"
exit 1
fi
if [ ! -r ${backupFile} ]; then
echo "Backup file is not readable: $backupFile"
exit 1
fi
echo "Stop SynologyPhotos"
synopkg stop SynologyPhotos
tempDB="${sourceDb}_restore_$(date +%s)"
sudo -iu postgres psql -t -c "create database ${tempDB} WITH TEMPLATE = template1"
if [ ! $? -eq 0 ]; then
echo "Creating temporary database failed"
exit 1
fi
echo "Import dump to temporary database: ${tempDB} < ${backupFile}"
sudo -iu postgres psql ${tempDB} <${backupFile}
if [ ! $? -eq 0 ]; then
echo "Importing dump to temporary database failed"
exit 1
fi
restoreBackupDbVersionCheck "${tempDB}"
restoreBackupAdjustSystemUsers "${tempDB}"
backupDb="${sourceDb}_backup_$(date +%s)"
echo "Backup current database to ${backupDb}"
terminateDbConnections ${sourceDb}
sudo -iu postgres psql -t -c "ALTER DATABASE ${sourceDb} RENAME TO ${backupDb}"
if [ ! $? -eq 0 ]; then
echo "Backup of current database failed"
exit 1
fi
echo "Switch temporary database to current"
sudo -iu postgres psql -t -c "ALTER DATABASE ${tempDB} RENAME TO ${sourceDb}"
if [ ! $? -eq 0 ]; then
echo "Switch database failed"
exit 1
fi
echo "Start SynologyPhotos"
synopkg start SynologyPhotos
echo "--------------------------------------------------------------------------"
echo "Please check the SynologyPhotos application"
echo "if it works as expected type 'yes' witch will delete the backup database"
echo "if not type 'no' witch will replace the imported database with the backup database"
echo ""
while true; do
read -p "SynologyPhotos works as expected? (yes/no) " yn
case $yn in
yes)
echo "DROP DATABASE ${backupDb}"
sudo -iu postgres psql -t -c "DROP DATABASE ${backupDb}"
break
;;
no)
echo "RESTORE DATABASE ${backupDb} to ${sourceDb}"
synopkg stop SynologyPhotos
terminateDbConnections ${sourceDb}
sudo -iu postgres psql -t -c "DROP DATABASE ${sourceDb}"
sudo -iu postgres psql -t -c "ALTER DATABASE ${backupDb} RENAME TO ${sourceDb}"
;;
*) echo "invalid response: type yes or no" ;;
esac
done
}
mode=
while getopts "br:d:" o; do
case "${o}" in
d)
backupDir=${OPTARG}
;;
b)
mode="backup"
;;
r)
mode="restore"
restoreFile=${OPTARG}
;;
*)
helpMessage
exit 1
;;
esac
done
if [ "${mode}" = "restore" ]; then
restoreBackup "${restoreFile}"
elif [ "${mode}" = "backup" ]; then
createBackup
else
helpMessage
fi
Ja, von wegen! Das sieht doch ganz übersichtlich aus und die einzelnen Schritte sind auch nachvollziehbar dank der reichlichen Kommentare.[...] denn ich bin wahrlich kein Profi im bash-scripting.
Wenn du das Forum hilfreich findest oder uns unterstützen möchtest, dann gib uns doch einfach einen Kaffee aus.
Als Dankeschön schalten wir deinen Account werbefrei.