Skip to content

Commit 2012aef

Browse files
committed
fix(tuto): fix script duplicity
1 parent 4bbc984 commit 2012aef

File tree

1 file changed

+66
-93
lines changed
  • tutorials/backup-dedicated-server-s3-duplicity

1 file changed

+66
-93
lines changed

tutorials/backup-dedicated-server-s3-duplicity/index.mdx

Lines changed: 66 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,13 @@ In this step, we install the various software needed. As well as installing Dupl
5252
Run the following command to update the APT package manager, upgrade the software already installed on the server and download and install Duplicity and the other required software:
5353

5454
```bash
55-
apt update && apt upgrade
56-
apt install -y python3-boto python3-pip haveged gettext librsync-dev
57-
wget https://gitlab.com/duplicity/duplicity/-/archive/rel.2.1.2/duplicity-rel.2.1.2.tar.gz
58-
tar xaf duplicity-rel.2.1.2.tar.gz
59-
cd duplicity-rel.2.1.2/
60-
pip3 install -r requirements.txt
61-
python3 setup.py install
55+
sudo apt update && sudo apt upgrade -y
56+
sudo apt install -y python3-pip gnupg2 haveged
57+
pip3 install duplicity[boto3]
6258
```
6359

60+
The command above installs Duplicity with modern `boto3` support (required for newer AWS-compatible services).
61+
6462
## Creating a GPG key
6563

6664
1. Generate the GPG key with the following command.
@@ -164,29 +162,21 @@ As everything is installed and ready, we will now configure and script our inter
164162
mkdir -p /var/log/duplicity
165163
touch /var/log/duplicity/logfile{.log,-recent.log}
166164
```
167-
2. Add the following lines to `.scw-configrc`. Make sure you replace the necessary values with the details of your Scaleway API key, Object Storage bucket, and GPG key. You also need to enter a path to the desired backup folder:
165+
2. Create a config file `~/.scw-backup.env`. Make sure you replace the necessary values with the details of your Scaleway API key, Object Storage bucket, and GPG key. You also need to enter a path to the desired backup folder:
168166
```bash
169167
# Scaleway credentials keys
170168
export AWS_ACCESS_KEY_ID="<SCALEWAY ACCESS KEY>"
171-
export AWS_SECRET_ACCESS_KEY="<SCALEWAY SECRET ACCESS KEY>"
169+
export AWS_SECRET_ACCESS_KEY="<SCALEWAY SECRET KEY>"
172170

173-
export SCW_REGION="<REGION OF YOUR BUCKET>"
171+
export SCW_REGION="<REGION OF YOUR BUCKET>s"
174172
export SCW_ENDPOINT_URL="https://s3.${SCW_REGION}.scw.cloud"
175-
176-
# set SCW_BUCKET as follows for duplicity < 0.8.23
177-
# for higher versions, see below
178-
# export SCW_BUCKET="s3://s3.${SCW_REGION}.scw.cloud/<NAME OF YOUR BUCKET>"
179-
180-
# set the next two variables for duplicity >= 0.8.23
181-
# it uses the boto3 library, which uses a different naming scheme for bucket names
182-
export SCW_BUCKET="s3://<NAME OF YOUR BUCKET>"
183-
173+
export SCW_BUCKET="s3://<YOUR BUCKET NAME>"
184174
# GPG Key information
185-
export PASSPHRASE="<YOUR GPG KEY PASSPHRASE>"
186-
export GPG_FINGERPRINT="<YOUR GPG KEY FINGERPRINT>"
175+
export GPG_FINGERPRINT="<YOUR GPG KEY PASSPHRASE>"
176+
export PASSPHRASE="<YOUR GPG KEY FINGERPRINT>"
187177

188178
# Folder to backup
189-
export SOURCE="<PATH TO FOLDER TO BACKUP>"
179+
export SOURCE="/path/to/backup"
190180

