V
V
Vechkov Alexander2019-06-19 15:30:28
bash
Vechkov Alexander, 2019-06-19 15:30:28

Through Bash, delete the oldest file on the Yandex disk in a certain folder if there are more than 10 files in it, how?

I make a backup of the site (database and files), throw them on the site server and duplicate them on the Yandex disk. All this through the Linux console bahs. On the server, I delete all backups except the last 10 through the same bash.
How can I implement this on a Yandex disk using bahs ???
Here is the code for my bahs script:

#!/bin/bash
#
#Переменные Базы данных
DBHOST="Адрес MySQL сервера"
DBUSER="Имя пользователя базы данных"
DBPASS="Пароль пользователя базы данных"
DBNAME="Имя базы данных"
DBARC=$DBNAME.sql.gz
#
#Переменные WEBDAV
WEBDAVURL="Адрес Яндекс.Диск/"
WEBDAVUSER="Имя пользователя от Яндекс.Диска(Яндекс.Почта)" 
WEBDAVPASS="Пароль от Яндекс.Диска"
#
#Переменные сайта
SCRIPTDIR="Абсолютный путь откуда запускается скрипт и где хранятся архивы" 
SCRDIR="Абсолютный путь к сайту от корня диска" 
#SCREXCLUDE="Что не попадет в архив"
SCRARC="Имя архива файлов сайта.tar.gz"
#
#Переменные Резерных копий
ARCNAME="Имя архивной копии сайта"=$(date '+%F(%H:%M)')".tar"
ARCMAX="Количество файлов в локальном хранилище"
#
#Переходим в корневую директорию вебсервера
cd $SCRDIR
#
#Создаем файловый архив со сжатием, учитываем исключения
tar cfz $SCRIPTDIR$SCRARC --exclude=$SCREXCLUDE *
#
#Возвращаемся в папку со скриптом, где лежат все архивы
cd $SCRIPTDIR
#
#Архивируем базу данных со сжатием
mysqldump -h$DBHOST -u$DBUSER -p'$DBPASS' --single-transaction > $DBNAME | gzip > $DBARC
#
#Объединяем файловый архив и дамп базы данных, теперь уже без сжатия
tar cf $SCRIPTDIR$ARCNAME $SCRARC $DBARC
#
#Отправляем результат в Яндекс.Диск
curl --user $WEBDAVUSER:$WEBDAVPASS -T $ARCNAME $WEBDAVURL
#
#Убираем промежуточные архивы
rm *.gz
rm $DBNAME
#
#Удаляем старые копии сайта, оставляем несколько свежих копий
ls -t *.tar | tail -n+$ARCMAX | xargs rm -f

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Viktor Taran, 2019-06-19
@shambler81

no need to mount, this may be required by the security policy on the server
, but remotely it’s quite possible for yourself, in fact everything is the same.
on the crown from the root, you mount,
you get the values, you delete, you unmount. and that's it
You may need to
find -type f delete the
oldest file
rm -rf $(ls /var/backup/site | head -1 ) head -1 I would also optimize your file, and add to it not a hard-coded database, but show databases in a loop, and in this case, new databases will automatically enter the script, but this was not in the task, just a comment.

M
moropsk, 2019-06-19
@moropsk

https://serveradmin.ru/backup-linux-servera-s-pomo...
https://blog.selectel.ru/duplicity-rezervnoe-kopir...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question