📜 ⬆️ ⬇️

Script convenient work with WebDAV in Ubuntu

Once I went to my favorite blog, I was very interested in an article on WebDAV technology. This article was subsequently inspired to write a script that simplifies working with this technology in Ubuntu. I want to share this script with the audience of Habrahabr.

In the script, I tried to take into account three main operations:
  1. Mounting WebDAV Directories
  2. Synchronization (this is the problem that remained unresolved in the above article)
  3. Unmounting


The essence of the idea


I tried to embody the idea that was expressed in the comments to the above article. We have 2N WebDAV accounts. We use N of them to store data, combining them into some sort of RAID. We will use the remaining N for backup storage in case any account is closed or data is damaged.

It is even possible to store multiple versions of files, but this method will not be considered in this article.
')
So let's get down to pray ...

0. Start


To mount, we will need the mhddfs and davfs2 packages, which we will immediately supply:

sudo apt-get install mhddfs davfs2 


To ensure that when mounting the password is not asked, we write it in a special file:

 sudo nano /etc/davfs2/secrets 


As:

 URL login pass 

If the URL matches, you can use the path to the directory.

Take care of the initial variables:

 # URL  WEBDAV_URL1=URL1 WEBDAV_URL2=URL2 WEBDAV_URL3=URL3 WEBDAV_URL4=URL4 #    WebDAV(  ) WEBDAV_DIR1=/mnt/webdav1/ WEBDAV_DIR2=/mnt/webdav2/ WEBDAV_DIR3=/mnt/webdav3/ WEBDAV_DIR4=/mnt/webdav4/ # ,      WebDAV RAID_DIR=/mnt/webdav RAID_DIR_BACKUP=/mnt/webdav.backup #        #    "user"     SYMLINK_WEBDAV=/home/user/webdav #   ROOT_UID=0 #    $UID 0   root. E_NOTROOT=67 #   root-. ME=`basename $0` 


Immediately take care that the script runs with the rights of root, as we will need them soon.

 if [ "$UID" -ne "$ROOT_UID" ]; then echo "     root." exit $E_NOTROOT fi 


1. Mounting


Writing a function to mount.

 function mount_webdav() { # ,     , ,  ,  . if [ ! -d "$WEBDAV_DIR1" ]; then echo " $WEBDAV_DIR1" mkdir $WEBDAV_DIR1 fi #  WebDAV  echo " $WEBDAV_DIR1" mount -t davfs -o rw $WEBDAV_URL1 $WEBDAV_DIR1 # ... #    ... #      , #       WebDAV if [ ! -d "$RAID_DIR" ]; then echo " $RAID_DIR" mkdir $RAID_DIR fi echo " $RAID_DIR" mhddfs $WEBDAV_DIR1,$WEBDAV_DIR2 $RAID_DIR -o allow_other if [ ! -d "$RAID_DIR_BACKUP" ]; then echo " $RAID_DIR_BACKUP" mkdir $RAID_DIR_BACKUP fi echo " $RAID_DIR_BACKUP" mhddfs $WEBDAV_DIR3,$WEBDAV_DIR4 $RAID_DIR_BACKUP -o allow_other #       echo "  $SYMLINK_WEBDAV  $SYMLINK_WEBDAV" ln -s $RAID_DIR $SYMLINK_WEBDAV 


2. Synchronization


Everything is very simple here. For synchronization, use the standard rsync utility:

 function sync_webdav() { #  rsync --progress -zuogthvr --delete-after $RAID_DIR $RAID_DIR_BACKUP } 


Description of options can be found on the Internet, and specifically here .

3. Unmounting


In addition to unmounting, our function will delete empty directories behind it. Please note that first unmount the directories in which we created the semblance of RAID (well, you understand why).

 function umount_webdav() { echo "   $RAID_DIR_BACKUP" umount $RAID_DIR_BACKUP rm -rf $RAID_DIR_BACKUP echo "   $RAID_DIR" umount $RAID_DIR rm -rf $RAID_DIR echo "   $WEBDAV_DIR1" umount $WEBDAV_DIR1 rm -rf $WEBDAV_DIR1 # ... #     ... #    rm $SYMLINK_WEBDAV } 


4. Help


Where is the normal script without help? We fix the situation.

 function print_help() { echo "   Webdav" echo echo ": $ME options..." echo ":" echo " -m   webdav." echo " -u  ." echo " -s  ." echo " -h ." echo } 


If the script is run without parameters, open help.

 if [ $# = 0 ]; then print_help fi 


5. Almost done


It remains to add the switching system of our functions, which we will do.

 while getopts msuh opt ; do case $opt in m) mount_webdav; ;; s) sync_webdav; ;; u) umount_webdav; ;; h) print_help ;; *) echo " "; echo "    $ME -h"; exit 1 ;; esac done 


Results


Here is the script, which, if desired, can be expanded to a serious tool. Using rsync, you can organize an incremental copy of data from account to account. You can add synchronization with the data on the disk, getting an analogue of the well-known Dropbox. It all depends on your imagination.

The main disadvantage of the above script is that it does not control the integrity of the original data, but only copies them to another location. I hope I still solve this problem.

Find the full version of the script, as well as help in the development can be here .

PS The script is still quite raw. The article will be updated and supplemented. Plans to make protection "from a fool", with checking mount directories for emptiness. Follow the development ...

Source: https://habr.com/ru/post/126488/


All Articles