V
V
Vyacheslav Kordienko2016-07-25 17:54:11
linux
Vyacheslav Kordienko, 2016-07-25 17:54:11

Backup script (files + database) for cron?

I can't figure out what I'm doing wrong.
With the help of cron, I make different execution options:
1. Option
I write a script (for example, backup_site.sh) and throw it into /etc/cron.daily

spoiler
#!/bin/bash
echo $(date +%y%m%d)
cd /home/bitrix/
echo "Add site site.ru to archive ...Start"
tar -czpf site1.tar.gz ./www
echo "Add site to archive ...Done!"

echo "Move archive to backup directory"
mv site1.tar.gz /backup/site1-$(date +%y%m%d).tar.gz 
echo "Move archive to backup directory ...Done!"

cd /backup/
echo "Make backup database newshop"
mysqldump --single-transaction -u root sitedatabase1 > sitedatabase1-$(date +%y%m%d).sql
echo "Make backup database newshop ...Done!"

cd /backup
find . -mtime +20 -exec rm {} \;


I write in crontab
spoiler
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly


So from the cron.daily folder, my script will be executed daily at 4:20.
Does not work
2. Option,
I write the script directly in crontab
spoiler
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

#backupsite
20 4 * * * mysqldump --single-transaction -u root sitedatabase1 > sitedatabase1-$(date +%y%m%d).sql > /dev/null 2>&1
20 5 * * * tar -czf /backup/site1-$(date +%y%m%d).tar.gz /home/bitrix/www > /dev/null 2>&1


PS doesn't work either
. crond service started

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Victor Taran, 2016-07-26
@shambler81

#!/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 -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 -czvf /var/backup/mysql/`date +%Y`/sqldump-`date +%Y-%m-%u`.tar.gz ./last >> /dev/null > /var/log/sqlbackup.log
#(echo "Subject: Бэкап mysql  klondike-server завершен"; cat /var/log/sqlbackup.log;) | /usr/sbin/sendmail  [email protected]
##################  Конец скрипта

#  БЭКАП /etc  весит мало а нужна бывает очень сильно ;)
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;

And here is the backup files
#!/bin/sh
cd /var/www
DATE=`date +"%Y-%m-%d"`
LIST=`ls | grep -E ".*\.\w{1,4}"`

#(echo "Subject: Бэкап 2 server начат"; cat /var/log/wput.log;) | /usr/sbin/sendmail  [email protected]
test ! -d /var/backup/site/`date +%Y`/`date +%m` && { mkdir -p /var/backup/site/`date +%Y`/`date +%m` ; }

 for ELEMENT in $LIST
    do

tar -zcvpf /var/backup/site/`date +%Y`/`date +%m`/$ELEMENT-$DATE.tar.gz /var/www/$ELEMENT/web --exclude=backup --exclude=managed_cache --exclude=stats --exclude=resize_cache >> /dev/nool 2> /var/log/file_backup.err

    done

#exit 0
#find /var/backup/site/ -type f -mtime +750 | xargs rm -f; #удаляет предыдущие бэкапы стaарше 2 лет.
#find /var/backup/site/ -type d -empty | xargs rm -r;

But the backup on ftp, using wput - requires you to install from the repository.
#!/bin/sh
#Заливаем файлы на внешний сервер
wput --limit-rate=3000K --timestamping --output-file=/var/log/wput.log /var/backup/mysql/ ftp://login:[email protected]/data/server1/

Well, crown
00 04 * * * nice -19 filebackup_wput >/dev/null 2 >> /var/log/syslog
00 00 * * * nice -19 sqlbackup >/dev/null 2 >> /var/log/syslog

L
Lindon_cano, 2016-07-25
@Lindon_cano

In the first case, I would look at the rights, most likely you did not give +x to the script. In the second, you do not have a line break after the last line, if you were not too lazy to look at the log, you would read about it. Press Enter after the last task.

A
abcd0x00, 2016-07-26
@abcd0x00

It must first be run without cron (from the same directory). If it works without cron, but doesn't work in cron, you need to look at the cron log.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question