📜 ⬆️ ⬇️

Elementary bash script to back up data

Hi habrareyd, now I will tell as little as possible to automate the routine work of preparing backups.

In this case, we will not use powerful programs, or even entire systems for backing up data, we limit ourselves to the most affordable ones we have. Namely - Bash script.


')

What should execute our script?


Back up a web project, namely:
- Make a backup of the database MySQL.
- Make backup copies of files.
- 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 run a couple of ways:

- Easy start: ./backup.sh
- Run + write to log: ./backup.sh | tee backup.log
- and you can also cram it into cron: 00 20 * * 7 root sh /home/bond/backup.sh | tee /home/bond/backup/backup.log
After successful completion of the script, 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 directories named by date.

ps Thanks for opkdx for writing.

Source: https://habr.com/ru/post/51966/


All Articles