]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
Merge pull request #1 from BernieO/patch-1
[nextcloud-backup-restore.git] / NextcloudBackup.sh
1 #!/bin/bash
2
3 #
4 # Bash script for creating backups of Nextcloud.
5 # Usage: ./NextcloudBackup.sh
6 #
7 # The script is based on an installation of Nextcloud using nginx and MariaDB, see https://decatec.de/home-server/nextcloud-auf-ubuntu-server-mit-nginx-mariadb-php-lets-encrypt-redis-und-fail2ban/
8 #
9
10 #
11 # IMPORTANT
12 # You have to customize this script (directories, users, etc.) for your actual environment.
13 # All entries which need to be customized are tagged with "TODO".
14 #
15
16 # Variables
17 currentDate=$(date +"%Y%m%d_%H%M%S")
18 # TODO: The directory where you store the Nextcloud backups
19 backupMainDir="/mnt/Share/NextcloudBackups/"
20 # The actual directory of the current backup - this is is subdirectory of the main directory above with a timestamp
21 backupdir="${backupMainDir}/${currentDate}/"
22 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
23 nextcloudFileDir="/var/www/nextcloud"
24 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
25 # 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
26 nextcloudDataDir="/var/nextcloud_data"
27 # TODO: Your Nextcloud database name
28 nextcloudDatabase="nextcloud_db"
29 # TODO: Your Nextcloud database user
30 dbUser="nextcloud_db_user"
31 # TODO: The password of the Nextcloud database user
32 dbPassword="mYpAsSw0rd"
33 # TODO: Your webserver user
34 webserverUser="www-data"
35 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
36 maxNrOfBackups=0
37
38 # File names for backup files
39 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
40 fileNameBackupFileDir="nextcloud-filedir.tar.gz"
41 fileNameBackupDataDir="nextcloud-datadir.tar.gz"
42 fileNameBackupDb="nextcloud-db.sql"
43
44 # Function for error messages
45 errorecho() { cat <<< "$@" 1>&2; }
46
47 #
48 # Check for root
49 #
50 if [ "$(id -u)" != "0" ]
51 then
52 errorecho "ERROR: This script has to be run as root!"
53 exit 1
54 fi
55
56 #
57 # Check if backup dir already exists
58 #
59 if [ ! -d "${backupdir}" ]
60 then
61 mkdir -p "${backupdir}"
62 else
63 errorecho "ERROR: The backup directory ${backupdir} already exists!"
64 exit 1
65 fi
66
67 #
68 # Set maintenance mode
69 #
70 echo "Set maintenance mode for Nextcloud..."
71 cd "${nextcloudFileDir}"
72 sudo -u "${webserverUser}" php occ maintenance:mode --on
73 cd ~
74 echo "Done"
75 echo
76
77 #
78 # Stop webserver
79 #
80 echo "Stopping nginx..."
81 service nginx stop
82 echo "Done"
83 echo
84
85 #
86 # Backup file and data directory
87 #
88 echo "Creating backup of Nextcloud file directory..."
89 tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
90 echo "Done"
91 echo
92
93 echo "Creating backup of Nextcloud data directory..."
94 tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
95 echo "Done"
96 echo
97
98 #
99 # Backup DB
100 #
101 echo "Backup Nextcloud database..."
102 mysqldump --single-transaction -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb}"
103 echo "Done"
104 echo
105
106 #
107 # Start webserver
108 #
109 echo "Starting webserver..."
110 service nginx start
111 echo "Done"
112 echo
113
114 #
115 # Disable maintenance mode
116 #
117 echo "Switching off maintenance mode..."
118 cd "${nextcloudFileDir}"
119 sudo -u "${webserverUser}" php occ maintenance:mode --off
120 cd ~
121 echo "Done"
122 echo
123
124 #
125 # Delete old backups
126 #
127 if [ ${maxNrOfBackups} != 0 ]
128 then
129 nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
130
131 if [ ${maxNrOfBackups} > ${nrOfBackups} ]
132 then
133 echo "Removing old backups..."
134 ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read dirToRemove; do
135 echo "${dirToRemove}"
136 rm -r ${backupMainDir}/${dirToRemove}
137 echo "Done"
138 echo
139 done
140 fi
141 fi
142
143 echo
144 echo "DONE!"
145 echo "Backup created: ${backupdir}"

patrick-canterino.de