deployment-dcdn/utils/export-content.sh

39 lines
1.4 KiB
Bash
Raw Normal View History

2024-04-16 16:03:33 +02:00
#!/bin/bash
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
PURPLE='\033[35m'
BLUE='\033[34m'
BOLD='\033[1m'
RESET='\033[0m'
echo -e "${PURPLE}${BOLD}Export Directus Database and files ? (y/N) ${RESET}"
read answer
if [[ "$answer" == "y" ]]; then
user=$(getent passwd 1001 | awk -F: '{print $1}')
2024-04-16 16:17:54 +02:00
2024-04-16 16:03:33 +02:00
site_name=$(ls /var/www/repositories/ | grep -v '^cms')
db_password=$(cat /var/www/repositories/cms*/.env | grep DB_PASSWORD | sed "s/[^']*'\([^']*\)'.*/\1/")
current_date=$(date +'%d-%m-%y_%H-%M')
2024-04-16 16:19:54 +02:00
export_base_folder="/home/${user}/content_exports"
export_folder="${export_base_folder}/${site_name}_export_${current_date}"
2024-04-17 16:48:16 +02:00
2024-04-16 16:03:33 +02:00
mkdir -p "${export_folder}"
2024-04-16 17:32:31 +02:00
mysqldump -u directus -p"${db_password}" directus > "${export_folder}/db_${site_name}_${current_date}.sql"
2024-04-16 16:03:33 +02:00
cp -r /var/www/repositories/cms*/uploads "${export_folder}"
2024-04-16 17:51:55 +02:00
cp -r /var/www/repositories/cms*/.env "${export_folder}"
2024-04-16 16:19:54 +02:00
tar -czf "${export_folder}.tar.gz" -C "${export_base_folder}" .
2024-04-16 17:32:31 +02:00
rm -r "${export_folder}"
chown -R "${user}:${user}" "${export_base_folder}"
2024-04-16 16:03:33 +02:00
2024-04-17 16:48:16 +02:00
ssh_port=$(cat /etc/ssh/sshd_config.d/custom.conf | grep "Port " | sed 's/^Port //')
2024-04-16 16:03:33 +02:00
ip=$(hostname -I)
2024-04-16 16:17:54 +02:00
if [[ "${ip: -1}" == " " ]]; then
ip="${ip%?}"
fi
2024-04-16 16:03:33 +02:00
echo -e "${PURPLE}${BOLD}You can now download the backup${RESET}"
2024-04-16 16:17:54 +02:00
echo -e "${BLUE}scp -P ${ssh_port} ${user}@${ip}:${export_folder}.tar.gz ./path/to/local/folder${RESET}"
2024-04-16 16:03:33 +02:00
fi