Advertisement
Guest User

Backup to box.com via curl/WebDAV

a guest
Mar 5th, 2013
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.75 KB | None | 0 0
  1. #!/bin/sh
  2. LOGIN=your@box.com # Your box.com login (email)
  3. PASS=SomePass # Your box.com password
  4. LOCALDIR=/home/backups/sites # Local path to backup
  5. REMOTEDIR=/backups/sites # Remote storage path
  6. MAXFS=190000000 # Maximum file size allowed without splitting in bytes
  7. URL=https://www.box.net/dav # WebDAV URL. Leave this for Box.com.
  8.  
  9. cd $LOCALDIR
  10. # Traverse into each directory inside LOCALDIR
  11. for dirs in *; do
  12.     # Now we need to create same directory on remote, else upload fails
  13.     echo Creating remote $REMOTEDIR/$dirs...
  14.     curl -u $LOGIN:$PASS -X MKCOL $URL$REMOTEDIR/$dirs
  15.     # Now lets work with each file which ends on current date.tar.gz (can change to * for every file)
  16.     for files in $dirs/*-`date +%Y-%m-%d`.tar.gz; do
  17.         # Let's check if file size are less than MAXFS.
  18.         fs=$( stat -c %s $files )
  19.         # If smaller -> upload directly.
  20.         if [ $fs -lt $MAXFS ]; then
  21.             echo Uploading $LOCALDIR/$files...
  22.             curl --user $LOGIN:$PASS -T $files $URL$REMOTEDIR/$files
  23.         # Else we are splitting in into parts by MAXFS bytes then upload them as basename.partnum
  24.         else
  25.             echo $LOCALDIR/$files more than $MAXFS bytes, splitting.
  26.             cnt=0
  27.             # Split file to temporary parts in /tmp. $$ is the current PID, you can do something else.
  28.             split -b $MAXFS $files /tmp/$$.
  29.             for splits in /tmp/$$.*; do
  30.                 echo Uploading part $cnt $REMOTEDIR/$dirs/`basename $files`.$cnt
  31.                 curl --user $LOGIN:$PASS -T $splits $URL$REMOTEDIR/$dirs/`basename $files`.$cnt
  32.                 cnt=$((cnt+1))
  33.             done
  34.             # Clean up temporary parts in /tmp after finishing uploads.
  35.             rm /tmp/$$.*
  36.         fi
  37.     done
  38. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement