Como hacer una copia integra del HA y de Raspbian, desde terminal…

Tal y como os prometimos, toca hacer un pequeño manual de como podemos hacer una copia de nuestra tarjeta MicroSD, sin tener que sacarla a otra tarjeta os USB, si en su momento decidimos instalar el Raspbian Lite, Hassbian o simplemente no queremos usar la aplicación de escritorio.

Para ello, os pasaremos un script, con el que se realizará todo de forma automática. Como ya comentamos en el manual anterior, necesitaremos introducir otra MicroSD, con un adaptador USB en uno de los puertos de la Pi.

Nos conectamos por SSH o abrimos un terminal desde la misma Pi, vamos a crear el siguiente script, con el comando:

bash

  1. sudo nano Raspberry_Pi_Backup_Script.sh

En el que copiaremos las siguientes líneas:

bash

  1. #!/bin/bash
  2.  
  3. # Usage instructions at http://www.megaleecher.net/Best_Raspberry_Pi_Hot_Backup_Shell_Script
  4. # This version disables backup image compression as it takes too much time on Pi, to enable uncomment the relavent lines
  5. # Setting up directories, Just change SUBDIR and DIR varibales below to get going
  6.  
  7. SUBDIR=raspberrypi_backups
  8. DIR=/media/ExternalHd/$SUBDIR
  9.  
  10. echo "Starting RaspberryPI backup process!"
  11.  
  12. # First check if pv package is installed, if not, install it first
  13. PACKAGESTATUS=`dpkg -s pv | grep Status`;
  14.  
  15. if [[ $PACKAGESTATUS == S* ]]
  16. then
  17. echo "Package 'pv' is installed."
  18. else
  19. echo "Package 'pv' is NOT installed."
  20. echo "Installing package 'pv'. Please wait..."
  21. apt-get -y install pv
  22. fi
  23.  
  24. # Check if backup directory exists
  25. if [ ! -d "$DIR" ];
  26. then
  27. echo "Backup directory $DIR doesn't exist, creating it now!"
  28. mkdir $DIR
  29. fi
  30.  
  31. # Create a filename with datestamp for our current backup (without .img suffix)
  32. OFILE="$DIR/backup_$(date +%Y%m%d_%H%M%S)"
  33.  
  34. # Create final filename, with suffix
  35. OFILEFINAL=$OFILE.img
  36.  
  37. # First sync disks
  38. sync; sync
  39.  
  40. # Shut down some services before starting backup process
  41. echo "Stopping some services before backup."
  42. service apache2 stop
  43. service mysql stop
  44. service cron stop
  45.  
  46. # Begin the backup process, should take about 1 hour from 8Gb SD card to HDD
  47. echo "Backing up SD card to USB HDD."
  48. echo "This will take some time depending on your SD card size and read performance. Please wait..."
  49. SDSIZE=`blockdev --getsize64 /dev/mmcblk0`;
  50. pv -tpreb /dev/mmcblk0 -s $SDSIZE | dd of=$OFILE bs=1M conv=sync,noerror iflag=fullblock
  51.  
  52. # Wait for DD to finish and catch result
  53. RESULT=$?
  54.  
  55. # Start services again that where shutdown before backup process
  56. echo "Start the stopped services again."
  57. service apache2 start
  58. service mysql start
  59. service cron start
  60.  
  61. # If command has completed successfully, delete previous backups and exit
  62. if [ $RESULT = 0 ];
  63. then
  64. echo "Successful backup, previous backup files will be deleted."
  65. rm -f $DIR/backup_*.tar.gz
  66. mv $OFILE $OFILEFINAL
  67. echo "Backup is being tarred. Please wait..."
  68. #tar zcf $OFILEFINAL.tar.gz $OFILEFINAL
  69. # rm -rf $OFILEFINAL
  70. echo "RaspberryPI backup process completed! FILE: $OFILEFINAL"
  71. exit 0
  72. # Else remove attempted backup file
  73. else
  74. echo "Backup failed! Previous backup files untouched."
  75. echo "Please check there is sufficient space on the HDD."
  76. rm -f $OFILE
  77. echo "RaspberryPI backup process failed!"
  78. exit 1
  79. fi

 

Una vez creado el script, para ejecutarlo escribiremos:

bash

  1. sudo ./Raspberry_Pi_Backup_Script.sh

…veremos el progreso de la copia y nos avisará cuando finalice.

Y eso es todo.

 

Fuente y Fuente GitHub

 

También te podría gustar...

Deja un comentario