#[SOLVED] Cron for backup 💾

1 messages · Page 1 of 1 (latest)

cold musk
#

The script for backup.sh and restore.sh work normally when I run manually, but when adding a crontab to run the script the dump file.sql is saved empty, someone has some solution. I use a ubuntu vps.

charred raptor
#

I've encounter the same issue

#

What I've done was to add full path to all the files
Like so

/usr/local/bin/docker-compose -f /root/appwrite/docker-compose.yml exec mariadb sh -c 'exec mysqldump --all-databases --add-drop-database -u"$MYSQL_USER" -p"$MYSQL_PASSWORD"' > dump.sql

The reason is that when the task is ran through t cronjob service it can get wrong with the variables etc,

cold musk
#

I will try to replicate this! 😁

charred raptor
#

The main idea is that any file or binary need to have there full path

  • docker-compose /usr/local/bin/docker-compose
  • Yaml file - /root/appwrite/docker-compose.yml
cold musk
#

currently my backup file looks like this:

cd /home/ubuntu/appwrite

mkdir -p backup

docker-compose exec mariadb sh -c 'exec mysqldump --all-databases --add-drop-database -u"$MYSQL_USER" -p"$MYSQL_PASSWORD"' > ./backup/dump.sql

appwrite_volumes=(uploads cache config certificates functions)
for volume in ${appwrite_volumes[@]}; do
    mkdir -p backup && docker run --rm --volumes-from "$(docker-compose ps -q appwrite)" -v $PWD/backup:/backup ubuntu bash -c "cd /storage/$volume && tar cvf /backup/$volume.tar ."
done ```

how would I modify this to work?
charred raptor
#
#!/bin/bash
cd /home/ubuntu/appwrite

mkdir -p backup

usr/local/bin/docker-compose -f /home/ubuntu/appwrite/docker-compose.yml exec mariadb sh -c 'exec mysqldump --all-databases --add-drop-database -u"$MYSQL_USER" -p"$MYSQL_PASSWORD"' > ./backup/dump.sql

appwrite_volumes=(uploads cache config certificates functions)
for volume in ${appwrite_volumes[@]}; do
    mkdir -p backup && docker run --rm --volumes-from "$(docker-compose ps -q appwrite)" -v $PWD/backup:/backup ubuntu bash -c "cd /storage/$volume && tar cvf /backup/$volume.tar ."
done
#

This is updated version
Try this

cold musk
#

It didn't solve the problem I'm still getting the empty dump.sql 😥

charred raptor
#

Try to run this command alone is it works?
usr/local/bin/docker-compose -f /home/ubuntu/appwrite/docker-compose.yml exec mariadb sh -c 'exec mysqldump --all-databases --add-drop-database -u"$MYSQL_USER" -p"$MYSQL_PASSWORD"' > ./backup/dump.sql

#

On what user you're running to Cron? ubuntu or root?
On what user you've created the docker?
You can do

$/home/ubuntu/appwrite: ls -lah

To check the ownership

cold musk
#

usr/local/bin/docker-compose -f /home/ubuntu/appwrite/docker-compose.yml exec mariadb sh -c 'exec mysqldump --all-databases --add-drop-database -u"$ MYSQL_USER" -p"$MYSQL_PASSWORD"' > ./backup/dump.sql

it works if I run the command "sudo backup.sh" but through crontab it still returns empty dump.sql. the crontab is created with the root user I do the permission for chmod 777 for backup.sh and when running the command ls -lah it shows the docker as root too.

I don't know what to do anymore. 🫤

charred raptor
#

How did you wrote the line in the crontab?

cold musk
#

*/2 * * * * /bin/bash /home/ubuntu/appwrite/backup.sh

charred raptor
#

Try it one time without the /bin/bash

As such

*/2 * * * *  /home/ubuntu/appwrite/backup.sh
cold musk
#

doesn't work yet

charred raptor
#

One more thing to try.
Try to change the first line to sh

#!/bin/sh
cd /home/ubuntu/appwrite

mkdir -p backup

usr/local/bin/docker-compose -f /home/ubuntu/appwrite/docker-compose.yml exec mariadb sh -c 'exec mysqldump --all-databases --add-drop-database -u"$MYSQL_USER" -p"$MYSQL_PASSWORD"' > ./backup/dump.sql

appwrite_volumes=(uploads cache config certificates functions)
for volume in ${appwrite_volumes[@]}; do
    mkdir -p backup && docker run --rm --volumes-from "$(docker-compose ps -q appwrite)" -v $PWD/backup:/backup ubuntu bash -c "cd /storage/$volume && tar cvf /backup/$volume.tar ."
done
cold musk
#

Still empty, I can't find where I might be going wrong.

charred raptor
#

It's weird when you're finding the solution do let me know

#

Just final check you sure that this file & path do exists?
home/ubuntu/appwrite/docker-compose.yml

cold musk
#

yes, exist

charred raptor
#

OK, sorry I've made a mistake
Try this

#!/bin/sh
cd /home/ubuntu/appwrite

mkdir -p backup

/usr/local/bin/docker-compose -f /home/ubuntu/appwrite/docker-compose.yml exec mariadb sh -c 'exec mysqldump --all-databases --add-drop-database -u"$MYSQL_USER" -p"$MYSQL_PASSWORD"' > ./backup/dump.sql

appwrite_volumes=(uploads cache config certificates functions)
for volume in ${appwrite_volumes[@]}; do
    mkdir -p backup && docker run --rm --volumes-from "$(docker-compose ps -q appwrite)" -v $PWD/backup:/backup ubuntu bash -c "cd /storage/$volume && tar cvf /backup/$volume.tar ."
done
cold musk
#

I continue with the problem. Thank you so much for your time and effort!

raven bridge
cold musk
#

Not yet 🥲

raven bridge
cold musk
#

everything happens normally when I run the backup.sh script directly in cmd

#

Files are generated

charred raptor
#

Yes of course. The problem is with cron path

raven bridge
cold musk
#
./
./app-63ab55ce4321283aa7fe/
./app-63ab55ce4321283aa7fe/63c9aa8377f9fb2109d7/```
#

I get this error: the input device is not a TTY

#

the rest is basically the same as normal operation

raven bridge
cold musk
#

I no longer get the TTY error and the file is still empty

raven bridge
raven bridge
cold musk
raven bridge
# cold musk

this is an old docker compose 👀 you need docker compose v2

#

you should definitely try and uninstall that old compose. maybe it was installed via pip?

cold musk
#

@raven bridge @charred raptor after updating everything, docker and docker-compose the problem was solved. I really appreciate your help!

#

[SOLVED] Cron for backup 💾

timid plume
#

checking