-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMPROVE] Refactoring the snap backup scripts to improve UX
- Loading branch information
1 parent
1a7e04d
commit 15a01ee
Showing
4 changed files
with
117 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/bin/bash | ||
|
||
if [[ ${EUID} != 0 ]]; then | ||
echo "[-] This task must be run with 'sudo'." | ||
exit 1 | ||
fi | ||
|
||
if $(ps x | grep "node ${SNAP}/main.js" | grep -qv "grep"); then | ||
echo "[-] Please shutdown Rocket.Chat first to get a clean backup" | ||
echo "[-] Use 'sudo systemctl stop snap.rocketchat-server.rocketchat-server'" | ||
fi | ||
|
||
TIMESTAMP=$(date +"%Y%m%d.%H%M") | ||
BACKUP_DIR="${SNAP_COMMON}/backup" | ||
|
||
if [[ ! -d ${BACKUP_DIR} ]]; then | ||
mkdir ${BACKUP_DIR} | ||
fi | ||
|
||
if [[ -d ${BACKUP_DIR}/dump ]]; then | ||
rm -rf ${BACKUP_DIR}/dump | ||
fi | ||
|
||
if [[ -f ${BACKUP_DIR}/rocketchat_backup_${TIMESTAMP}.tar.gz ]]; then | ||
rm -f ${BACKUP_DIR}/rocketchat_backup_${TIMESTAMP}.tar.gz | ||
fi | ||
|
||
if [[ -f ${BACKUP_DIR}/backup_${TIMESTAMP}.log ]]; then | ||
rm -f ${BACKUP_DIR}/backup_${TIMESTAMP}.log | ||
fi | ||
|
||
echo "[*] Creating backup file..." | ||
mkdir ${BACKUP_DIR}/dump | ||
echo "[*] Dumping database with \"mongodump -d parties -o ${BACKUP_DIR}/dump\"" > "${BACKUP_DIR}/backup_${TIMESTAMP}.log" | ||
mongodump -d parties -o ${BACKUP_DIR}/dump &>> "${BACKUP_DIR}/backup_${TIMESTAMP}.log" | ||
echo "[*] Packing archive with \"tar czvf ${BACKUP_DIR}/rocketchat_backup_${TIMESTAMP}.tar.gz ${BACKUP_DIR}/dump\"" >> "${BACKUP_DIR}/backup_${TIMESTAMP}.log" | ||
tar czvf ${BACKUP_DIR}/rocketchat_backup_${TIMESTAMP}.tar.gz -C ${BACKUP_DIR} dump &>> "${BACKUP_DIR}/backup_${TIMESTAMP}.log" | ||
rm -rf ${BACKUP_DIR}/dump | ||
|
||
echo "[+] A backup of your data can be found at ${BACKUP_DIR}/rocketchat_backup_${TIMESTAMP}.tar.gz" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,88 @@ | ||
#! /bin/bash | ||
#!/bin/bash | ||
|
||
if [[ ${EUID} != 0 ]] | ||
then | ||
function warn { | ||
echo "[!] ${1}" | ||
echo "[*] Check ${RESTORE_DIR}/${LOG_NAME} for details." | ||
} | ||
|
||
function abort { | ||
echo "[!] ${1}" | ||
echo "[*] Check ${RESTORE_DIR}/${LOG_NAME} for details." | ||
echo "[-] Restore aborted!" | ||
exit 1 | ||
} | ||
|
||
if [[ ${EUID} != 0 ]]; then | ||
echo "[-] This task must be run with 'sudo'." | ||
exit | ||
exit 1 | ||
fi | ||
|
||
backup_file=${1} | ||
if [[ ! -f ${backup_file} ]] | ||
then | ||
echo "[-] Usage: snap run rocketchat-server.restoredb ${SNAP_COMMON}/backup_file.tgz" | ||
exit | ||
echo "*** ATTENTION ***" | ||
echo "* Your current database WILL BE DROPPED prior to the restore!" | ||
echo "* Do you want to continue?" | ||
|
||
select yn in "Yes" "No"; do | ||
case $yn in | ||
Yes ) break;; | ||
No ) exit 1;; | ||
esac | ||
done | ||
|
||
if $(ps x | grep "node ${SNAP}/main.js" | grep -qv "grep"); then | ||
echo "[-] Please shutdown Rocket.Chat first to restore a clean backup" | ||
echo "[-] Use 'sudo systemctl stop snap.rocketchat-server.rocketchat-server'" | ||
echo "[-] Backup aborted!" | ||
exit 1 | ||
fi | ||
|
||
cd ${backup_file%/*} | ||
if [[ -z $(pwd | grep "${SNAP_COMMON}") ]] | ||
then | ||
echo "[-] Backup file must be within ${SNAP_COMMON}." | ||
exit | ||
TIMESTAMP=$(date +"%Y%m%d.%H%M") | ||
RESTORE_DIR="${SNAP_COMMON}/restore" | ||
DATA_DIR="${RESTORE_DIR}/dump/parties" | ||
LOG_NAME="restore_${TIMESTAMP}.log" | ||
|
||
if [[ ! -d ${RESTORE_DIR} ]]; then | ||
mkdir ${RESTORE_DIR} | ||
fi | ||
|
||
function ask_backup { | ||
echo -n "\ | ||
*** ATTENTION *** | ||
* Your current database WILL BE DROPPED prior to the restore! | ||
* Would you like to make a backup of the current database before proceeding? | ||
* (y/n/Q)> " | ||
|
||
read choice | ||
[[ "${choice,,}" = n* ]] && return | ||
[[ "${choice,,}" = y* ]] && backupdb && return | ||
exit | ||
} | ||
if [[ -d ${RESTORE_DIR}/dump ]]; then | ||
rm -rf ${RESTORE_DIR}/dump | ||
fi | ||
|
||
function warn { | ||
echo "[!] ${1}" | ||
echo "[*] Check ${restore_dir}/${log_name} for details." | ||
} | ||
if [[ -f ${RESTORE_DIR}/${LOG_NAME} ]]; then | ||
rm -f ${RESTORE_DIR}/${LOG_NAME} | ||
fi | ||
|
||
function abort { | ||
echo "[!] ${1}" | ||
echo "[*] Check ${restore_dir}/${log_name} for details." | ||
echo "[-] Restore aborted!" | ||
exit | ||
} | ||
BACKUP_FILE=${1} | ||
if [[ ! -f ${BACKUP_FILE} ]]; then | ||
echo "[-] Usage: sudo snap run rocketchat-server.restoredb ${SNAP_COMMON}/rocketchat_backup_{TIMESTAMP}.tar.gz" | ||
exit 1 | ||
fi | ||
|
||
if ! $(echo "${BACKUP_FILE}" | grep -q "${SNAP_COMMON}"); then | ||
echo "[-] Backup file must be within ${SNAP_COMMON}." | ||
exit 1 | ||
fi | ||
|
||
mongo parties --eval "db.getCollectionNames()" | grep "\[ \]" >> /dev/null || ask_backup | ||
echo "[*] Extracting backup file..." | ||
restore_dir="${SNAP_COMMON}/restore" | ||
log_name="extraction.log" | ||
mkdir -p ${restore_dir} | ||
cd ${restore_dir} | ||
tar --no-same-owner --overwrite -zxvf ${backup_file} &> "${restore_dir}/${log_name}" | ||
[[ $? != 0 ]] && abort "Failed to extract backup files to ${restore_dir}!" | ||
echo "[*] Extracting backup file with \"tar --no-same-owner --overwrite -xzvf ${BACKUP_FILE}\"" &> "${RESTORE_DIR}/${LOG_NAME}" | ||
cd ${RESTORE_DIR} | ||
tar --no-same-owner --overwrite -xzvf ${BACKUP_FILE} &>> "${RESTORE_DIR}/${LOG_NAME}" \ | ||
|| abort "Failed to extract backup files to ${RESTORE_DIR}!" | ||
|
||
if [ $(ls -l ${DATA_DIR} | wc -l) -le 1 ]; then | ||
abort "No restore data found within ${DATA_DIR}!" | ||
fi | ||
echo "[*] Restoring data..." | ||
data_dir=$(tail "${restore_dir}/${log_name}" | grep parties/. | head -n 1) | ||
[[ -z ${data_dir} ]] && abort "Restore data not found within ${backup_file}! | ||
Please check that your backup file contains the backup data within the \"parties\" directory." | ||
data_dir=$(dirname ${data_dir}) | ||
log_name="mongorestore.log" | ||
mongorestore --db parties --noIndexRestore --drop ${data_dir} &> "${restore_dir}/${log_name}" | ||
[[ $? != 0 ]] && abort "Failed to execute mongorestore from ${data_dir}!" | ||
# If mongorestore.log only has a few lines, it likely didn't find the dump files | ||
log_lines=$(wc -l < "${restore_dir}/${log_name}") | ||
[[ ${log_lines} -lt 24 ]] && warn "Little or no restore data found within ${backup_file}! | ||
Please check that your backup file contains all the backup data within the \"parties\" directory." | ||
echo "[*] Restoring data with \"mongorestore --db parties --noIndexRestore --drop ${DATA_DIR}\"" &>> "${RESTORE_DIR}/${LOG_NAME}" | ||
mongorestore --db parties --noIndexRestore --drop ${DATA_DIR} &>> "${RESTORE_DIR}/${LOG_NAME}" \ | ||
|| abort "Failed to execute mongorestore from ${DATA_DIR}!" | ||
if [ $(cat ${RESTORE_DIR}/${LOG_NAME} | grep $(date +%Y) | wc -l) -lt 24 ]; then | ||
warn "Little or no data could be restored from the backup!" | ||
fi | ||
|
||
echo "[*] Preparing database..." | ||
log_name="mongoprepare.log" | ||
mongo parties --eval "db.repairDatabase()" --verbose &> "${restore_dir}/${log_name}" | ||
[[ $? != 0 ]] && abort "Failed to prepare database for usage!" | ||
echo "[+] Restore completed! Please restart the snap.rocketchat services to verify." | ||
echo "[*] Preparing database with \"mongo parties --eval 'db.repairDatabase()' --verbose\"" &>> "${RESTORE_DIR}/${LOG_NAME}" | ||
mongo parties --eval "db.repairDatabase()" --verbose &>> "${RESTORE_DIR}/${LOG_NAME}" \ | ||
|| abort "Failed to prepare database for usage!" | ||
|
||
echo "[+] Restore completed! Please with 'sudo systemctl restart snap.rocketchat-server.rocketchat-server' to verify." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters