Advertisement
draddy

nextcloudBackup.sh

Feb 12th, 2020
1,531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.89 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Bash script for creating backups of Nextcloud.
  5. #
  6. # Version 1.0.0
  7. #
  8. # Usage:
  9. #   - With backup directory specified in the script:  ./NextcloudBackup.sh
  10. #   - With backup directory specified by parameter: ./NextcloudBackup.sh <BackupDirectory> (e.g. ./NextcloudBackup.sh /media/hdd/nextcloud_backup)
  11. #
  12. # The script is based on an installation of Nextcloud using nginx and MariaDB, see https://decatec.de/home-server/nextcloud-auf-ubuntu-server-18-04-lts-mit-nginx-mariadb-php-lets-encrypt-redis-und-fail2ban/
  13. #
  14.  
  15. #
  16. # IMPORTANT
  17. # You have to customize this script (directories, users, etc.) for your actual environment.
  18. # All entries which need to be customized are tagged with "TODO".
  19. #
  20.  
  21. #
  22. # Customisations by Draddy to fit with Docker install with this guide: https://forum.openmediavault.org/index.php/Thread/28216-How-To-Nextcloud-with-Letsencrypt-using-OMV-and-docker-compose/
  23. # all "(TODO)" should work if you don't change the composer file "TODO" still need your attantion
  24. #
  25.  
  26. # Variables
  27.  
  28. #Backup user Data? true or false
  29. backupUserData=true
  30. backupMainDir=$1
  31.  
  32. if [ -z "$backupMainDir" ]; then
  33.     # TODO: The directory where you store the Nextcloud backups (when not specified by args)
  34.     backupMainDir='/media/hdd/backup'
  35. fi
  36.  
  37.  
  38. currentDate=$(date +"%Y%m%d_%H%M%S")
  39.  
  40. # The actual directory of the current backup - this is a subdirectory of the main directory above with a timestamp
  41. backupdir="${backupMainDir}/${currentDate}/"
  42.  
  43. # TODO: The directory of your appdata
  44. appdataDir='/srv/dev-disk-by-label-disk1/appdata'
  45.  
  46. # (TODO): should work if you follow the guide
  47. nextcloudFileDir="${appdataDir}/nextcloud/config"
  48.  
  49. # (TODO): The directory of your Nextcloud data directory (outside the Nextcloud file directory)
  50. # If your data directory is located under Nextcloud's file directory (somewhere in the web root), the data directory should not be a separate part of the backup
  51. nextcloudDataDir="${appdataDir}/nextcloud/data"
  52.  
  53. # TODO: The directory of your Nextcloud's local external storage.
  54. # Uncomment if you use local external storage.
  55. #nextcloudLocalExternalDataDir='/var/nextcloud_external_data'
  56.  
  57. # config.php to get some values from - Helperfile - can't set a var to the wrapper - will deleted later
  58. cp $"${nextcloudFileDir}/www/nextcloud/config/config.php" /tmp/tConfig.php
  59.  
  60. #(TODO): Nextcloud containername
  61. ncDocker="nextcloud"
  62.  
  63. #The name of the database system (ome of: mysql, mariadb, postgresql)
  64. databaseSystem="$(php -r 'include("/tmp/tConfig.php"); print $CONFIG["dbtype"];')"
  65.  
  66. #Your Nextcloud database name
  67. nextcloudDatabase="$(php -r 'include("/tmp/tConfig.php"); print $CONFIG["dbname"];')"
  68.  
  69. #Database containername
  70. dbDocker="$(php -r 'include("/tmp/tConfig.php"); print $CONFIG["dbhost"];')"
  71.  
  72. #Your Nextcloud database user
  73. dbUser="$(php -r 'include("/tmp/tConfig.php"); print $CONFIG["dbuser"];')"
  74.  
  75. #The password of the Nextcloud database user
  76. dbPassword="$(php -r 'include("/tmp/tConfig.php"); print $CONFIG["dbpassword"];')"
  77.  
  78. # (TODO): Your web server user - abc is the default in linuxserver.io containers
  79. webserverUser='abc'
  80.  
  81. # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
  82. maxNrOfBackups=0
  83.  
  84. # File names for backup files
  85. # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
  86. fileNameBackupFileDir='nextcloud-filedir.tar.gz'
  87. fileNameBackupDataDir='nextcloud-datadir.tar.gz'
  88.  
  89. # TODO: Uncomment if you use local external storage
  90. #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
  91.  
  92. fileNameBackupDb='nextcloud-db.sql'
  93.  
  94. # Function for error messages
  95. errorecho() { cat <<< "$@" 1>&2; }
  96.  
  97. rm /tmp/tConfig.php #Delete Helperfile
  98.  
  99.  
  100.  
  101. function DisableMaintenanceMode() {
  102.     echo "Switching off maintenance mode..."
  103.     docker exec $ncDocker sudo -u $webserverUser php /config/www/nextcloud/occ maintenance:mode --off
  104.     echo "Done"
  105.     echo
  106. }
  107.  
  108. # Capture CTRL+C
  109. trap CtrlC INT
  110.  
  111. function CtrlC() {
  112.     read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
  113.     echo
  114.  
  115.     if ! [[ $REPLY =~ ^[Yy]$ ]]
  116.     then
  117.         DisableMaintenanceMode
  118.     else
  119.         echo "Maintenance mode still enabled."
  120.     fi
  121.  
  122.     exit 1
  123. }
  124.  
  125. #
  126. # Check for root
  127. #
  128. if [ "$(id -u)" != "0" ]
  129. then
  130.     errorecho "ERROR: This script has to be run as root!"
  131.     exit 1
  132. fi
  133.  
  134. #
  135. # Check if backup dir already exists
  136. #
  137. if [ ! -d "${backupdir}" ]
  138. then
  139.     mkdir -p "${backupdir}"
  140. else
  141.     errorecho "ERROR: The backup directory ${backupdir} already exists!"
  142.     exit 1
  143. fi
  144.  
  145. #
  146. # Set maintenance mode
  147. #
  148. echo "Set maintenance mode for Nextcloud..."
  149. docker exec $ncDocker sudo -u $webserverUser php /config/www/nextcloud/occ maintenance:mode --on
  150. echo "Done"
  151. echo
  152.  
  153. #
  154. # Stop web server
  155. #
  156. echo "Stopping web server..."
  157. docker stop $ncDocker
  158. echo "Done"
  159. echo
  160.  
  161. #
  162. # Backup file directory
  163. #
  164. echo "Creating backup of Nextcloud file directory..."
  165. tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
  166. echo "Done"
  167. echo
  168.  
  169. #
  170. # Backup data directory
  171. #
  172. if  $backupUserData -eq "true"  ; then
  173.     echo "Creating backup of Nextcloud data directory..."
  174.     tar -cpzf "${backupdir}/${fileNameBackupDataDir}"  -C "${nextcloudDataDir}" .
  175.     echo "Done"
  176.     echo
  177. fi
  178.  
  179. # Backup local external storage.
  180. # Uncomment if you use local external storage
  181. #echo "Creating backup of Nextcloud local external storage directory..."
  182. #tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}"  -C "${nextcloudLocalExternalDataDir}" .
  183. #echo "Done"
  184. #echo
  185.  
  186. #
  187. # Backup DB
  188. #
  189. if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
  190.     echo "Backup Nextcloud database (MySQL/MariaDB)..."
  191.         docker exec $dbDocker mysqldump --single-transaction -h localhost -u"$dbUser" -p"$dbPassword" $nextcloudDatabase > "${backupdir}/${fileNameBackupDb}"
  192.     echo "Done"
  193.     echo
  194. elif [ "${databaseSystem,,}" = "postgresql" ]; then
  195.     echo "Backup for postgresql not implemented ... feel free to do it by yourself ;)"
  196. #   echo "Backup Nextcloud database (PostgreSQL)..."
  197. #
  198. #   if ! [ -x "$(command -v pg_dump)" ]; then
  199. #       errorecho "ERROR:PostgreSQL not installed (command pg_dump not found)."
  200. #       errorecho "ERROR: No backup of database possible!"
  201. #   else
  202. #       PGPASSWORD="${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb}"
  203. #   fi
  204. #
  205. #   echo "Done"
  206. #   echo
  207. fi
  208.  
  209. #
  210. # Start web server
  211. #
  212. echo "Starting web server..."
  213. docker start $ncDocker
  214. echo "Done"
  215. echo
  216.  
  217. #
  218. # Disable maintenance mode
  219. #
  220. DisableMaintenanceMode
  221.  
  222. #
  223. # Delete old backups
  224. #
  225. if [ ${maxNrOfBackups} != 0 ]
  226. then
  227.     nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
  228.  
  229.     if [[ ${nrOfBackups} > ${maxNrOfBackups} ]]
  230.     then
  231.         echo "Removing old backups..."
  232.         ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read -r dirToRemove; do
  233.             echo "${dirToRemove}"
  234.             rm -r "${backupMainDir}/${dirToRemove:?}"
  235.             echo "Done"
  236.             echo
  237.         done
  238.     fi
  239. fi
  240.  
  241. echo
  242. echo "DONE!"
  243. echo "Backup created: ${backupdir}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement