sunetdrive/templates/mariadb_backup/listusers.erb.sh

49 lines
1.8 KiB
Bash
Raw Permalink Normal View History

2023-02-13 09:44:56 +00:00
#!/bin/bash
container=${1}
customer=${2}
if [[ -z ${container} ]]; then
2023-03-13 12:23:19 +00:00
container='mariadb_backup_mariadb_backup_1'
2023-02-13 09:44:56 +00:00
fi
if [[ -z ${customer} ]]; then
location='<%= @location %>'
fi
dexec="docker exec ${container}"
password=$(${dexec} env | grep MYSQL_ROOT_PASSWORD | awk -F '=' '{print $2}')
mysql="${dexec} mysql -p${password}"
2024-06-11 12:12:38 +00:00
if [[ "$(${mysql} -NB -e 'select exists(select * from information_schema.TABLES where TABLE_SCHEMA = "nextcloud" and TABLE_NAME = "oc_global_scale_users")')" == "1" ]]
then
users="$(${mysql} -NB -e 'select uid,displayname from nextcloud.oc_global_scale_users')"
fi
users="${users}"$'\n'"$(${mysql} -NB -e 'select uid,displayname from nextcloud.oc_users')"
if [[ "$(${mysql} -NB -e 'select exists(select * from information_schema.TABLES where TABLE_SCHEMA = "nextcloud" and TABLE_NAME = "oc_user_saml_users")')" == "1" ]]
then
users="${users}"$'\n'"$(${mysql} -NB -e 'select uid,displayname from nextcloud.oc_user_saml_users')"
fi
users="$(echo "${users}" | sort | uniq)"
2023-02-13 09:44:56 +00:00
project="statistics"
bucket="drive-server-coms"
base_dir="${project}:${bucket}"
mountpoint="/opt/statistics"
customer_dir="${mountpoint}/${location}"
mkdir -p "${customer_dir}"
rclone mkdir --no-check-certificate --webdav-headers "Host,sunet.drive.sunet.se" --use-cookies "${base_dir}/${location}"
2023-02-13 09:44:56 +00:00
echo "${users}" | awk 'BEGIN{print "{"} {print t "\""$1"\": \""$2"\""} {t=","} END{print "}"}' | jq . >"${customer_dir}/users.json"
status=0
if ! jq . "${customer_dir}/users.json" &>/dev/null; then
status=1
fi
if [[ ${status} -eq 0 ]]; then
# something is wrong if we cant copy the file in 30 seconds, so we should note that
if ! timeout 30s rclone copy -c --no-check-certificate --webdav-headers "Host,sunet.drive.sunet.se" --use-cookies "${customer_dir}/users.json" "${base_dir}/${location}/"; then
2023-02-13 09:44:56 +00:00
status=1
fi
fi
exit ${status}