Wiki source code of Automate a Backup with a Shell Script
Last modified by Eleni Cojocariu-testing account on 2026/08/21 15:47
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | This example turns [[Back Up an XWiki Instance>>doc:documentation.xs.admin.backup.backup-instance.WebHome]] into a single script a scheduler runs every night, for an instance served by Tomcat on a MariaDB database, and it stops the wiki for as long as the copy takes: | ||
| 2 | |||
| 3 | 1. Store the database credentials in the ##.my.cnf## file of the account that will run the script, so that no password reaches the command line and the process list:((( | ||
| 4 | {{code language="none"}} | ||
| 5 | [client] | ||
| 6 | user=xwiki | ||
| 7 | password=the password from hibernate.cfg.xml | ||
| 8 | {{/code}} | ||
| 9 | ))) | ||
| 10 | 1. Save the script as ##/usr/local/bin/xwiki-backup##:((( | ||
| 11 | {{code language="bash"}} | ||
| 12 | #!/bin/bash | ||
| 13 | set -euo pipefail | ||
| 14 | |||
| 15 | # Adapt these five values to your own installation. | ||
| 16 | DATABASE=xwiki | ||
| 17 | WEBAPP=/usr/lib/xwiki | ||
| 18 | PERMANENT=/var/lib/xwiki/data | ||
| 19 | DESTINATION=/srv/backup/xwiki | ||
| 20 | SERVICE=tomcat10 | ||
| 21 | |||
| 22 | TARGET="$DESTINATION/$(date '+%Y-%m-%d')" | ||
| 23 | mkdir -p "$TARGET" | ||
| 24 | |||
| 25 | echo "Stopping $SERVICE" | ||
| 26 | systemctl stop "$SERVICE" | ||
| 27 | |||
| 28 | echo "Dumping the database" | ||
| 29 | mysqldump --add-drop-database --databases "$DATABASE" | gzip > "$TARGET/database.sql.gz" | ||
| 30 | |||
| 31 | echo "Copying the permanent directory, without its cache" | ||
| 32 | tar -C "$(dirname "$PERMANENT")" -czf "$TARGET/permanent-directory.tar.gz" \ | ||
| 33 | --exclude=cache "$(basename "$PERMANENT")" | ||
| 34 | |||
| 35 | echo "Copying the web application, configuration files and added jars included" | ||
| 36 | tar -C "$(dirname "$WEBAPP")" -czf "$TARGET/webapp.tar.gz" "$(basename "$WEBAPP")" | ||
| 37 | |||
| 38 | echo "Starting $SERVICE" | ||
| 39 | systemctl start "$SERVICE" | ||
| 40 | |||
| 41 | echo "Backup written to $TARGET" | ||
| 42 | {{/code}} | ||
| 43 | ))) | ||
| 44 | 1. Make the script executable:((( | ||
| 45 | {{code language="bash"}} | ||
| 46 | chmod +x /usr/local/bin/xwiki-backup | ||
| 47 | {{/code}} | ||
| 48 | ))) | ||
| 49 | 1. Run it once by hand and read what it prints, since a failing step stops it before it can report a backup it did not take. | ||
| 50 | 1. Schedule it, for example every night at 3 o'clock, from the crontab of the account that owns the ##.my.cnf## file:((( | ||
| 51 | {{code language="none"}} | ||
| 52 | 0 3 * * * /usr/local/bin/xwiki-backup | ||
| 53 | {{/code}} | ||
| 54 | ))) | ||
| 55 | 1. Confirm the next morning that the night's run produced its own dated directory:((( | ||
| 56 | {{code language="none"}} | ||
| 57 | $ ls -1 /srv/backup/xwiki/2026-08-19/ | ||
| 58 | database.sql.gz | ||
| 59 | permanent-directory.tar.gz | ||
| 60 | webapp.tar.gz | ||
| 61 | {{/code}} | ||
| 62 | ))) |