Backup - It's needed!

This is my bakupscript from the web server.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash

day=$(date +%A-%d%m%y-%H%M%S)

echo "Make a backupfile of the web files and folders."
tar cfz web-FILES-$day.tgz /var/www
echo "OK."

echo "Make a MySQL dump of all the databases."
sudo mysqldump -u<USER> -p<PASSWORD> --all-databases > web-SQL-$day.sql
echo "OK."

echo "Make a final backup file."
tar cfz web-$day.tgz web-FILES-$day.tgz web-SQL-$day.sql
echo "OK."

echo "Remove MySQL dump file."
rm web-SQL-$day.sql
echo "OK."

echo "Remove temporary web site backup file."
rm web-FILES-$day.tgz
echo "OK."
echo "Backup done!"

/bin/mv web-$day.tgz /mnt/backup

Run through

To get the day, date and time:

day=$(date +%A-%d%m%y-%H%M%S)

To make a *.tgz of the web folders:

tar cfz web-FILES-$day.tgz /var/www

To make a dump of all the databases in mySQL:

sudo mysqldump -u<USER> -p<PASSWORD> --all-databases > web-SQL-$day.sql

To make a *.tgz file of the web folders and the databases:

tar cfz web-$day.tgz web-FILES-$day.tgz web-SQL-$day.sql

Then the temporary files are removed with the rm command.

The last command, moves the newly created backup file to a designated backup folder, on another computer.

/bin/mv web-$day.tgz /mnt/backup

Time the backup

When the backupscript is working as it should, edit crontab.

# m h dom mon dow command
55 23 * * * bash /home/pi/backup/backup.sh

This line will run the backupscript every day 5 minutes to midnight.