Elementary Bash script for data backup
Hi habralyudi, now I’ll tell you how to automate the routine work of preparing backups a bit.
In this case, we will not use powerful programs, or even entire systems for data backup, we will restrict ourselves to the most affordable that we have. Namely - Bash script.
What should our script execute?
Backup a web project, namely:
- Make a backup copy of the MySQL database.
- Back up files.
- To structure it.
And so, here is our script:
You can
start in a couple of ways: - Simple start: ./backup.sh
- Start + write to the log: ./backup.sh | tee backup.log
- and you can also push it into cron: 00 20 * * 7 root sh /home/bond/backup.sh | tee /home/bond/backup/backup.log
After the script completes successfully, we will see the following:
As a result, our backups are added to the directory that you specified, + backups are in the directories named by date.
ps thanks opkdx for the hint when writing.
In this case, we will not use powerful programs, or even entire systems for data backup, we will restrict ourselves to the most affordable that we have. Namely - Bash script.
What should our script execute?
Backup a web project, namely:
- Make a backup copy of the MySQL database.
- Back up files.
- To structure it.
And so, here is our script:
#!/bin/bash
PROJNAME= #Имя проекта
CHARSET= #Кодировка базы данных (utf8)
DBNAME= #Имя базы данных для резервного копирования
DBFILENAME= #Имя дампа базы данных
ARFILENAME= #Имя архива с файлами
HOST= #Хост MySQL
USER= #Имя пользователя базы данных
PASSWD= #Пароль от базы данных
DATADIR= #Путь к каталогу где будут храниться резервные копии
SRCFILES= #Путь к каталогу файлов для архивирования
PREFIX=`date +%F` #Префикс по дате для структурирования резервных копий
#start backup
echo "[--------------------------------[`date +%F--%H-%M`]--------------------------------]"
echo "[----------][`date +%F--%H-%M`] Run the backup script..."
mkdir $DATADIR/$PREFIX 2> /dev/null
echo "[++--------][`date +%F--%H-%M`] Generate a database backup..."
#MySQL dump
mysqldump --user=$USER --host=$HOST --password=$PASSWD --default-character-set=$CHARSET $DBNAME > $DATADIR/$PREFIX/$DBFILENAME-`date +%F--%H-%M`.sql
if [[ $? -gt 0 ]];then
echo "[++--------][`date +%F--%H-%M`] Aborted. Generate database backup failed."
exit 1
fi
echo "[++++------][`date +%F--%H-%M`] Backup database [$DBNAME] - successfull."
echo "[++++++----][`date +%F--%H-%M`] Copy the source code project [$PROJNAME]..."
#Src dump
tar -czpf $DATADIR/$PREFIX/$ARFILENAME-`date +%F--%H-%M`.tar.gz $SRCFILES 2> /dev/null
if [[ $? -gt 0 ]];then
echo "[++++++----][`date +%F--%H-%M`] Aborted. Copying the source code failed."
exit 1
fi
echo "[++++++++--][`date +%F--%H-%M`] Copy the source code project [$PROJNAME] successfull."
echo "[+++++++++-][`date +%F--%H-%M`] Stat datadir space (USED): `du -h $DATADIR | tail -n1`"
echo "[+++++++++-][`date +%F--%H-%M`] Free HDD space: `df -h /home|tail -n1|awk '{print $4}'`"
echo "[++++++++++][`date +%F--%H-%M`] All operations completed successfully!"
exit 0
You can
start in a couple of ways: - Simple start: ./backup.sh
- Start + write to the log: ./backup.sh | tee backup.log
- and you can also push it into cron: 00 20 * * 7 root sh /home/bond/backup.sh | tee /home/bond/backup/backup.log
After the script completes successfully, we will see the following:
bond@serv:~$ sudo sh backup.sh
[--------------------------------[2009-02-14--12-28]--------------------------------]
[----------][2009-02-14--12-28] Run the backup script...
[++--------][2009-02-14--12-28] Generate a database backup...
[++++------][2009-02-14--12-29] Backup database [images] - successfull.
[++++++----][2009-02-14--12-29] Copy the source code project [itmages]...
[++++++++--][2009-02-14--12-29] Copy the source code project [itmages] - successfull.
[+++++++++-][2009-02-14--12-29] Stat datadir space (USED): 1,3G /home/bond/backup
[+++++++++-][2009-02-14--12-29] Free HDD space: 49G
[++++++++++][2009-02-14--12-29] All operations completed successfully!
bond@serv:~$
As a result, our backups are added to the directory that you specified, + backups are in the directories named by date.
ps thanks opkdx for the hint when writing.