191181
# Will keep backup up to 1 month
192182
export KEEP_BACKUP_TIME="1M"
@@ -197,13 +187,6 @@ As everything is installed and ready, we will now configure and script our inter
197187
# Log files
198188
export LOGFILE_RECENT="/var/log/duplicity/logfile-recent.log"
199189
export LOGFILE="/var/log/duplicity/logfile.log"
200-
201-
log () {
202-
date=`date +%Y-%m-%d`
203-
hour=`date +%H:%M:%S`
204-
echo "$date $hour $*" >> ${LOGFILE_RECENT}
205-
}
206-
export -f log
207190
```
208191
209192
The backup policy described here makes a full backup every 10 days and removes all backups older than one month.
@@ -214,41 +197,39 @@ Using the configuration and Duplicity, we automatize the backup process with the
214197
215198
1. Copy the following script to `scw-backups.sh`:
216199
```bash
217-
#!/bin/bash
218-
source <FULL PATH TO>/.scw-configrc
219-
220-
currently_backuping=$(ps -ef | grep duplicity | grep python | wc -l)
221-
222-
if [ $currently_backuping -eq 0 ]; then
223-
# Clear the recent log file
224-
cat /dev/null > ${LOGFILE_RECENT}
225-
226-
log ">>> removing old backups"
227-
duplicity remove-older-than \
228-
--s3-endpoint-url ${SCW_ENDPOINT_URL} \
229-
--s3-region-name ${SCW_REGION} \
230-
${KEEP_BACKUP_TIME} ${SCW_BUCKET} >> ${LOGFILE_RECENT} 2>&1
231-
232-
# duplicity >= 0.8.23
233-
# determine S3_ENDPOINT_URL for scaleway
234-
S3_ENDPOINT_URL="https://s3.${S3_REGION_NAME}.scw.cloud"
235-
236-
log ">>> creating and uploading backup to Scaleway Glacier"
237-
duplicity \
238-
incr --full-if-older-than ${FULL_BACKUP_TIME} \
239-
--asynchronous-upload \
240-
--s3-use-glacier \
241-
--s3-endpoint-url ${SCW_ENDPOINT_URL} \
242-
--s3-region-name ${SCW_REGION} \
243-
--encrypt-key=${GPG_FINGERPRINT} \
244-
--sign-key=${GPG_FINGERPRINT} \
245-
${SOURCE} ${SCW_BUCKET} >> ${LOGFILE_RECENT} 2>&1
246-
247-
cat ${LOGFILE_RECENT} >> ${LOGFILE}
248-
fi
200+
#!/bin/bash
201+
source ~/.scw-backup.env
202+
203+
mkdir -p /var/log/duplicity
204+
touch "$LOGFILE_RECENT" "$LOGFILE"
205+
206+
currently_backuping=$(pgrep -f duplicity | wc -l)
207+
208+
if [ "$currently_backuping" -eq 0 ]; then
209+
echo "$(date '+%F %T') >>> Removing old backups" >> "$LOGFILE_RECENT"
210+
duplicity remove-older-than "$KEEP_BACKUP_TIME" "$SCW_BUCKET" \
211+
--s3-endpoint-url "$SCW_ENDPOINT_URL" \
212+
--s3-region-name "$SCW_REGION" >> "$LOGFILE_RECENT" 2>&1
213+
214+
echo "$(date '+%F %T') >>> Running backup" >> "$LOGFILE_RECENT"
215+
duplicity \
216+
incr --full-if-older-than "$FULL_BACKUP_TIME" \
217+
--asynchronous-upload \
218+
--encrypt-key="$GPG_FINGERPRINT" \
219+
--sign-key="$GPG_FINGERPRINT" \
220+
--s3-endpoint-url "$SCW_ENDPOINT_URL" \
221+
--s3-region-name "$SCW_REGION" \
222+
"$SOURCE" "$SCW_BUCKET" >> "$LOGFILE_RECENT" 2>&1
223+
224+
cat "$LOGFILE_RECENT" >> "$LOGFILE"
225+
fi
249226
```
250227
251-
2. Run the script `./scw-backups.sh` to make sure the configuration is correctly set. Check the Object Storage bucket on the [Scaleway console](https://console.scaleway.com) to see the backup files, or examine the logs with the following command:
228+
2. Make the script executable:
229+
```bash
230+
chmod +x ~/scw-backup.sh
231+
```
232+
3. Run the script `./scw-backups.sh` to make sure the configuration is correctly set. Check the Object Storage bucket on the [Scaleway console](https://console.scaleway.com) to see the backup files, or examine the logs with the following command:
252233
```bash
253234
cat /var/log/duplicity/logfile-recent.log
254235
```
@@ -260,39 +241,31 @@ Duplicity also allows you to recover a backup. We will create a script to make t
260241
1. Add the following script to `scw-restore.sh`:
261242
```bash
262243
#!/bin/bash
263-
source <FULL PATH TO>/.scw-configrc
264-
265-
if [ $# -lt 2 ]; then
266-
echo -e "Usage $0 <time or delta> [file to restore] <restore to>
267-
Exemple:
268-
\t$ $0 2018-7-21 recovery/ ## recovers * from closest backup to date
269-
\t$ $0 0D secret data/ ## recovers most recent file nammed 'secret'";
270-
exit; fi
271-
272-
if [ $# -eq 2 ]; then
273-
duplicity \
274-
--s3-endpoint-url ${SCW_ENDPOINT_URL} \
275-
--s3-region-name ${SCW_REGION} \
276-
--time $1 \
277-
${SCW_BUCKET} $2
278-
fi
279-
280-
if [ $# -eq 3 ]; then
281-
duplicity \
282-
--s3-endpoint-url ${SCW_ENDPOINT_URL} \
283-
--s3-region-name ${SCW_REGION} \
284-
--time $1 \
285-
--file-to-restore $2 \
286-
${SCW_BUCKET} $3
287-
fi
288-
```
289-
2. Recover the data you uploaded in the previous section with the following command:
290-
```bash
291-
./scw-restore.sh 0D /tmp/backup-recovery-test/
244+
source ~/.scw-backup.env
245+
246+
if [ $# -lt 2 ]; then
247+
echo "Usage: $0 <time or delta> [file to restore] <restore dir>"
248+
exit 1
249+
fi
250+
251+
if [ $# -eq 2 ]; then
252+
duplicity \
253+
--s3-endpoint-url "$SCW_ENDPOINT_URL" \
254+
--s3-region-name "$SCW_REGION" \
255+
--time "$1" \
256+
"$SCW_BUCKET" "$2"
257+
elif [ $# -eq 3 ]; then
258+
duplicity \
259+
--s3-endpoint-url "$SCW_ENDPOINT_URL" \
260+
--s3-region-name "$SCW_REGION" \
261+
--time "$1" \
262+
--file-to-restore "$2" \
263+
"$SCW_BUCKET" "$3"
264+
fi
292265
```
293-
3. Alternatively, recover one specific file with the following format from a backup 5 days ago with:
266+
2. Make the script executable:
294267
```bash
295-
./scw-restore.sh 5D <file> /tmp/backup-recovery-test/
268+
chmod +x ~/scw-restore.sh
296269
```
297270
298271
### Automation of the backups

0 commit comments

Comments
 (0)