Backup your system using a simple Shell Scripts

Backup your system using a simple Shell Scripts

by Sidick Allaladé on 13/02/2020 |

Updated at 2/04/2020


This is a super simple way to backup your system without spending a penny. It can be really practical, as backup is always important.

Someone said "When your data does not exist 3 times, that means it's not exist at all". So made some backup and backup your backup :smile::smile:

Here we go:

#!/bin/

date
echo "############### Backup of files on my server ###############"

backupfilename=server_backup_`date '+%Y-%m-%d'`

echo "----- Sql first and dumping current DB -----"
rm -f /tmp/backup.sql
mysqldump --user=youruser --password=yourpassword --all-databases --add-drop-table > /tmp/backup.sql

echo "----- Tar plus zip all files -----"

tar cvf /home/mybackups/files/${backupfilename}.tar /home/www/html/* /tmp/backup.sql
gzip /home/mybackups/files/${backupfilename}.tar
chmod 666 /home/mybackups/files/${backupfilename}.tar.gz
echo "############### Backup done successfully.... ###############"
date

***

Now let explain each line:

  • date => we want to see the server date in our log file
  • backupfilename=server_backup_`date '+%Y-%m-%d'` => filename of the backup with date
  • rm -f /tmp/backup.sql ==> we are removing the old sql backup file generated
  • mysqldump => we are backing up all the sql DB again
  • tar cvf home/www/html/ /tmp/backup.sql => You are backing up all home/www/html/* and /tmp/backup.sql You can also exclude some paths there with : --exclude='yourpathfile'

NOW let's Executing the Script

Name your file 'backup.sh'
To execute it you need to opne terminal prompt and start with a dot '.' so if you name your file backup.sh you will do .backup.sh
Now let's automate it because you probably don't want to do it manually everyday.
So if you want to auto run it everyday at 23h55 you will open your crontab with crontab -e and you will add this :

55 23 * * * /path/toyourfile/backup.sh >> /path/toyourlogfile/backups.log

and close your crontab terminal with :x
NOTE:
>> /path/toyourlogfile/mybackups.log is not mandatory it only needed if you want to be able too see the log of your backup in case of any error.
If you want to set different time in your cron I recommend you to use this tool is super easy and it will do the job.

 

 

Was this article useful?

People Who Read This Article Also Read...

A communication agency everything you need to know

By definition, a communication agency (marketing) is a company that plans and harmonizes internal and / or external communication on be...

A communication agency everyth...


Is ETL Still Relevant in 2019?

ETL is an abbreviation for extract, transform, and load. It is one of data integration tools used in the extraction of data from a sour...

Is ETL Still Relevant in 2019?


Develop a Native, Hybrid or Web application? What are the differences?

There are different types of applications (apps), each with their own advantages and disadvantages. We will try here to lighten your sh...

Develop a Native, Hybrid or We...


How Netlinking Is Useful for Your SEO

With the aim of helping companies improve their notoriety through their blogs. Oshara Inc, shares his knowledge about Netlinking in thi...

How Netlinking Is Useful for Y...


How to obtain instagram certification?

Like Twitter and Facebook, Instagram offers the possibility for administrators of an Instagram page to certify certain pages. Oshara I...

How to obtain instagram certif...


Good or bad idea invest in a good visual identity

Complicated to find something more important than visual identity in business communication. The visual identity of a company is the fi...

Good or bad idea invest in a g...


×