S
S
Sergey Goryachev2016-12-15 01:38:42
linux
Sergey Goryachev, 2016-12-15 01:38:42

How to save backups to the server through the console?

When I'm done with a site, for today or in general, I run a simple script that commits to the git server and creates two files (a database and a site archive).
I want to slightly expand the functionality of the script, but I don’t know which side to approach.
What we have now, for committing to git and archiving:

#!/bin/bash
# делаем бэкап базы данных в папку проекта
sudo mysqldump -u`head -1 mysql.txt | tail -1` -p`head -2 mysql.txt | tail -1` `head -1 mysql.txt | tail -1` > `head -1 mysql.txt | tail -1`.sql
# декодируем базу в нормальную кодировку
sudo grep -rl utf8mb4 `head -1 mysql.txt | tail -1`.sql | xargs perl -p -i -e 's/utf8mb4/utf8/g'
# меняем права и владельца на конечный файл
sudo chown sergey:sergey -R `head -1 mysql.txt | tail -1`.sql
sudo chmod -R 644 `head -1 mysql.txt | tail -1`.sql
# добавляем изменения в git
git add --ignore-removal .
git commit -a -m "$(date +"%Y-%m-%d %H:%M")"
git push origin master
# записываем файлы из директории в архив
tar -cvpzf archive.tar.gz --exclude='node_modules' --exclude='.git' --exclude='archive.sh' --exclude='gitcommit.sh' --exclude='gulpfile.js' --exclude='mysql.txt' --exclude='package.json' --exclude='.gitignore' --exclude='*.sql' --exclude='*.tar.gz' * .htaccess

1. You need to add the date and time of file creation to the name of the archive, ideally, if it is like this: YYYY-MM-DD_HH-MM-SS
2. You need to upload the archive via SSH to the server (you don’t want to enter the password every time) , always /mnt/backupftp/$sitename will be sent to a certain folder - the name $sitename is dynamic, it is substituted depending on the command: sitestop sitename , which actually starts the script.
3. At the same time, I don’t want to make garbage out of the archives folder, it is necessary that no more than 3 backup copies be stored in the folder of each site, that is, old files need to be overwritten somehow if the 4th archive is already being recorded.
Yes, I'm paranoid, I don't have enough copies on the git, I want to have regular archive copies as well.
It seems that the date and time are not difficult to write, but with sending to the server without a password and with overwriting files, I have a real problem)
Where can I dig here?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene Wolf, 2016-12-15
@webirus

It is quite difficult to answer in the "blind to the deaf" mode, because It's not entirely clear what exactly is not working for you ... but, let's try.
In order not to enter a password - use authorization by key. Information on this topic, just a breakthrough, for example here (the first link that came across).
If the name $sitename is already passed to the script, I don't see any problem with using it.

At the same time, I don’t want to make a dump out of the archives folder, it is necessary that no more than 3 backup copies be stored in the folder of each site, that is, old files need to be overwritten somehow if the 4th archive is already being recorded.
After connecting via SSH - count the number of files in the folder, you can also pre-sort them in direct or reverse order by date, and then delete all files according to the list, whose index in the array is greater / less than 2 or 3.
Then just upload your file there...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question