Answer the question
In order to leave comments, you need to log in
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
#!/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 {} \;
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
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
Answer the question
In order to leave comments, you need to log in
#!/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;
#!/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;
#!/bin/sh
#Заливаем файлы на внешний сервер
wput --limit-rate=3000K --timestamping --output-file=/var/log/wput.log /var/backup/mysql/ ftp://login:[email protected]/data/server1/
00 04 * * * nice -19 filebackup_wput >/dev/null 2 >> /var/log/syslog
00 00 * * * nice -19 sqlbackup >/dev/null 2 >> /var/log/syslog
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question