Update Nextcloud backup #2
This commit is contained in:
		
							parent
							
								
									6fab3a5754
								
							
						
					
					
						commit
						5372903c26
					
				
					 2 changed files with 78 additions and 40 deletions
				
			
		|  | @ -1,40 +0,0 @@ | ||||||
| #!/bin/bash |  | ||||||
| # Backup script for Nextcloud in a kubernetes cluster |  | ||||||
| 
 |  | ||||||
| BACKUP_DEST='/mnt/PRIVATE_DOCS/home/backup' |  | ||||||
| PASSFILE='./nextcloud_pass.txt' |  | ||||||
| 
 |  | ||||||
| # Create filename for database |  | ||||||
| database_backupfile="nextcloud-sqlbkp_$(date +'%Y%m%d').bak" |  | ||||||
| 
 |  | ||||||
| # Retrieve container names |  | ||||||
| base_container="$( docker ps --format '{{.Names}}' | grep hugocloud-nextcloud_hugocloud-nextcloud )" |  | ||||||
| database_container="$( docker ps --format '{{.Names}}' | grep hugocloud-postgresql_hugocloud-postgresql )" |  | ||||||
| 
 |  | ||||||
| # Abort entire script if any command fails |  | ||||||
| set -e |  | ||||||
| 
 |  | ||||||
| # Turn on maintenance mode |  | ||||||
| docker exec --user www-data "${base_container}" php occ maintenance:mode --on |  | ||||||
| 
 |  | ||||||
| # Database backup |  | ||||||
| >&2 echo 'Backing up database' |  | ||||||
| internal_database_backupfile="/tmp/${database_backupfile}" |  | ||||||
| docker exec --env-file "${PASSFILE}" "${database_container}" pg_dump 'nextcloud' -cwv -h 'localhost' -U 'nextcloud' -f "${internal_database_backupfile}" |  | ||||||
| docker cp "${database_container}":"${internal_database_backupfile}" "${BACKUP_DEST}" |  | ||||||
| 
 |  | ||||||
| # Files backup |  | ||||||
| for file in 'config' 'themes'; do |  | ||||||
| 	>&2 printf 'Copying %s\n' "${file}" |  | ||||||
| 	docker cp -a "${base_container}":"/var/www/html/${file}" "${BACKUP_DEST}" |  | ||||||
| done |  | ||||||
| 
 |  | ||||||
| # Turn off maintenance mode |  | ||||||
| docker exec --user www-data "${base_container}" php occ maintenance:mode --off |  | ||||||
| 
 |  | ||||||
| # Backup cleanup |  | ||||||
| # Only keep 30 days of backups |  | ||||||
| >&2 echo 'Clean up old database backups' |  | ||||||
| find "${BACKUP_DEST}" -name '*sqlbkp*' -type f -mtime +30 -print -delete |  | ||||||
| 
 |  | ||||||
| >&2 echo 'Done' |  | ||||||
							
								
								
									
										78
									
								
								scripts/nextcloud/backup-database.sh
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										78
									
								
								scripts/nextcloud/backup-database.sh
									
										
									
									
									
										Executable file
									
								
							|  | @ -0,0 +1,78 @@ | ||||||
|  | #!/bin/bash | ||||||
|  | # Backup Nextcloud database in a Kubernetes environment. | ||||||
|  | # Usage: backup-database <destination> | ||||||
|  | 
 | ||||||
|  | usage() { | ||||||
|  |     >&2 printf "Usage: %s <destination>\n" "${0}" | ||||||
|  |     exit "${1:-1}" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | # Get options | ||||||
|  | 
 | ||||||
|  | while getopts ":e:" option; do | ||||||
|  |     case "${option}" in | ||||||
|  |         e) | ||||||
|  |             if ! [ -f "${OPTARG}" ]; then | ||||||
|  |                 >&2 printf "Error: Specified environment file does not exist: '%s'.\n" "${OPTARG}" | ||||||
|  |             elif ! [ -r "${OPTARG}" ]; then | ||||||
|  |                 >&2 printf "Error: Specified environment file is not readable: '%s'.\n" "${OPTARG}" | ||||||
|  |             fi | ||||||
|  |             env_file="${OPTARG}" | ||||||
|  |             ;; | ||||||
|  |         *) | ||||||
|  |             >&2 printf "Error: Invalid option: '%s'.\n" "${option}" | ||||||
|  |             usage | ||||||
|  |             ;; | ||||||
|  |     esac | ||||||
|  | done | ||||||
|  | 
 | ||||||
|  | # Check arguments. | ||||||
|  | 
 | ||||||
|  | if [ $# -ne 1 ]; then | ||||||
|  |     >&2 printf "Error: You need to specify a destination.\n" | ||||||
|  |     usage | ||||||
|  | elif ! [ -d "${1}" ]; then | ||||||
|  |     >&2 printf "Error: Specified destination does not exist or is not readable : '%s'.\n" "${1}" | ||||||
|  |     usage | ||||||
|  | else | ||||||
|  |     destination="${1}" | ||||||
|  | fi | ||||||
|  | 
 | ||||||
|  | # Filename for database backup | ||||||
|  | database_backupfile="nextcloud-sqlbkp_$(date +'%Y%m%d').bak" | ||||||
|  | 
 | ||||||
|  | # Retrieve container names | ||||||
|  | base_container="$( docker ps --format '{{.Names}}' | grep -E 'nextcloud-2_nextcloud-2-[0-9a-z]{10}-[0-9a-z]{5}' )" | ||||||
|  | database_container="$( docker ps --format '{{.Names}}' | grep postgres_nextcloud-2-cnpg-main-1 )" | ||||||
|  | 
 | ||||||
|  | if ! [[ -n "${base_container}" && -n "${database_container}" ]]; then | ||||||
|  |     >&2 printf "Error: Not all containers could be found.\n" | ||||||
|  |     exit 2 | ||||||
|  | fi | ||||||
|  | 
 | ||||||
|  | # Abort entire script if any command fails | ||||||
|  | set -e | ||||||
|  | 
 | ||||||
|  | # Turn on maintenance mode | ||||||
|  | docker exec "${base_container}" php occ maintenance:mode --on | ||||||
|  | 
 | ||||||
|  | # Database backup | ||||||
|  | echo 'Backing up database' | ||||||
|  | host_database_backupfile="${destination}/${database_backupfile}" | ||||||
|  | docker exec --env-file "${env_file:='./nextcloud.env'}" "${database_container}" pg_dump 'nextcloud' -cwv -h 'localhost' -U 'nextcloud' > "${host_database_backupfile}" | ||||||
|  | 
 | ||||||
|  | # Files backup | ||||||
|  | for file in 'config' 'themes'; do | ||||||
|  | 	printf 'Copying %s\n' "${file}" | ||||||
|  |     docker cp -a "${base_container}":"/var/www/html/${file}" "${destination}" | ||||||
|  | done | ||||||
|  | 
 | ||||||
|  | # Turn off maintenance mode | ||||||
|  | docker exec "${base_container}" php occ maintenance:mode --off | ||||||
|  | 
 | ||||||
|  | # Backup cleanup | ||||||
|  | # Only keep 30 days of backups | ||||||
|  | printf 'Clean up old database backups' | ||||||
|  | find "${destination}" -name '*sqlbkp*' -type f -mtime +30 -print -delete | ||||||
|  | 
 | ||||||
|  | printf 'Done' | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue