Advertisement
elythomaslumber

Raspberry Pi SD-card image-back-up to WIn7

Aug 7th, 2013
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.77 KB | None | 0 0
  1. 1.  Make your last manual image via Win32DiskImager (if something will going wrong)
  2. 2.  Watch that video until 1:28 and do it: http://www.youtube.com/watch?v=1_qp74VU5ao
  3. 3.  Create a new folder on your Raspi:  /home/pi/mynetworkdrive
  4. 4.  Edit the this file:             sudo nano /etc/fstab
  5. 5.  Add the following line:     //192.168.x.xxx/folder /home/pi/mynetworkdrive cifs guest,_netdev 0 0
  6.                     replace 192.168.x.xxx with the actual IP-address of your network drive)
  7. 6.  Mount the new drive:            sudo mount /home/pi/mynetworkdrive
  8. 7(Reboot your Raspberry PI:      sudo reboot (just to make sure… maybe not necessary))
  9. 8.  Place the following script (raspy_back-up.sh) into:  home/pi/domoticz/scripts
  10. 9.  Make it executable and…
  11. 10. …exchange the IPadress with your IPadress
  12.     a.  check also if all directories are the same as in the script
  13.     b.  check if you need to have pv installed
  14.     c.  check if you want to stop and start services
  15. 11. Save the edited script and start it from PUTTY…
  16.  
  17. ----------------------------------------------------------------------
  18.  
  19. #!/bin/sh
  20.  
  21. # this script will make an image from a raspberry pi SD card without a shut-down
  22. # back-up image will be stored on a network-drive or a network drive on a WIN7 PC
  23. # check the path of your virtual network-drive on raspberry -> is it also: /home/pi/mynetworkdrive ???
  24. # check your path to your scripts on your Raspy -> is it also: /home/pi/domoticz/scripts ???
  25. # check the services you want to stop during back-up -> here: APACHE2, mysql, crontab
  26.  
  27. ipadress="192.168.1.12"                  # IP adress of network-drive in your LAN
  28.  
  29. echo "Starting RaspberryPI backup process!"
  30.  
  31. # -------------------------------------------------------------------
  32. # First check if pv package is installed, if not, install it first
  33. # pv is not needed if you do not like pv to install remove this part
  34. # do not forget also to remove the pv row before the dd command
  35.  
  36. PACKAGESTATUS=`dpkg -s pv | grep Status`
  37. if [ "$PACKAGESTATUS" = "Status: install ok installed" ] ; then
  38.       echo "Package 'pv' is installed."
  39. else
  40.       echo "Package 'pv' is NOT installed."
  41.       echo "Installing package 'pv'. Please wait..."
  42.       sudo apt-get -y install pv
  43. fi
  44. # end of pv installation
  45. # -------------------------------------------------------------------
  46.  
  47. # Check if destination network drive is accessable
  48. echo "Check if destination network drive is accessable."
  49. echo "Trying to PING network drive on $ipadress."
  50. ping -c 1 -t 1 $ipadress > /dev/null            # PING to static IP
  51.  
  52. if [ $? -eq 0 ] ; then                          # if available in local LAN, then do...
  53.                                  
  54.    echo "Network drive found."
  55.            
  56. else                                                 # not found in LAN
  57.  
  58.    echo "Back-up not possible. No network-drive found. Check IPadress of your network drive."
  59.     exit 1
  60. fi
  61.  
  62. # check if network drive is mounted
  63. if [ "`mount | grep /home/pi/mynetworkdrive`" ] ; then
  64.     echo "Network drive is still mounted."
  65. else
  66. echo "Mounting network drive."
  67.     sudo mount /home/pi/mynetworkdrive
  68. fi
  69.  
  70. # Create a filename with datestamp for our current backup
  71. OFILE="/home/pi/mynetworkdrive/backup_$(date +%Y%m%d_%H%M%S).img"
  72. echo "Generating new back-up file $OFILE"
  73.  
  74. # First sync disks
  75. sync; sync
  76.  
  77. # Shut down some services before starting backup process, remove or add services
  78. echo "Stopping some services before backup."
  79. sudo service apache2 stop
  80. sudo service mysql stop
  81. sudo service cron stop
  82.  
  83. # Begin the backup process, should take about 1.5 hour from 16 Gb SD card
  84. echo "Backing up SD card to WIN7 network drive."
  85. echo "This will take some time depending on your SD card size and read performance. Please wait..."
  86.  
  87. # get SD card size
  88. SDSIZE=`sudo blockdev --getsize64 /dev/mmcblk0`
  89. echo "SD card size: $SDSIZE"
  90.  
  91. # ---------------------------------------------------------------------------
  92. # remove this part if you have no pv installed but use the dd part stand alone
  93. sudo pv /dev/mmcblk0 -s $SDSIZE | sudo dd of=$OFILE bs=1M conv=sync,noerror iflag=fullblock
  94. # ---------------------------------------------------------------------------
  95.  
  96. # use this dd row if you do not want to use pv
  97. #sudo dd if=/dev/mmcblk0 of=$OFILE bs=1M conv=sync,noerror iflag=fullblock
  98. # ---------------------------------------------------------------------------
  99.  
  100. # Wait for DD to finish and catch result
  101. RESULT=$?
  102.  
  103. # Start services again that where shutdown before backup process, remove or add services
  104. echo "Start the stopped services again."
  105. sudo service apache2 start
  106. sudo service mysql start
  107. sudo service cron start
  108.  
  109. # If command has completed , give a state message
  110. if [ $RESULT = 0 ];
  111.    then
  112.       echo "Raspberry Pi SD card backup image generated."
  113.       exit 0
  114. # Else state a failure message
  115.    else
  116.       echo "RaspberryPI backup process failed!"
  117.       exit 1
  118. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement