2

I have work on one Magento 2 project with lots of data ~10GB Database + ~1GB CODE + ~5GB Media.

And I want to set up it on my local server. So I want to compress the full project with a database.

I have tried to create a database backup with this command

# mysqldump -u magento2_user -p magento2_db > magento2_db-20201110.sql

It takes half-day ~6h to create DB backup and during this process, my sh connection was backed so I need to recreate the backup.

Now, I have the better command for tack backup.

website:

tar -czvf ~/multi_dump.tar.gz --exclude=var/cache --exclude=var/session --exclude=var/log --exclude=var/tmp --exclude=var/export --exclude=var/report --exclude=var/backups --exclude='media/import' --exclude=media/tmp --exclude=media/downloadable --exclude=media/catalog . && echo OK

database:

mysqldump --single-transaction --add-drop-table -h <host> -u <user> -p <db_name> | gzip > dump.sql.gz

It was a very lengthy and time-consuming task.

So, I am finding a way to create a database backup with the automatic process in the background.

and I am found Ctrl+Z , bg , jobs. means add this process in the background. but when I disconnect my ssh and reconnect it then I can't found that process in the jobs and bg command.

and also I have tried to add this process in crontab -e but I haven't access it.

So, Anyone has any better solution for this problem?

This is os detail.

OS Details

  • sorry, but I can't get a new prompt I have also tried ctrl+Z but not work so I have cancel it by using ctrl+C. – Bhavesh Prajapati Feb 12 '21 at 10:49
  • @Panki thanks for suggestion. – Bhavesh Prajapati Feb 12 '21 at 10:49
  • "I can't found that process in the jobs and bg command" This is because these commands show the jobs of the current shell, and put jobs of the current shell into the background. However, if your shell is Bash, by default it does not kill background processes when it exits. Your tar and mysqldump processes are probably still running, and you can see them with ps -ef | grep -e tar -e mysqldump. – berndbausch Feb 12 '21 at 11:30

3 Answers3

3

tmux (or screen) is indeed a nice solution, but it needs an extra package.

Use of nohup (as suggested by Panki) is probably the best solution regarding portability in the *nix world. However, ubuntu 16 came with systemd, then you could use systemd-run to launch a command:

systemd-run --unit=my_backup --remain-after-exit mysqldump --single-transaction --add-drop-table -h <host> -u <user> -p <db_name> -r magento2_db-20201110.sql

You can then access status and outputs with regular systemd command:

journalctl -b -u my_backup
systemctl status my_backup

You can even easily limit resources your backup uses with extra systemd-run options, cf man 1 systemd-run.

Some explanations:

  • --remain-after-exit is needed to have my_backup.service lying around even after mysqldump finished in order to consult the service status and log (through journalctl)
  • --unit allows to name as you whish to service launched by systemd-run
  • I used mysqldump option -r to avoid stdout redirection.

Using systemd-run to launch any command permits closing remote ssh access without terminating the command.

Panki
  • 6,664
kaliko
  • 583
1

You can try to run the process in background from the start.

$ nohup mysqldump -u magento2_user -p magento2_db > magento2_db-20201110.sql &

nohup gives the immune to hangups. means that after the your terminal or ssh session is closed the process will continue in the background. And by adding & in the command your saying the shell to run it in background.

if you wanna track any error that can happen while running the command then you can add this also

$ nohup mysqldump -u magento2_user -p magento2_db > magento2_db-20201110.sql 2>error.log & 

the 2>error.log means that the stderr of your running process will be redirected to the error.log file.

Sanaf
  • 222
  • 3
    If this is a Bash shell, and the huponexit option is not set (see https://www.gnu.org/software/bash/manual/html_node/The-Shopt-Builtin.html), background processes will not receive the hangup signal. Although it is not stated in the Bash reference guide, it looks like by default, huponexit is not set. To be certain, OP can use the nohup command. – berndbausch Feb 12 '21 at 13:33
1

install a terminal multiplexer like tmux could be an option?

you log to the server :

ssh user@srv

Then start tmux:

tmux new -s main

when you open another connection to the remote server your tmux session is still be there:

tmux -a t main

EDIT

I have tried this and it work :

cat .foo.sh

#!/bin/bash while [ 1 ]; do echo "OK"; sleep 120; done;

chmod +x .foo.sh trap './foo.sh &' exit

exit from ssh session

exit

Now log in again:

ssh user@srv
ps -U $USER -o cmd,pid | grep "foo"
/bin/bash ./foo.sh           3099

substantially trap command call the script when receive the exit signal. Of course you need to put your mysqldump cmd on the script that will be executed.