Advertisement
draddy

nextcloudRestore.sh

Feb 12th, 2020
1,421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.39 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Bash script for restoring backups of Nextcloud.
  5. #
  6. # Version 1.0.0
  7. #
  8. # Usage:
  9. #   - With backup directory specified in the script: ./NextcloudRestore.sh <BackupName> (e.g. ./NextcloudRestore.sh 20170910_132703)
  10. #   - With backup directory specified by parameter: ./NextcloudRestore.sh <BackupName> <BackupDirectory> (e.g. ./NextcloudRestore.sh 20170910_132703 /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. # If you try to migrate to another server, the best way would be to reinstall the setup as befor using your composer file
  26. # resetup it the same way (dbuser / password) and bring it up one time (login with admin) after that - run the script to get your settings, apps and userdata back.
  27. #
  28.  
  29. # Variables
  30. restore=$1
  31. backupMainDir=$2
  32.  
  33. if [ -z "$backupMainDir" ]; then
  34.     # TODO: The directory where you store the Nextcloud backups (when not specified by args)
  35.     backupMainDir='/media/hdd/backup'
  36. fi
  37.  
  38.  
  39. currentRestoreDir="${backupMainDir}/${restore}"
  40.  
  41. echo "Restore from Backup: $backupMainDir"
  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 restored separately
  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. #Database containername
  64. dbDocker="$(php -r 'include("/tmp/tConfig.php"); print $CONFIG["dbhost"];')"
  65.  
  66. # (TODO): Your web server user
  67. webserverUser='abc'
  68.  
  69. # The name of the database system (ome of: mysql, mariadb, postgresql)
  70. databaseSystem="$(php -r 'include("/tmp/tConfig.php"); print $CONFIG["dbtype"];')"
  71.  
  72. # Your Nextcloud database name
  73. nextcloudDatabase="$(php -r 'include("/tmp/tConfig.php"); print $CONFIG["dbname"];')"
  74.  
  75. # Your Nextcloud database user
  76. dbUser="$(php -r 'include("/tmp/tConfig.php"); print $CONFIG["dbuser"];')"
  77.  
  78. # The password of the Nextcloud database user
  79. dbPassword="$(php -r 'include("/tmp/tConfig.php"); print $CONFIG["dbpassword"];')"
  80.  
  81.  
  82.  
  83.  
  84. # File names for backup files
  85. # If you prefer other file names, you'll also have to change the NextcloudBackup.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. #
  98. # Check if parameter(s) given
  99. #
  100. if [ $# != "1" ] && [ $# != "2" ]
  101. then
  102.     errorecho "ERROR: No backup name to restore given, or wrong number of parameters!"
  103.     errorecho "Usage: NextcloudRestore.sh 'BackupDate' ['BackupDirectory']"
  104.     exit 1
  105. fi
  106.  
  107. #
  108. # Check for root
  109. #
  110. if [ "$(id -u)" != "0" ]
  111. then
  112.     errorecho "ERROR: This script has to be run as root!"
  113.     exit 1
  114. fi
  115.  
  116. #
  117. # Check if backup dir exists
  118. #
  119. if [ ! -d "${currentRestoreDir}" ]
  120. then
  121.     errorecho "ERROR: Backup ${restore} not found!"
  122.     exit 1
  123. fi
  124.  
  125. #
  126. # Check if the commands for restoring the database are available
  127. #
  128. #if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
  129. #    if ! [ -x "$(command -v mysql)" ]; then
  130. #       errorecho "ERROR: MySQL/MariaDB not installed (command mysql not found)."
  131. #       errorecho "ERROR: No restore of database possible!"
  132. #       errorecho "Cancel restore"
  133. #       exit 1
  134. #    fi
  135. #elif [ "${databaseSystem,,}" = "postgresql" ]; then
  136. #    if ! [ -x "$(command -v psql)" ]; then
  137. #       errorecho "ERROR: PostgreSQL not in#talled (command psql not found)."
  138. #       errorecho "ERROR: No restore of database possible!"
  139. #        errorecho "Cancel restore"
  140. #        exit 1
  141. #   fi
  142. #fi
  143.  
  144. #
  145. # Set maintenance mode
  146. #
  147. echo "Set maintenance mode for Nextcloud..."
  148. docker exec $ncDocker sudo -u $webserverUser php /config/www/nextcloud/occ maintenance:mode --on
  149. echo "Done"
  150. echo
  151.  
  152. #
  153. # Stop web server
  154. #
  155. echo "Stopping web server..."
  156. docker stop $ncDocker
  157. echo "Done"
  158. echo
  159.  
  160. #
  161. # Delete old Nextcloud directories
  162. #
  163.  
  164. # File directory
  165. echo "Deleting old Nextcloud file directory..."
  166. rm -r "${nextcloudFileDir}"
  167. mkdir -p "${nextcloudFileDir}"
  168. echo "Done"
  169. echo
  170.  
  171. # Data directory
  172. echo "Deleting old Nextcloud data directory..."
  173. rm -r "${nextcloudDataDir}"
  174. mkdir -p "${nextcloudDataDir}"
  175. echo "Done"
  176. echo
  177.  
  178. # Local external storage
  179. # TODO: Uncomment if you use local external storage
  180. #echo "Deleting old Nextcloud local external storage directory..."
  181. #rm -r "${nextcloudLocalExternalDataDir}"
  182. #mkdir -p "${nextcloudLocalExternalDataDir}"
  183. #echo "Done"
  184. #echo
  185.  
  186. #
  187. # Restore file and data directory
  188. #
  189.  
  190. # File directory
  191. echo "Restoring Nextcloud file directory..."
  192. tar -xmpzf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
  193. echo "Done"
  194. echo
  195.  
  196. # Data directory
  197. echo "Restoring Nextcloud data directory..."
  198. tar -xmpzf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
  199. echo "Done"
  200. echo
  201.  
  202. # Local external storage
  203. # TODO: Uncomment if you use local external storage
  204. #echo "Restoring Nextcloud data directory..."
  205. #tar -xmpzf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
  206. #echo "Done"
  207. #echo
  208.  
  209. #
  210. # Restore database
  211. #
  212. echo "Dropping old Nextcloud DB..."
  213.  
  214. if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
  215.     docker exec $dbDocker mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase}"
  216. elif [ "${databaseSystem,,}" = "postgresql" ]; then
  217.     echo "postgresql not implemented ... feel free to do it by yourself ;)"
  218. #   sudo -u postgres psql -c "DROP DATABASE ${nextcloudDatabase};"
  219. fi
  220.  
  221. echo "Done"
  222. echo
  223.  
  224. echo "Creating new DB for Nextcloud..."
  225.  
  226. if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
  227.     # Use this if the databse from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames):
  228.     docker exec $dbDocker mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
  229.     # TODO: Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
  230.     #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
  231. elif [ "${databaseSystem,,}" = "postgresql" ]; then
  232.     echo "postgresql not implemented ... feel free to do it by yourself ;)"
  233. #   sudo -u postgres psql -c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \"UTF8\";"
  234. fi
  235.  
  236. echo "Done"
  237. echo
  238.  
  239. echo "Restoring backup DB..."
  240.  
  241. if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
  242.     docker exec -i $dbDocker mysql -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
  243. elif [ "${databaseSystem,,}" = "postgresql" ]; then
  244.     echo "postgresql not implemented ... feel free to do it by yourself ;)"
  245. #   sudo -u postgres psql "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
  246. fi
  247.  
  248. echo "Done"
  249. echo
  250.  
  251. #
  252. # Start web server
  253. #
  254. echo "Starting web server..."
  255. docker start $ncDocker
  256. echo "Done"
  257. echo
  258.  
  259. #
  260. # Set directory permissions
  261. #
  262. echo "Setting directory permissions..."
  263. docker exec $ncDocker chown -R "${webserverUser}":"${webserverUser}" "/config"
  264. docker exec $ncDocker chown -R "${webserverUser}":"${webserverUser}" "/data"
  265. # TODO: Uncomment if you use local external storage
  266. #chown -R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
  267. echo "Done"
  268. echo
  269.  
  270. #
  271. # Update the system data-fingerprint (see https://docs.nextcloud.com/server/16/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
  272. #
  273. echo "Updating the system data-fingerprint..."
  274. docker exec $ncDocker sudo -u "${webserverUser}" php /config/www/nextcloud/occ maintenance:data-fingerprint
  275. echo "Done"
  276. echo
  277.  
  278. #
  279. # Disbale maintenance mode
  280. #
  281. echo "Switching off maintenance mode..."
  282. docker exec $ncDocker sudo -u $webserverUser php /config/www/nextcloud/occ maintenance:mode --off
  283. echo "Done"
  284. echo
  285.  
  286. echo
  287. rm /tmp/tConfig.php #Delete Helperfile
  288. echo "DONE!"
  289. echo "Backup ${restore} successfully restored."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement