Skip to content

Commit 36a5f70

Browse files
committed
backup: improve reliability and documentation
* allow starting backup from any folder * automatically run using sudo (to never fail due to ./backup owned by root) * run post_backup.sh after 'docker-compose start', to minimize downtime when e.g. uploading backups to a remote server * chown all of ./backup to the appropriate user Fixes #566
1 parent ff246ca commit 36a5f70

File tree

4 files changed

+171
-178
lines changed

4 files changed

+171
-178
lines changed

docs/Backups.md.old

Lines changed: 0 additions & 113 deletions
This file was deleted.

docs/Basic_setup/Backup-and-Restore.md

Lines changed: 144 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,64 @@ This page explains how to use the backup and restore functionality of IOTstack.
44
## Backup
55
The backup command can be executed from IOTstack's menu, or from a cronjob.
66

7-
### Running backup
8-
To ensure that all your data is saved correctly, the stack should be brought down. This is mainly due to databases potentially being in a state that could cause data loss.
7+
To ensure that all your data is saved correctly, the stack has to be stopped.
8+
This is mainly due to databases potentially being in a state that could cause
9+
data loss.
910

1011
There are 2 ways to run backups:
1112

1213
* From the menu: `Backup and Restore` > `Run backup`
1314
* Running the following command: `bash ./scripts/backup.sh`
1415

15-
The command that's run from the command line can also be executed from a cronjob:
16+
The command that's run from the command line can also be executed from crontab:
1617

17-
```0 2 * * * cd /home/pi/IOTstack && /bin/bash ./scripts/backup.sh```
18+
```
19+
0 2 * * * cd /home/pi/IOTstack && /bin/bash ./scripts/backup.sh
20+
```
1821

1922
The current directory of bash must be in IOTstack's directory, to ensure that it can find the relative paths of the files it's meant to back up. In the example above, it's assume that it's inside the `pi` user's home directory.
2023

21-
### Arguments
22-
```
23-
./scripts/backup.sh {TYPE=3} {USER=$(whoami)}
24+
**Usage:**
25+
``` console
26+
$ ~/IOTstack/scripts/backup.sh {TYPE}
2427
```
2528

26-
* Types:
27-
* 1 = Backup with Date
28-
* A tarball file will be created that contains the date and time the backup was started, in the filename.
29-
* 2 = Rolling Date
30-
* A tarball file will be created that contains the day of the week (0-6) the backup was started, in the filename.
31-
* If a tarball already exists with the same name, it will be overwritten.
32-
* 3 = Both
33-
* User:
34-
This parameter only becomes active if run as root. This script will default to the current logged in user
35-
If this parameter is not supplied when run as root, the script will ask for the username as input
29+
**Types:**
30+
31+
1. Backup with Date. Backup to filename with the date and time the backup was
32+
started.
33+
2. Rolling Date. Filename will based on the day of the week (0-6) the backup
34+
was started. New backups will overwrite old ones.
35+
3. Both
36+
37+
You can find the backups in the `~/IOTstack/backups/` folder. With rolling
38+
being in `~/IOTstack/backups/rolling/` and date backups in
39+
`~/IOTstack/backups/backup/`. Log files can also be found in the logs/
40+
directory.
3641

37-
Backups:
42+
**Examples:**
3843

39-
* You can find the backups in the ./backups/ folder. With rolling being in ./backups/rolling/ and date backups in ./backups/backup/
40-
* Log files can also be found in the ./backups/logs/ directory.
44+
- Regular date & timestamped backup into ~/IOTstack/backup/backups:
4145

42-
### Examples:
46+
`~/IOTstack/scripts/backup.sh 1`
4347

44-
* `./scripts/backup.sh`
45-
* `./scripts/backup.sh 3`
48+
- Either these commands will produce backups of both types:
4649

47-
Either of these will run both backups.
50+
`~/IOTstack/scripts/backup.sh` or<br />
51+
`~/IOTstack/scripts/backup.sh 3`
4852

49-
* `./scripts/backup.sh 2`
5053

51-
This will only produce a backup in the rollowing folder. It will be called 'backup_XX.tar.gz' where XX is the current day of the week (as an int)
54+
- Produce a backup into ~/IOTstack/backup/rolling/. It will be called
55+
'backup_XX.tar.gz' where XX is the current day of the week (as an int):
5256

53-
* `sudo bash ./scripts/backup.sh 2 pi`
57+
`~/IOTstack/scripts/backup.sh 2`
5458

55-
This will only produce a backup in the rollowing folder and change all the permissions to the 'pi' user.
59+
- (expert use) Usually the backup should be executed without sudo using your
60+
regular user, as this will automatically produce backups with correct
61+
permissions. This will only produce a backup in the rolling folder and
62+
change all the permissions to the 'pi' user:
63+
64+
`sudo bash ~/IOTstack/scripts/backup.sh 2 pi`
5665

5766
## Restore
5867
There are 2 ways to run a restore:
@@ -64,7 +73,7 @@ There are 2 ways to run a restore:
6473

6574
*Note*: It is suggested that you test that your backups can be restored after initially setting up, and anytime you add or remove a service. Major updates to services can also break backups.
6675

67-
### Arguments
76+
**Usage:**
6877
```
6978
./scripts/restore.sh {FILENAME=backup.tar.gz} {noask}
7079
```
@@ -76,35 +85,126 @@ The restore script takes 2 arguments:
7685
## Pre and post script hooks
7786
The script checks if there are any pre and post back up hooks to execute commands. Both of these files will be included in the backup, and have also been added to the `.gitignore` file, so that they will not be touched when IOTstack updates.
7887

79-
### Prebackup script hook
80-
The prebackup hook script is executed before any compression happens and before anything is written to the temporary backup manifest file (`./.tmp/backup-list_{{NAME}}.txt`). It can be used to prepare any services (such as databases that IOTstack isn't aware of) for backing up.
88+
Both of these scripts will be run as root.
89+
90+
### Pre-backup script hook
91+
The pre-backup hook script is executed before any compression happens and before anything is written to the temporary backup manifest file (`./.tmp/backup-list_{{NAME}}.txt`). It can be used to prepare any services (such as databases that IOTstack isn't aware of) for backing up.
8192

82-
To use it, simple create a `./pre_backup.sh` file in IOTstack's main directory. It will be executed next time a backup runs.
93+
To use it, simple create the `~/IOTstack/pre_backup.sh` file. It will be executed next time a backup runs.
8394

84-
### Postbackup script hook
85-
The postbackup hook script is executed after the tarball file has been written to disk, and before the final backup log information is written to disk.
95+
### Post-backup script hook { #post-backup }
96+
The post-backup hook script is executed after the backup tarball file has been
97+
written to disk and the stack has been started back up. Any output will be
98+
included in the backup log file.
8699

87-
To use it, simple create a `./post_backup.sh` file in IOTstack's main directory. It will be executed after the next time a backup runs.
100+
To use it, simple create the `~/IOTstack/post_backup.sh` file. It will be
101+
executed after the next time a backup runs. It will be provided the backup
102+
.tar.gz as its first argument.
103+
104+
This is useful for triggering transfer of the backup to a cloud or another
105+
server, see below for possible third party integrations as to what is possible.
88106

89107
### Post restore script hook
90108
The post restore hook script is executed after all files have been extracted and written to disk. It can be used to apply permissions that your custom services may require.
91109

92110
To use it, simple create a `./post_restore.sh` file in IOTstack's main directory. It will be executed after a restore happens.
93111

94112
## Third party integration
95-
This section explains how to backup your files with 3rd party software.
113+
This section explains how you could backup your files to different
114+
integrations. Actual initiation of the transfer of the backup has to be done
115+
using the [post backup](#post-backup) hook described above.
116+
117+
### IOTstackBackup
118+
119+
[IOTstackBackup](https://github.com/Paraphraser/IOTstackBackup) is a project
120+
aiming to provide a sophisticated all-in-on solution: on-line backups of
121+
databases and configurable transfers to remotes without writing your own
122+
scripts.
96123

97124
### Dropbox
98-
Coming soon.
125+
Dropbox-Uploader is a great utility to easily upload data from your Pi to the cloud. https://magpi.raspberrypi.org/articles/dropbox-raspberry-pi. It can be installed from the Menu under Backups.
126+
127+
#### Troubleshoot: Token added incorrectly or install aborted at the token screen
128+
129+
Run `~/Dropbox-Uploader/dropbox_uploader.sh unlink` and if you have added it key then it will prompt you to confirm its removal. If no key was found it will ask you for a new key.
130+
131+
Confirm by running `~/Dropbox-Uploader/dropbox_uploader.sh` it should ask you for your key if you removed it or show you the following prompt if it has the key:
132+
133+
``` console
134+
$ ~/Dropbox-Uploader/dropbox_uploader.sh
135+
Dropbox Uploader v1.0
136+
Andrea Fabrizi - [email protected]
137+
138+
Usage: /home/pi/Dropbox-Uploader/dropbox_uploader.sh [PARAMETERS] COMMAND...
139+
140+
Commands:
141+
upload <LOCAL_FILE/DIR ...> <REMOTE_FILE/DIR>
142+
download <REMOTE_FILE/DIR> [LOCAL_FILE/DIR]
143+
delete <REMOTE_FILE/DIR>
144+
move <REMOTE_FILE/DIR> <REMOTE_FILE/DIR>
145+
copy <REMOTE_FILE/DIR> <REMOTE_FILE/DIR>
146+
mkdir <REMOTE_DIR>
147+
....
148+
149+
```
150+
151+
Ensure you **are not** running as sudo as this will store your api in the /root directory as `/root/.dropbox_uploader`
152+
153+
If you ran the command with sudo the remove the old token file if it exists with either `sudo rm /root/.dropbox_uploader` or `sudo ~/Dropbox-Uploader/dropbox_uploader.sh unlink`
154+
155+
99156

100157
### Google Drive
101-
Coming soon.
158+
rclone is a program uploading to Google Drive. Install it from the menu then
159+
see [here](
160+
https://medium.com/@artur.klauser/mounting-google-drive-on-raspberry-pi-f5002c7095c2)
161+
for these sections:
162+
163+
* Getting a Google Drive Client ID
164+
* Setting up the Rclone Configuration
165+
166+
Note: When naming the service in `rclone config` ensure to call it "gdrive"
167+
168+
**The Auto-mounting instructions for the drive in the link don't work on Rasbian**. Auto-mounting of the drive isn't necessary for the backup script.
102169

103-
### rsync
104-
Coming soon.
170+
If you want your Google Drive to mount on every boot then follow the instructions at the bottom of the wiki page
105171

106-
### Duplicati
107-
Coming soon.
172+
#### Auto-mounting Google Drive
108173

109-
### SFTP
110-
Coming soon.
174+
To enable rclone to mount on boot you will need to make a user service. Run the following commands
175+
176+
``` console
177+
$ mkdir -p ~/.config/systemd/user
178+
$ nano ~/.config/systemd/user/gdrive.service
179+
```
180+
Copy the following code into the editor, save and exit
181+
182+
```
183+
[Unit]
184+
Description=rclone: Remote FUSE filesystem for cloud storage
185+
Documentation=man:rclone(1)
186+
187+
[Service]
188+
Type=notify
189+
ExecStartPre=/bin/mkdir -p %h/mnt/gdrive
190+
ExecStart= \
191+
/usr/bin/rclone mount \
192+
--fast-list \
193+
--vfs-cache-mode writes \
194+
gdrive: %h/mnt/gdrive
195+
196+
[Install]
197+
WantedBy=default.target
198+
```
199+
enable it to start on boot with: (no sudo)
200+
``` console
201+
$ systemctl --user enable gdrive.service
202+
```
203+
start with
204+
``` console
205+
$ systemctl --user start gdrive.service
206+
```
207+
if you no longer want it to start on boot then type:
208+
``` console
209+
$ systemctl --user disable gdrive.service
210+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ extra_javascript:
6161
- javascript/fix-codeblock-console-copy-button.js
6262

6363
markdown_extensions:
64+
- attr_list
6465
- pymdownx.highlight:
6566
pygments_lang_class: true
6667
- admonition

0 commit comments

Comments
 (0)