Difference between revisions of "Automatic backup script"

From Bisq Wiki
Jump to navigation Jump to search
(Consolidated most common text, added Bisq2 on linux)
Line 6: Line 6:
  
 
__TOC__
 
__TOC__
 +
 +
= Common requirements and backup strategy =
 +
 +
All scripts require 7z compression (7-zip on Windows, p7zip-full on Linux, p7zip on MacOS).
 +
 +
The code will backup all functional files in the data directory, leaving out non-unique files that the Bisq application will fetch/re-create autonomously, and compress them into a 7zip archive, keeping the number of last backups that you define, while deleting the oldest one(s) if the existing number of backups is higher.
 +
 +
It is advised that you direct the backups to either an external drive, or a NAS, or an encrypted cloud storage.
 +
 +
Depending on how bulletproof of a backup schedule you want, you can increase the number of backups to keep on disk to 10 or more (the size of each depends on the history of your instance, but it usually resides in a few tens of MB, often less) and the frequency the script runs with, for example every 1 or 2 hours.
  
 
= Bisq1 =
 
= Bisq1 =
  
 
== Linux ==
 
== Linux ==
 
This script requires <code>p7zip-full</code> to be installed, use <code>sudo apt install p7zip-full</code> in debian derivatives if needed.
 
  
 
Create a <code>scripts</code> folder in your <code>home</code> directory, prepare an executable file and open it with an editor:
 
Create a <code>scripts</code> folder in your <code>home</code> directory, prepare an executable file and open it with an editor:
Line 24: Line 32:
 
</pre>
 
</pre>
  
the following code, that you need to paste in the file and edit where needed, will backup all functional files in the data directory, leaving out non-unique files that the Bisq application will fetch autonomously, and compress them into a 7zip archive, keeping the last N amount of backups that you define (default is 4), while deleting the oldest one if the existing number of backups is higher.
+
paste the following code in the file, and edit where needed:
 
 
It is advised that you direct the backups to either an external drive, or a NAS, or an encrypted cloud storage.
 
  
 
<pre>
 
<pre>
 
#!/bin/bash
 
#!/bin/bash
  
 +
# EDIT THE FOLLOWING
 
SOURCE_DIR="/home/YOURUSERNAME/local/share/Bisq/btc_mainnet"
 
SOURCE_DIR="/home/YOURUSERNAME/local/share/Bisq/btc_mainnet"
 
TARGET_FOLDER="/mnt/BACKUPS/BISQ1"
 
TARGET_FOLDER="/mnt/BACKUPS/BISQ1"
 
BACKUP_COUNT=4
 
BACKUP_COUNT=4
 +
# ##
 +
 
ARCHIVE_ROOT="bisq1_backup_"
 
ARCHIVE_ROOT="bisq1_backup_"
 
BACKUP_FILE="${ARCHIVE_ROOT}$(date +%Y%m%d_%H%M%S).7z"
 
BACKUP_FILE="${ARCHIVE_ROOT}$(date +%Y%m%d_%H%M%S).7z"
Line 108: Line 117:
 
<code>0 */8 * * * /home/YOURUSERNAME/scripts/bisq1backup > /tmp/bisq1backup.log</code>
 
<code>0 */8 * * * /home/YOURUSERNAME/scripts/bisq1backup > /tmp/bisq1backup.log</code>
  
depending on how bulletproof of a backup strategy you want, you can increase the number of backups to keep on disk (the size of each depends on the history of your instance, but it usually resides in less than a few tens of MB) and the frequency the script runs, for example every 1 or 2 hours.
+
 
 +
= Bisq2 =
 +
 
 +
== Linux ==
 +
 
 +
Create a <code>scripts</code> folder in your <code>home</code> directory, prepare an executable file and open it with an editor:
 +
 
 +
<pre>
 +
cd ~
 +
mkdir -p scripts
 +
cd scripts
 +
touch bisq2backup
 +
chmod +x bisq2backup
 +
nano bisq2backup
 +
</pre>
 +
 
 +
paste the following code in the file, and edit where needed:
 +
 
 +
<pre>
 +
#!/bin/bash
 +
 
 +
# EDIT THE FOLLOWING
 +
FOLDER_TO_BACKUP="/home/YOURUSERNAME/local/share/Bisq2/db"
 +
