2021-09-14 22:36:54 +07:00
#!/bin/bash
if [ $# -eq 0 ] || [ " $1 " = "-h" ] || [ " $1 " = "-help" ] ; then
echo "# script to upload a file to Nextcloud"
echo "# nextcloud.upload.sh on [server] [user] [password]"
echo "# nextcloud.upload.sh off"
echo "# nextcloud.upload.sh upload [filepath]"
echo "err='just informational output'"
exit 1
fi
source /mnt/hdd/raspiblitz.conf
on( ) {
2021-12-14 23:34:35 +01:00
2021-09-14 22:36:54 +07:00
local server = " ${ 1 } "
local user = " ${ 2 } "
local password = " ${ 3 } "
2021-12-14 23:34:35 +01:00
sudo touch /var/cache/raspiblitz/.tmp
sudo chmod 777 /var/cache/raspiblitz/.tmp
2021-09-14 22:36:54 +07:00
if [ -z " ${ server } " ] ; then
2021-12-14 23:34:35 +01:00
whiptail --title "Static Channel Backup on Nextcloud" --inputbox "Enter your Nextcloud server URL\nExample: https://cloud.johnsmith.com" 11 70 2>/var/cache/raspiblitz/.tmp
server = $( cat /var/cache/raspiblitz/.tmp)
2021-09-14 22:36:54 +07:00
fi
if [ -z " ${ user } " ] ; then
2021-12-14 23:34:35 +01:00
whiptail --title "Static Channel Backup on Nextcloud" --inputbox "\nEnter your Nextcloud username:\n(best to use a dedicated user for backup)" 10 70 2>/var/cache/raspiblitz/.tmp
user = $( cat /var/cache/raspiblitz/.tmp)
2021-09-14 22:36:54 +07:00
fi
if [ -z " ${ password } " ] ; then
2021-12-14 23:34:35 +01:00
whiptail --title "Static Channel Backup on Nextcloud" --inputbox "\nEnter your Nextcloud password:\n(will get stored in cleartext on raspiblitz)" 10 70 2>/var/cache/raspiblitz/.tmp
password = $( cat /var/cache/raspiblitz/.tmp)
2021-09-14 22:36:54 +07:00
fi
2021-12-14 23:34:35 +01:00
# normal delete is OK because it a mem drive
2022-06-02 17:09:03 +02:00
sudo rm -f /var/cache/raspiblitz/.tmp
2021-09-14 22:36:54 +07:00
if [ " ${ server } " ] && [ " ${ user } " ] && [ " ${ password } " ] ; then
2022-02-06 15:56:48 +01:00
/home/admin/config.scripts/blitz.conf.sh set nextcloudBackupServer " ${ server } "
/home/admin/config.scripts/blitz.conf.sh set nextcloudBackupUser " ${ user } "
/home/admin/config.scripts/blitz.conf.sh set nextcloudBackupPassword " ${ password } "
2021-09-14 22:36:54 +07:00
else
echo "Please provide nextcloud server, username and password"
exit 1
fi
}
off( ) {
2021-12-14 23:34:35 +01:00
/home/admin/config.scripts/blitz.conf.sh delete nextcloudBackupServer
/home/admin/config.scripts/blitz.conf.sh delete nextcloudBackupUser
/home/admin/config.scripts/blitz.conf.sh delete nextcloudBackupPassword
2021-09-14 22:36:54 +07:00
}
upload( ) {
local filepath = " ${ 1 } "
if [ -z " ${ filepath } " ] ; then
echo "err='Missing argument: filepath'"
exit 1
fi
if [ -z " ${ nextcloudBackupServer } " ] || [ -z " ${ nextcloudBackupUser } " ] || [ -z " ${ nextcloudBackupPassword } " ] ; then
echo "err='Nextcloud credentials are missing'"
exit 1
fi
local remoteDirUrl = " $nextcloudBackupServer /remote.php/dav/files/ $nextcloudBackupUser /raspiblitz/ "
# checking if remote directory exists
local response
response = $( curl " ${ remoteDirUrl } " \
--user " ${ nextcloudBackupUser } : ${ nextcloudBackupPassword } " \
--request PROPFIND \
--silent)
# if remote directory doesn't exist, we need to create it
if [ [ " ${ response } " = *DAV\\ Exception\\ NotFound* ] ] ; then
curl " ${ remoteDirUrl } " \
--user " ${ nextcloudBackupUser } : ${ nextcloudBackupPassword } " \
--request MKCOL \
--silent
fi
if curl " ${ remoteDirUrl } " \
--user " ${ nextcloudBackupUser } : ${ nextcloudBackupPassword } " \
--upload-file " ${ filepath } " \
--silent;
then
echo " File ${ filepath } has been uploaded "
echo "upload=1"
else
echo "err='File upload failed'"
echo "upload=0"
exit 1
fi
}
case " ${ 1 } " in
on) on " ${ 2 } " " ${ 3 } " " ${ 4 } " ; ;
off) off ; ;
upload) upload " ${ 2 } " ; ;
*) echo " err=Unknown action: ${ 1 } " ; exit 1 ; ;
esac