S
S
Sergey Shevchenko2017-11-28 17:52:40
bash
Sergey Shevchenko, 2017-11-28 17:52:40

How to find a substring in a string in bash and store it in a variable?

I'm trying to write a script that will download a database dump via lftp.

lftp -u user,password ftp.server.ru << EOF
ls
bye
EOF

Working out is what it says
drwxr-xr-x   1 8000027  8000027         0 Nov 28 14:48 2017-11-22                                  
drwxr-xr-x   1 8000027  8000027         0 Nov 28 14:48 2017-11-23
drwxr-xr-x   1 8000027  8000027         0 Nov 28 14:48 2017-11-24
drwxr-xr-x   1 8000027  8000027         0 Nov 28 14:48 2017-11-25
drwxr-xr-x   1 8000027  8000027         0 Nov 28 14:48 2017-11-26
drwxr-xr-x   1 8000027  8000027         0 Nov 28 14:48 2017-11-27
drwxr-xr-x   1 8000027  8000027         0 Nov 28 14:48 2017-11-28

I need to go to the last folder and download the latest .tgz file
Tried to use both grep and sed but didn't work (or couldn't)
Please help! I beg you very much!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Taran, 2017-11-29
@lancer_serega

YES please:
reads the list of current databases, creates an archive of each one separately (if you need, you can archive it right there)

#Создаем папку для архивов. -p не ругается когда папка уже есь
mkdir -p /var/backup/mysql/`date +%Y`
mkdir -p /var/backup/mysql/last

#прячем от умных, и так не зайдут но все же.
chmod 700 /var/backup/mysql
chmod 700 /var/backup/mysql/last

# делаем сам дапм файлов sql, свежинькие файлы лежат всегда в ней, очень удобно не нужно заходить в архивы и искать там вчерашние базы, и логируется.
for i in `mysql -uroot -pТУТ_ПАРОЛЬ_РУТА_БД -e'show databases;' | grep -v information_schema | grep -v Database`; do mysqldump -uroot -pТУТ_ПАРОЛЬ_РУТА_БД $i > /var/backup/mysql/last/$i.sql;done >> /dev/null 2>> /var/log/sqlbackup.log
# Архивируем дамп, ну и логируем разумеется
cd /var/backup/mysql/
tar -zcvpf /var/backup/mysql/`date +%Y`/sqldump-`date +%Y-%m-%u`.tar.bz2 ./last >> /dev/nool >> /var/log/sqlbackup.log
# Конец скрипта

This is if done on the other side, but the meaning does not change much.
In fact, you need this
The rest is so read
All that remains for you is to execute the command on a remote server
https://www.shellhacks.com/en/ssh-execute-remote-c...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question