How to use rsync with samba share

8,119

I wrote a script to solve this. Some things are in Italian but i think you can easily understan how the scripts works. Anyway you can easily translate everything.

The script basically checks if the blackberry is connected to the network. If yes, it checks if a dummy file exists, if it does, it means that another istance of the script is already running so it exits. If no other istances are running, it checks when is the last time the script was ran (so not to backup the blackberry every 10minutes). If a dummy.file.time is older than 3 hours (10800 seconds), it starts the backup using rsync.

#!/bin/sh
# BlackBerry backup via SMB share
#
# Cosa fa lo script?
#
# Lo script fa il ping del blackberry
# Se il ping va a buon fine, controlla se esiste "$dummyfile"
# (per non eseguire più backup in parallelo).
# Se esiste "$dummyfile", controlla se "$dummyfiletime" è più
# vecchio di due ore (per non ripetere il backup in continuazione).
# Se "$dummyfiletime" è più vecchio di due ore, monta lo SMB share
# del blackberry e inizia il backup con rsync.
#


##### PARAMETERS START #####
logdate=$(date +"%m-%Y")
ip="192.168.1.5"
dummyfile="/media/truecrypt2/blackberry SMB Backup/dummy.file"
dummyfiletime="/media/truecrypt2/blackberry SMB Backup/dummy.file.time"
monta="mount -t cifs -o username=USER,password=PASSWORD //$ip/media/ /media/blackberrySMB/"
sincronizza="rsync -a /media/blackberrySMB /media/truecrypt2/blackberry\ SMB\ Backup"
scriptlog="/var/log/blackberrySMBbackup/script_$logdate.log"
rsynclog="/var/log/blackberrySMBbackup/rsync_$logdate.log"
dummyfiletimeage=$(( `date +%s` - `stat -L --format %Y "$dummyfiletime"` )) #age of the dummy.file.time
defaultage="10800" #in seconds
###### PARAMETERS END ######

echo "$(date +"%D %T") : BlackBerry SMB backup lanciato." >>$scriptlog
echo "Effettuo ping verso $ip" >>$scriptlog
if fping -c 1 -t 500 $ip >>/dev/null
then
  echo "Risposta al ping ricevuta da $ip" >>$scriptlog
  if [ -f "$dummyfile" ]
  then
    echo -e "dummy.file esistente. Forse il processo e gia in esecuzione. Esco.\n" >>$scriptlog
    exit
  else
    echo "dummy.file non esistente. Controllo se dummy.file.time e piu vecchio di 3 ore." >>$scriptlog
    if [ "$dummyfiletimeage" -gt "$defaultage" ]
    then
      echo "dummy.file.time e piu vecchio di 3 ore. Inizio il backup." >>$scriptlog
      touch "$dummyfile"
      rm "$dummyfiletime"
      touch "$dummyfiletime"
      umount //$ip/media/
      umount //$ip/media/
      $monta
      echo "$(date +"%D %T") : BlackBerry SMB rsync lanciato." >>$rsynclog
      rsync --verbose -a /media/blackberrySMB /media/truecrypt2/blackberry\ SMB\ Backup >>$rsynclog
      echo "***************************" >>$rsynclog
      umount //192.168.1.5/media/
      rm "$dummyfile"
      echo -e "Backup completato. Esco.\n" >>$scriptlog
      exit
    else
      echo -e "dummy.file.time e piu recente di 3 ore. Esco.\n" >>$scriptlog
      exit
    fi
  fi
else
  echo -e "Nessuna risposta al ping da $ip. Blackberry non connesso. Esco.\n" >>$scriptlog
  exit
fi
Share:
8,119

Related videos on Youtube

user5613506
Author by

user5613506

Updated on September 18, 2022

Comments

  • user5613506
    user5613506 over 1 year

    I would like to synchronize all the content of my phone to my home server using the phone's samba share.

    My approach is to write a script that mounts the phone's samba share, and then copies all the files on the phone to the specified directory. Then the script is ran every 10 minutes with crontab.

    The first problem I am facing is that I would like the two folders (phone and server) to have a "contribute" relationship. This means that: new and updated files are copied from the phone to the server. Renames on the phone are repeated on the server. No deletions (if a file is deleted on the phone, it remains on the server). How can I achieve this? Maybe with rsync?

    The second problem is: is there a better approach than trying to mount the samba share every 10 minutes to find out if the phone is connected or not to the wifi network?

  • user5613506
    user5613506 about 9 years
    Do you know any tool like Microsoft SyncToy? It is a software for windows that does exactly what I want (contribute relationship between the two folders).
  • Nils
    Nils about 9 years
    @giovi unison might go into the same direction.
  • user5613506
    user5613506 about 9 years
    do you know what happens if the transfer is interrupted? Will it continue the transfer or will it start over again after?
  • Nils
    Nils about 9 years
    @giovi321 rsync will do what it is told to do. By default it will not transfer files in-place. So if there is an interrupt, the target file will be to old one. If you restart rsync, it will do the same thing that it ever does: Look at both sides and then it will start to transfer the delta. There is a whole-file-mode as well as a partial file mode (see man rsync about details) - so it will either transfer whole files or just changes within files.