Answer the question
In order to leave comments, you need to log in
What is the correct way to write a bash script to copy files?
There is a folder with a lot of documents and structure: year, month, day, hour. I want to organize backup with data archiving in the same style. For example:
Reports/2017/01/01/01/00 and get a folder with archives in the same order 2017/01/01/01/00.tar.gz . Since there is little space on a working PC, and up to 10 GB of reports are generated in each folder per hour, we would like the copied folder to be deleted after creating the archive. Please do not judge strictly, I am a beginner enikey. I will answer kind people with kindness to the Yandex wallet.
Answer the question
In order to leave comments, you need to log in
Well something like:
cd Отчеты
for d in 2017/01/01/01/*; do mkdir -p /path/to/backup/`dirname $d`; tar -czf /path/to/backup/$d.tgz $d; rm -rf $d; done
for d in 2017/*/*/*/*;
I give you real examples of scripts, there are all the answers to your questions.
#!/bin/sh
#скрипт бэкапа баз данных
###########################
#Создаем папку для архивов. -p не ругается когда папка уже есь test проверяет есть ли папка для чистых логов
test ! -d /var/backup/mysql/`date +%Y` && { mkdir -p /var/backup/mysql/`date +%Y` ; }
test ! -d /var/backup/mysql/last && { mkdir -p /var/backup/mysql/last ; }
#прячем от умных, и так не зайдут но всеже.
chmod 600 /var/backup/mysql
chmod 600 /var/backup/mysql/last
# делаем сам дапм файлов sql, свежинькие файлы лежат всегда в ней, очень удобно не нужно заходить в архивы и искать там вчерашние базы, и логируется.
for i in `mysql -uroot -p342342r2 -e'show databases;' | grep -v information_schema | grep -v Database`; do mysqldump -uroot -p342342r2 $i > /var/backup/mysql/last/$i.sql;done >> /dev/null 2>> /var/log/sqlbackup.log
# Архивируем дамп, ну и логируем разумеется
cd /var/backup/mysql/
tar -czvf /var/backup/mysql/`date +%Y`/sqldump-`date +%Y-%m-%u`.tar.gz ./last >> /dev/null > /var/log/sqlbackup.log
#(echo "Subject: Бэкап mysql завершен"; cat /var/log/sqlbackup.log;) | /usr/sbin/sendmail [email protected]
################## Конец скрипта
# БЭКАП /etca
test ! -d /var/backup/etc/ && { mkdir -p /var/backup/etc/ ; }
DATE=`date +%F`;
BACKUPPATH="/var/backup/etc";
find $BACKUPPATH/ -mtime +60 | xargs rm -f; #удаляет предыдущие бэкапы старше 60 дней.
tar -czvf "$BACKUPPATH/etc.$DATE.tar.gz" /etc/ > /dev/null 2> /dev/null;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question