Skip to content

Commit

Permalink
Add capability to sync multiple ids
Browse files Browse the repository at this point in the history
  • Loading branch information
rodriguestiago0 committed Jul 12, 2024
1 parent dfab858 commit 89875ac
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 47 deletions.
5 changes: 3 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
# BACKUP_FILE_SUFFIX="%Y%m%d"
# BACKUP_KEEP_DAYS="0"
# TIMEZONE="UTC"
# ACTUAL_BUDGET_URL="https://localhost:5006"
# ACTUAL_BUDGET_PASSWORD=""
# ACTUAL_BUDGET_URL= #without quotes and the last /
# ACTUAL_BUDGET_PASSWORD= #without quotes
# ACTUAL_BUDGET_SYNC_ID= #without quotes
55 changes: 33 additions & 22 deletions scripts/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ function clear_dir() {
rm -rf backup
}

function backup_init() {
function backup_file_name () {
NOW="$(date +"${BACKUP_FILE_DATE_FORMAT}")"
# backup zip file
BACKUP_FILE_ZIP="backup/backup.${NOW}.zip"
BACKUP_FILE_ZIP="backup/backup.$1.${NOW}.zip"
}

function download_actual_budget() {
color blue "Downloading Actual Budger backup"
color green "Login into Actual Budger"
local TOKEN="$(curl --location "${ACTUAL_BUDGET_URL}/account/login" --header 'Content-Type: application/json' --data-raw "{\"loginMethod\": \"password\",\"password\": \"${ACTUAL_BUDGET_PASSWORD}\"}" | jq --raw-output '.data.token'
)"
color green "Login into Actual Budget"

color green "Get file id"
local FILE_ID=$(curl --location "${ACTUAL_BUDGET_URL}/sync/list-user-files" \--header "X-ACTUAL-TOKEN: $TOKEN" | jq --raw-output ".data[] | select( [ .groupId | match(\"$ACTUAL_BUDGET_SYNC_ID\") ] | any) | .fileId")
local TOKEN="$(curl -s --location "${ACTUAL_BUDGET_URL}/account/login" --header 'Content-Type: application/json' --data-raw "{\"loginMethod\": \"password\",\"password\": \"${ACTUAL_BUDGET_PASSWORD}\"}" | jq --raw-output '.data.token')"

color green "Downloading backup files"
curl --location "${ACTUAL_BUDGET_URL}/sync/download-user-file" --header "X-ACTUAL-TOKEN: $TOKEN" --header "X-ACTUAL-FILE-ID: $FILE_ID" --output "${BACKUP_FILE_ZIP}"
for ACTUAL_BUDGET_SYNC_ID_X in "${ACTUAL_BUDGET_SYNC_ID_LIST[@]}"
do
color green "Get file id for ${ACTUAL_BUDGET_SYNC_ID_X}"
backup_file_name $ACTUAL_BUDGET_SYNC_ID_X

local FILE_ID=$(curl -s --location "${ACTUAL_BUDGET_URL}/sync/list-user-files" \--header "X-ACTUAL-TOKEN: $TOKEN" | jq --raw-output ".data[] | select( [ .groupId | match(\"$ACTUAL_BUDGET_SYNC_ID_X\") ] | any) | .fileId")
color green "Downloading backup files"
curl -s --location "${ACTUAL_BUDGET_URL}/sync/download-user-file" --header "X-ACTUAL-TOKEN: $TOKEN" --header "X-ACTUAL-FILE-ID: $FILE_ID" --output "${BACKUP_FILE_ZIP}"
done

}

function backup() {
Expand All @@ -35,22 +40,29 @@ function backup() {


function upload() {
if !(file "${BACKUP_FILE_ZIP}" | grep -q "Zip archive data" ) ; then
color red "Error downloading file"

exit 1
fi

for ACTUAL_BUDGET_SYNC_ID_X in "${ACTUAL_BUDGET_SYNC_ID_LIST[@]}"
do
backup_file_name $ACTUAL_BUDGET_SYNC_ID_X
if !(file "${BACKUP_FILE_ZIP}" | grep -q "Zip archive data" ) ; then
color red "Error downloading file"

exit 1
fi
done

# upload
for RCLONE_REMOTE_X in "${RCLONE_REMOTE_LIST[@]}"
do
color blue "upload backup file to storage system $(color yellow "[${BACKUP_FILE_ZIP} -> ${RCLONE_REMOTE_X}]")"
for ACTUAL_BUDGET_SYNC_ID_X in "${ACTUAL_BUDGET_SYNC_ID_LIST[@]}"
do
backup_file_name $ACTUAL_BUDGET_SYNC_ID_X
color blue "upload backup file to storage system $(color yellow "[${BACKUP_FILE_ZIP} -> ${RCLONE_REMOTE_X}]")"

rclone ${RCLONE_GLOBAL_FLAG} copy "${BACKUP_FILE_ZIP}" "${RCLONE_REMOTE_X}"
if [[ $? != 0 ]]; then
color red "upload failed"
fi
rclone ${RCLONE_GLOBAL_FLAG} copy "${BACKUP_FILE_ZIP}" "${RCLONE_REMOTE_X}"
if [[ $? != 0 ]]; then
color red "upload failed"
fi
done
done

}
Expand Down Expand Up @@ -81,9 +93,8 @@ color blue "running the backup program at $(date +"%Y-%m-%d %H:%M:%S %Z")"
init_env

check_rclone_connection
#

clear_dir
backup_init
backup
upload
clear_dir
Expand Down
47 changes: 24 additions & 23 deletions scripts/includes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,46 +161,47 @@ function get_rclone_remote_list() {
done
}

function init_actual_env(){


# ACTUAL BUDGET
get_env ACTUAL_BUDGET_URL
ACTUAL_BUDGET_URL="${ACTUAL_BUDGET_URL:-"https://localhost:5006"}"
color yellow "ACTUAL_BUDGET_URL: ${ACTUAL_BUDGET_URL}"
function init_actual_sync_list() {
ACTUAL_BUDGET_SYNC_ID_LIST=()

get_env ACTUAL_BUDGET_PASSWORD
ACTUAL_BUDGET_PASSWORD="${ACTUAL_BUDGET_PASSWORD:-""}"
color yellow "ACTUAL_BUDGET_PASSWORD: *****"

get_env ACTUAL_BUDGET_SYNC_ID
ACTUAL_BUDGET_SYNC_ID="${ACTUAL_BUDGET_SYNC_ID:-""}"
color yellow "ACTUAL_BUDGET_SYNC_ID: ${ACTUAL_BUDGET_SYNC_ID}"

ACTUAL_BUDGET_SYNC_ID_LIST=(${ACTUAL_BUDGET_SYNC_ID})
local i=0
local ACTUAL_BUDGET_SYNC_ID_X_REFER

# for multiple
while true; do
ACTUAL_BUDGET_SYNC_ID_X_REFER="ACTUAL_BUDGET_SYNC_ID_${i}"
get_env "${ACTUAL_BUDGET_SYNC_ID_X_REFER}"

if [[ -z "${!ACTUAL_BUDGET_SYNC_ID_X_REFER}" ]]; then
if [[ -z "${!ACTUAL_BUDGET_SYNC_ID_X_REFER}" ]]; then
break
fi

color blue "${ACTUAL_BUDGET_SYNC_ID_X_REFER}"
ACTUAL_BUDGET_SYNC_ID_LIST+=(${ACTUAL_BUDGET_SYNC_ID_X_REFER})

ACTUAL_BUDGET_SYNC_ID_LIST=(${ACTUAL_BUDGET_SYNC_ID_LIST[@]} ${!ACTUAL_BUDGET_SYNC_ID_X_REFER})

((i++))
done

for ACTUAL_BUDGET_SYNC_ID_X in "${ACTUAL_BUDGET_SYNC_ID_LIST[@]}"
do
color blue "${ACTUAL_BUDGET_SYNC_ID_X}"
color yellow "ACTUAL_BUDGET_SYNC_ID: ${ACTUAL_BUDGET_SYNC_ID_X}"
done
exit 1
}

function init_actual_env(){
# ACTUAL BUDGET
get_env ACTUAL_BUDGET_URL
ACTUAL_BUDGET_URL="${ACTUAL_BUDGET_URL:-"https://localhost:5006"}"
color yellow "ACTUAL_BUDGET_URL: ${ACTUAL_BUDGET_URL}"

get_env ACTUAL_BUDGET_PASSWORD
ACTUAL_BUDGET_PASSWORD="${ACTUAL_BUDGET_PASSWORD:-""}"
color yellow "ACTUAL_BUDGET_PASSWORD: *****"

get_env ACTUAL_BUDGET_SYNC_ID
ACTUAL_BUDGET_SYNC_ID="${ACTUAL_BUDGET_SYNC_ID:-""}"
ACTUAL_BUDGET_SYNC_ID_0="${ACTUAL_BUDGET_SYNC_ID}"

init_actual_sync_list
}

########################################
Expand Down

0 comments on commit 89875ac

Please sign in to comment.