TARGET_FOLDER="/mnt/BACKUPS/BISQ2"
 +
BACKUP_COUNT=4
 +
# ##
 +
 
 +
ARCHIVE_ROOT="bisq2_backup_"
 +
ARCHIVE_NAME="${ARCHIVE_ROOT}$(date +%Y%m%d_%H%M%S).7z"
 +
ARCHIVE_PATH="${TARGET_FOLDER}/${ARCHIVE_NAME}"
 +
 
 +
 
 +
7z a -t7z -mx=9 -mfb=64 -md=32m -ms=on -mmt=on "/tmp/${ARCHIVE_NAME}" "${FOLDER_TO_BACKUP}"
 +
mv "/tmp/${ARCHIVE_NAME}" "${TARGET_FOLDER}"
 +
 
 +
ARCHIVE_COUNT=$(find "${TARGET_FOLDER}" -maxdepth 1 -type f -name "${ARCHIVE_ROOT}*.7z" | wc -l)
 +
 
 +
if [ "${ARCHIVE_COUNT}" -gt "${BACKUP_COUNT}" ]; then
 +
  echo "found ${ARCHIVE_COUNT} backups, removing oldest one(s)"
 +
  find "${TARGET_FOLDER}" -maxdepth 1 -type f -name "${ARCHIVE_ROOT}*.7z" | sort -r | tail -n +"${BACKUP_COUNT}" | xargs -I {} rm -f {}
 +
fi
 +
</pre>
 +
 
 +
Lastly, edit the system <code>crontab</code> with <code>crontab -e</code> (in Debian derivatives), and add the following line to have a backup taken every 8 hours:
 +
 
 +
<code>0 */8 * * * /home/YOURUSERNAME/scripts/bisq2backup > /tmp/bisq1backup.log</code>

Revision as of 08:08, 21 July 2025

The biggest issues for users happen when data files are lost or corrupted, leading to missing or borked trades, or unrecoverable Delayed Payout Transactions.

Having an automated backup solution helps covering that risk, by removing the need to manually run a backup after every application state change (mainly "offer created", "trade started", "trade completed").

Issues could still happen when a critical event occurs right after a state change was saved to disk, and before an automated backup was taken, but there are strategies to mitigate that possibility, which will be discussed.

Common requirements and backup strategy

All scripts require 7z compression (7-zip on Windows, p7zip-full on Linux, p7zip on MacOS).

The code will backup all functional files in the data directory, leaving out non-unique files that the Bisq application will fetch/re-create autonomously, and compress them into a 7zip archive, keeping the number of last backups that you define, while deleting the oldest one(s) if the existing number of backups is higher.

It is advised that you direct the backups to either an external drive, or a NAS, or an encrypted cloud storage.

Depending on how bulletproof of a backup schedule you want, you can increase the number of backups to keep on disk to 10 or more (the size of each depends on the history of your instance, but it usually resides in a few tens of MB, often less) and the frequency the script runs with, for example every 1 or 2 hours.

Bisq1

Linux

Create a scripts folder in your home directory, prepare an executable file and open it with an editor:

cd ~
mkdir -p scripts
cd scripts
touch bisq1backup
chmod +x bisq1backup
nano bisq1backup

paste the following code in the file, and edit where needed:

#!/bin/bash

# EDIT THE FOLLOWING
SOURCE_DIR="/home/YOURUSERNAME/local/share/Bisq/btc_mainnet"
TARGET_FOLDER="/mnt/BACKUPS/BISQ1"
BACKUP_COUNT=4
# ##

ARCHIVE_ROOT="bisq1_backup_"
BACKUP_FILE="${ARCHIVE_ROOT}$(date +%Y%m%d_%H%M%S).7z"
ARCHIVE_PATH="${TARGET_FOLDER}/${BACKUP_FILE}"
SELECTED_FILES=(
"BsqSwapTrades"
"BallotList"
"DisputeList"
"RefundDisputeList"
"SequenceNumberMap"
"MyVoteList"
"OpenOffers"
"MyBlindVoteList"
"ClosedTrades"
"MediationDisputeList"
"MyProposalList"
"MailboxMessageList"
"AccountAgeWitnessStore"
"PendingTrades"
"MyProofOfBurnList"
"PreferencesPayload"
"UserPayload"
"AddressEntryList"
"FailedTrades"
"MyReputationList"
"TempProposalStore"
"NavigationPath"
"UnconfirmedBsqChangeOutputList"
"IgnoredMailboxMap"
"PeerList"
"RemovedPayloadsMap"
"TradeStatistics3Store"
"SignedWitnessStore"
)

