22

Script to backup Firefly III database, uploads and config files installed with d...

 1 year ago
source link: https://gist.github.com/dawid-czarnecki/8fa3420531f88b2b2631250854e23381
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Script to backup Firefly III database, uploads and config files installed with docker-compose · GitHub

Instantly share code, notes, and snippets.

Script to backup Firefly III database, uploads and config files installed with docker-compose

@dawid-czarnecki - thanks for doing the heaving lifting here. There is an issue with my install in that the db-version is not enclosed in single quotes.

'version'                      => '5.6.16',
'api_version'                  => '1.5.5',
'db_version'                   => 18,

I know almost nothing about bash scripts, but managed to work out the following (with help from others via google), but you may have a better solution

docker exec -it fcdacfb823eb fgrep "'db_version'" /var/www/html/config/firefly.php | tr -s ' ' | tr -d ',' | cut -d " " -f 4

Thanks for spotting this @william-1066. I fixed it.

hjhp commented Aug 29, 2022

Thank you for this script!

I made a few notes along the way for my install, which uses PostgreSQL.

full_path failing

I noticed that your full_path was failing on my Ubuntu 18 LTS (Digital Ocean VPS):
Suppose my command is ./fireflyiii_backup.sh backup /home/hugo/fireflyiii/backup/fireflyiii_$(date '+%F').tar AND the folder /home/hugo/fireflyiii/backup/ does not exist.
Then running realpath /home/hugo/fireflyiii/backup/fireflyiii_$(date '+%F').tar returns realpath: /home/hugo/fireflyiii/backup/fireflyiii_2022-08-29.tar: No such file or directory.

This was causing dest_path and dest_file to fail in a potentially dangerous way: because full_path was blank (mkdir: cannot create directory ‘’: No such file or directory), the temporary folder was defined as /tmp (the system tmp folder), which means the rmdir "$dest_path/tmp" command under "Clean up" tried to remove /tmp.
I was not running as root, so nothing bad happened, but the script failed.

Solution: bypass full_path entirely

dest_path="$(dirname $2)"
dest_file="$(basename $2)"

This seems to work because dirname ~/fireflyiii/dir_that_does_not_exist/test.txt returns /home/hugo/fireflyiii/dir_that_does_not_exist.

Modifications for PostgreSQL

  • I have not tested a restore yet.
  • This assumes passwordless access, which is especially important if this is to be a cron job (see pg_dump's -w flag). This may entail setting up a .pgaccess file.

Backup

docker exec $db_container bash -c '/usr/bin/pg_dump --username=hugo -w fireflyiii' > "$dest_path/tmp/firefly_db.sql"

Restore

cat "$src_path/tmp/firefly_db.sql" | docker exec -i $db_container bash -c '/usr/bin/psql --username=hugo fireflyiii'

Thanks @hjhp for spotting the bug. I applied realpath at the beginning because I remember having some issues without it when running the script form other locations. For that reason I prefer to create a directory in case it does not exist.
Feel free to implement a logic to detect the type of the database. Keep in mind, you can use credentials saved in the environmental variables inside db container the same way it's used for MySQL right now:

docker exec $db_container bash -c '/usr/bin/mysqldump -u firefly --password="$MYSQL_PASSWORD" "$MYSQL_DATABASE"' > "$dest_path/tmp/firefly_db.sql"

Thank you for sharing this! It works really well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK