Script for automatic backups of your server

Those of us who manage servers know how important it is to have saves, backups of everything ... well, in case of any problem or failure, the backup will be our best friend and will help us restore services 🙂

Some time ago (several months ... quite a few months) here, configurations were not backed up on servers, logs or something like that automatically. And it just couldn't be like that haha, I considered using Bacula, but God!! For what I wanted this was too, very, very complicated in my opinion, if what you want is to simply make backups and save these (or send them to another server, or by email) there is no need to use Bacula, with a simple script all problems are solved, hence I decided to make my own script and thus I was more satisfied 😀

And precisely this script is the one that I share with you, I explain very briefly what it does:

  1. Create a folder where everything will be saved, and this folder is the one that will then be compressed.
  2. This folder will have the name of the year, month and present day, for example today that folder would be called: 2012-04-26
  3. Copy / etc / (and all its content) to that folder.
  4. Copy logs (/ var / log /) to that aforementioned folder.
  5. Export the MySQL databases that we have.
  6. Compress with a password (PASSWORD) that folder, compress it into RAR.
  7. Generate a file (data.info) with all the log of the above done (file copy log and compression to .rar), in addition to putting the size (in MBs) of the .RAR file, which I remind you, contains everything we decided to save.
  8. Delete the folder in which we put the files and then compressed, because if we already have this compressed folder, there is no need to also have it uncompressed.
  9. Send emails to administrators or people related to the server, informing that the backup was done correctly, and the file with the log of everything will be sent attached to that email (data.info.rar)

Obviously, this script is not to be executed and now haha, you must open it and change your MySQL password in it, because I do not know the root password of your databases LOL !!!, as well as change the emails to the They want the notification to be sent, because the emails I put are only for example.

If you want to compress it into .tar.gz and not in .rar (how the script is configured) there I left the commented line, it is just uncomment it and comment on the .rar. Likewise, if you want to copy by SSH (using SCP) the compressed file to another server or hosting, I also left the line at the end (it is commented), in it you must put the access data to your server or hosting (user and domain or server URL), but for this to work you must also configure SSH without password, since the script will not be able to access the server if it does not have access allowed on it.

Ah, if you want the email sending thing to work, you must have postfix installed on the server, almost all install postfix but hey, the clarification is valid 🙂

Although ... they could modify and use a script in Python that I left a while ago, but I think it would work a little more ^ - ^ U

And well, it only remains to leave the script:

VPS Backup Script

Remember that you have to give it execution permissions (chmod + x vps_backup-script.sh)

To get it to run every day at 10AM, they put this in a terminal:

echo "* 10    * * *   root    cd /root && ./vps_backup-script.sh" >> /etc/crontab && /etc/init.d/cron restart

Assuming the script is saved as: /root/vps_backup-script.sh

Well enough, I don't want to make this seem too complicated, which is not haha, actually this is something extremely simple, only that when you see it for the first time it can be a bit scary 🙂

Let me know any doubt, question or suggestion, you know that I like to help 😀

regards

PD: I clarify that I am not nor do I consider myself a programmer haha, not even close LOL !! I know the script can be optimized much more, but hey… I'm not a programmer


The content of the article adheres to our principles of editorial ethics. To report an error click here!.

29 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Faustod said

    Putting away,

    Regards,

    How are you very interesting, but one caveat; if that script is run today it will be called 2012-04-25, today is the day of the watch.

    Thank you
    Faustod

    1.    KZKG ^ Gaara said

      HAHAHA true hahaha, is that I like I live in the future ... LOL !!!

  2.   linuxman said

    Very interesting, in fact it shows me how to do various things, I am developing my own and I am going to take some pieces of code from yours to add functionalities.

    In my case I will use rsync instead of cp to copy the files.

    Regards !!

    1.    KZKG ^ Gaara said

      Yes, using rsync is a good idea but I preferred to use cp because, I will copy files to a blank folder, I will not sync with any other information haha ​​that's why I just used cp :)

      If you want to know more tips for bash check the tag here on the site ... you will find how to make lock files, control which users can execute the script, etc 😀
      https://blog.desdelinux.net/tag/bash/

      Greetings and thanks for commenting 🙂

  3.   Edwin said

    a server with ssh without password?

    1.    KZKG ^ Gaara said

      Not without a password, but accept SSH connections with confidence from a specific IP, this is done in a VERY secure way with the use of public and private keys, in the link that I left I explain everything in detail 🙂

      1.    Edwin said

        With keys yes, for a moment I was scared xD

        1.    KZKG ^ Gaara said

          LOL!!! nor that I was suicidal LOL !!!

          1.    Courage said

            No, but inside things we are, you really like to complain

  4.   andresnetx said

    great this script.
    hope they keep posting scripts. help those of us who are switching to linux to be more productive in time and in reducing learning time.

    1.    KZKG ^ Gaara said

      😀 thanks to you for commenting.
      Soon I will publish another tip about bash 😉

      regards

      1.    mole frames said

        thank you crazy! one day I will return the favors that the comrades of this beautiful linux community give me!

  5.   ivan said

    How can I copy the content of several log files and place it in a single file ,,,, automatically every 5 minutes, taking into account ,,,,, that the content of the files to be copied is constantly measured

    1.    elwuilmer said

      My recommendation, (suggestion), would be a contribution… Create a condition that every 5 minutes he verifies the file or files, either with:

      * last access = atime
      * last modified = mtime
      * last information change = ctime

      Accordingly, if there is any change in the files, group them and / or read them (cat) and send them> logs files.

      This is a clear example, a matter of testing, trying, verifying and modifying.

  6.   KZKG ^ Gaara said

    Testing Caché Plugin ...

  7.   Santiago said

    Very good information, the truth that was very useful to me, I am carrying out a large project (obviously it is fictitious, because it is from the studio) and this information was very useful for me.
    Thank you very much!

  8.   little brunette said

    How would you make a shell script that :?
    Ask the user to specify the name of the directory to backup
    Ask you for the location where you will backup the directory
    Include backup date

    1.    KZKG ^ Gaara said

      Hello,

      With "read" I can ask the user for all that data, then I assign it to variables and that's it.

      Write me to my email if you have doubts: kzkggaara [at] desdelinux [dot] net

      regards

      PS: The backup date is already included in the script.

  9.   ana_gaby said

    present a simpler script to backup folders from ubuntu and transfer them by ftp to another server thanks

  10.   Juan said

    Hello, how do I make a backup of the entire linux operating system and send it to another pc. I would appreciate your attention, thank you very much !!

  11.   Franco Valdettaro said

    Could you send me the script to my email fvaldettaro@gmail.com please.

  12.   Javier said

    Could you please send me the script to my email, thanks a million, greetings.

  13.   Wuilmer Bolivar said

    Good morning friend, I think you have a problem with the subdomain "paste" because I was checking some published codes / scripts that lead to paste.desdelinux and all redirect me to blog.desdelinux.

  14.   Squirrels in the net said

    Yes, it is not possible to access the script because paste. redirects you, can you upload it somewhere else?

    1.    AlexStreaming said

      Has the problem to get the script been solved?

      Thank you.

      1.    Luigys toro said

        It has been fixed, they can now access the codes

      2.    Squirrels in the net said

        Now, thank you!

  15.   paco said

    good afternoon, I would like to be able to download the script, it would be possible to upload it again, now it is not available

  16.   Ramiro said

    Hello,
    Very good contribution! Could I ask you for the script to download? Thank you very much 🙂