FOLDER_KEYS="keys"
FOLDER_WALLET="wallet"
FOLDER_TOR="tor/hiddenservice"

FILE_LIST=$(mktemp)

SUBFOLDER_DB_PATH="$SOURCE_DIR/db"
for file in "${SELECTED_FILES[@]}"; do
  if [ -f "$SUBFOLDER_DB_PATH/$file" ]; then
    echo "$SUBFOLDER_DB_PATH/$file" >> "$FILE_LIST"
  fi
done

for subfolder in "$FOLDER_KEYS" "$FOLDER_WALLET"; do
  find "$SOURCE_DIR/$subfolder" -maxdepth 1 -type f >> "$FILE_LIST"
done

FOLDER_TOR_PATH="$SOURCE_DIR/$FOLDER_TOR"
if [ -d "$FOLDER_TOR_PATH" ]; then
  find "$FOLDER_TOR_PATH" -type f >> "$FILE_LIST"
fi

7z a -t7z -mx=9 -m0=lzma2 -mmt=on -spf "$ARCHIVE_PATH" @"$FILE_LIST"

rm "$FILE_LIST"

echo "Backup completed: $BACKUP_FILE"

ARCHIVE_COUNT=$(find "${TARGET_FOLDER}" -maxdepth 1 -type f -name "${ARCHIVE_ROOT}*.7z" | wc -l)

if [ "${ARCHIVE_COUNT}" -gt "${BACKUP_COUNT}" ]; then
  echo "found ${ARCHIVE_COUNT} backups, removing oldest one(s)"
  find "${TARGET_FOLDER}" -maxdepth 1 -type f -name "${ARCHIVE_ROOT}*.7z" | sort -r | tail -n +"${BACKUP_COUNT}" | xargs -I {} rm -f {}
fi

Lastly, edit the system crontab with crontab -e (in Debian derivatives), and add the following line to have a backup taken every 8 hours:

0 */8 * * * /home/YOURUSERNAME/scripts/bisq1backup > /tmp/bisq1backup.log


Bisq2

Linux

Create a scripts folder in your home directory, prepare an executable file and open it with an editor:

cd ~
mkdir -p scripts
cd scripts
touch bisq2backup
chmod +x bisq2backup
nano bisq2backup

paste the following code in the file, and edit where needed:

#!/bin/bash

# EDIT THE FOLLOWING
FOLDER_TO_BACKUP="/home/YOURUSERNAME/local/share/Bisq2/db"
TARGET_FOLDER="/mnt/BACKUPS/BISQ2"
BACKUP_COUNT=4
# ##

ARCHIVE_ROOT="bisq2_backup_"
ARCHIVE_NAME="${ARCHIVE_ROOT}$(date +%Y%m%d_%H%M%S).7z"
ARCHIVE_PATH="${TARGET_FOLDER}/${ARCHIVE_NAME}"


7z a -t7z -mx=9 -mfb=64 -md=32m -ms=on -mmt=on "/tmp/${ARCHIVE_NAME}" "${FOLDER_TO_BACKUP}"
mv "/tmp/${ARCHIVE_NAME}" "${TARGET_FOLDER}"

ARCHIVE_COUNT=$(find "${TARGET_FOLDER}" -maxdepth 1 -type f -name "${ARCHIVE_ROOT}*.7z" | wc -l)

if [ "${ARCHIVE_COUNT}" -gt "${BACKUP_COUNT}" ]; then
  echo "found ${ARCHIVE_COUNT} backups, removing oldest one(s)"
  find "${TARGET_FOLDER}" -maxdepth 1 -type f -name "${ARCHIVE_ROOT}*.7z" | sort -r | tail -n +"${BACKUP_COUNT}" | xargs -I {} rm -f {}
fi

Lastly, edit the system crontab with crontab -e (in Debian derivatives), and add the following line to have a backup taken every 8 hours:

0 */8 * * * /home/YOURUSERNAME/scripts/bisq2backup > /tmp/bisq1backup.log