Skip to content
This repository has been archived by the owner on Sep 18, 2022. It is now read-only.

Commit

Permalink
new option -p|--snap adds support for nextcloud-snap
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieO committed Apr 24, 2018
1 parent cf71c19 commit 2af9fbe
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ Paths (FILE / DIRECTORY) are absolute paths or relative paths to working directo
-o | --output DIRECTORY
Use directory DIRECTORY to store backups.
If this option is not given, folder 'backups/' in script's directory is created and used.
-p | --snap
Use this option, if you are running nextcloud-snap (https://github.com/nextcloud/nextcloud-snap).
calcardbackup will then use the in the snap package included cli utility nextcloud.mysql-client
to read the needed values from the database. Note that in order for this to work, calcardbackup has
to be run with sudo. Running as root will fail.
-r | --remove N
Remove backups older than N days from backup folder (N needs to be a positive integer).
-s | --selfsigned
Expand All @@ -101,6 +106,11 @@ This option may be used as follows to keep passwords of the main users secret:
__Benefit of this approach:__ if the file `users.txt` gets in wrong hands, only this new user account is being compromised.
__Drawback:__ no automatic inclusion of newly created addressbooks/calendars. Items will not be backed up unless being shared with that new user account.

## nextcloud-snap users

If you are running Nextcloud-snap (https://github.com/nextcloud/nextcloud-snap), you have to use option `-p|--snap` to tell calcardbackup to use the cli utility `nextcloud.mysql-client` from the snap package.
In order for this to work, calcardbackup has to be run with sudo (running as root will fail). As path to Nextcloud use the path to the configuration files of nextcloud. In a standard installation this would be `/var/snap/nextcloud/current/nextcloud`. See example no.6 below.

## Usage examples

1. `./calcardbackup /var/www/nextcloud -s -na -i -x`
Expand All @@ -118,6 +128,9 @@ Supress output except for path to the backup (`-b`), use extension .DD.HH (`-d .
5. `./calcardbackup`
Use files calcardbackup.conf and users.txt in the script's directory as configuration file and login information.

6. `sudo ./calcardbackup /var/snap/nextcloud/current/nextcloud -p`
This example is for nextcloud-snap users. calcardbackup will use the cli utility from nextcloud-snap to access the database.

## Considerations about encryption

If you want to use the included encryption possibility, be aware that:
Expand Down
31 changes: 26 additions & 5 deletions calcardbackup
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#######################################################

# calcardbackup version:
version="0.4.1 (06.03.2018), AGPL-3.0"
version="0.4.1-1 (24.04.2018), AGPL-3.0"

###
### BEGIN: FUNCTION DEFINITIONS
Expand Down Expand Up @@ -85,6 +85,7 @@ load_default_values() {
backup_addressbooks="yes"
backup_calendars="yes"
include_shares="no"
snap="no"
}

read_config_file() {
Expand Down Expand Up @@ -224,6 +225,13 @@ preparations() {
[[ -n "${config_file:-}" ]] && error_exit "ERROR: Passphrase is on insecure example value (1234)." "Change 'gpg_passphrase' in configuration file and run sript again."
fi
fi

# check $snap for valid value (could only be invalid when using config-file):
if [[ ! "${snap}" =~ ^(yes|no)$ ]]; then
_output echo "-- WARNING: Parameter 'snap' is not valid (only \"yes\" or \"no\" allowed)."
_output echo "-- WARNING: Using default value instead: snap=\"no\""
snap="no"
fi
}

getvalue_from_configphp() {
Expand Down Expand Up @@ -457,7 +465,14 @@ get_database_details() {
# mysql>=5.6 throws a warning when using password on command line interface: https://bugs.mysql.com/bug.php?id=66546
# avoiding that with option --defaults-extra-file and printf as suggested by Dave James Miller: https://stackoverflow.com/questions/20751352/suppress-warning-messages-using-mysql-from-within-terminal-but-password-written/20854048#comment42372603_22933056
# ATTENTION: variable $dbcommand will be passed to command "eval" later. So better take care when adding new stuff to $dbcommand!
dbcommand='mysql --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -se '
case "${snap}" in
no )
dbcommand='mysql --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -se '
;;
yes )
dbcommand='nextcloud.mysql-client --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -se '
;;
esac
# store type of database for output of script:
database="MySQL/MariaDB"
}
Expand Down Expand Up @@ -532,7 +547,8 @@ query_database() {
cd "${datadirectory}"

# check if table exists:
[[ "${dbtype}" == "mysql" ]] && check_table="$(mysql --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -e "show tables like '${table}'")"
[[ "${dbtype}" == "mysql" && "${snap}" == "no" ]] && check_table="$(mysql --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -e "show tables like '${table}'")"
[[ "${dbtype}" == "mysql" && "${snap}" == "yes" ]] && check_table="$(nextcloud.mysql-client --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -e "show tables like '${table}'")"
[[ "${dbtype}" == "sqlite3" ]] && check_table="$(sqlite3 owncloud.db "SELECT name FROM sqlite_master WHERE type='table' AND name='${table}'")"
[[ "${dbtype}" == "pgsql" ]] && check_table="$(PGPASSWORD="${dbpassword}" psql "${dbhost}" "${dbprotocol}" -U "${dbuser}" -d "${dbname}" -qtc "SELECT * FROM ${table}" 2>/dev/null)"

Expand Down Expand Up @@ -587,7 +603,8 @@ include_shares() {
read_shared=1

# check if table $table_shares exists:
[[ "${dbtype}" == "mysql" ]] && check_table="$(mysql --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -e "show tables like '${table_shares}'")"
[[ "${dbtype}" == "mysql" && "${snap}" == "no" ]] && check_table="$(mysql --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -e "show tables like '${table_shares}'")"
[[ "${dbtype}" == "mysql" && "${snap}" == "yes" ]] && check_table="$(nextcloud.mysql-client --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -e "show tables like '${table_shares}'")"
[[ "${dbtype}" == "sqlite3" ]] && check_table="$(sqlite3 owncloud.db "SELECT name FROM sqlite_master WHERE type='table' AND name='${table_shares}'")"
[[ "${dbtype}" == "pgsql" ]] && check_table="$(PGPASSWORD="${dbpassword}" psql "${dbhost}" "${dbprotocol}" -U "${dbuser}" -d "${dbname}" -qtc "SELECT * FROM ${table_shares}" 2>/dev/null)"

Expand Down Expand Up @@ -628,7 +645,8 @@ include_shares() {

# we also need to get info about group members to be able to backup calendars/addressbooks which are shared to groups:
# check if table group_user ($table_group_user) exists:
[[ "${dbtype}" == "mysql" ]] && check_table="$(mysql --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -e "show tables like '${table_group_user}'")"
[[ "${dbtype}" == "mysql" && "${snap}" == "no" ]] && check_table="$(mysql --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -e "show tables like '${table_group_user}'")"
[[ "${dbtype}" == "mysql" && "${snap}" == "yes" ]] && check_table="$(nextcloud.mysql-client --defaults-extra-file=<(printf "[client]\nuser = %s\npassword = %s\nhost = %s\n%s" "${dbuser}" "${dbpassword}" "${dbhost}" "${dbprotocol}") "${dbname}" -e "show tables like '${table_group_user}'")"
[[ "${dbtype}" == "sqlite3" ]] && check_table="$(sqlite3 owncloud.db "SELECT name FROM sqlite_master WHERE type='table' AND name='${table_group_user}'")"
[[ "${dbtype}" == "pgsql" ]] && check_table="$(PGPASSWORD="${dbpassword}" psql "${dbhost}" "${dbprotocol}" -U "${dbuser}" -d "${dbname}" -qtc "SELECT * FROM ${table_group_user}" 2>/dev/null)"

Expand Down Expand Up @@ -1069,6 +1087,9 @@ do
check_argument "${1:-}"
backupfolder="${1}"
;;
-p | --snap )
snap="yes"
;;
-r | --remove )
shift
check_argument "${1:-}"
Expand Down
11 changes: 11 additions & 0 deletions examples/calcardbackup.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,15 @@ backup_calendars="yes"
include_shares="no"


# if you are running nextcloud-snap (https://github.com/nextcloud/nextcloud-snap),
# you need to change this value to "yes" and use as path to nextcloud
# the path to the nextcloud configuration files. If you haven't changed anything, this
# will be: /var/snap/nextcloud/current/nextcloud/
# Note that calcardbackup needs to be run with sudo when used with snap installations.
# See example No.6 in README.md
# correlates to option -p|--snap

snap="no"


######## end of config file for calcardbackup ########

1 comment on commit 2af9fbe

@BernieO
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support nextcloud-snap: https://github.com/nextcloud/nextcloud-snap
Fix for #6

Please sign in to comment.