Python script for local backups with rsync

In Gnu / Linux there are different programs to carry out the backup but personally I like simple things, far from graphical interfaces (which have nothing wrong, of course, but if I can avoid using it, I avoid).
In the rsync command there is a formidable ally of backups that we often forget to do. It has enough options to make a copy with all the necessary requirements. computer-767784_640

The following python script makes backup copies for this purpose. You will see that it is extremely simple and even for those who have absolutely no idea of ​​this language, adding a line for the script to synchronize a new directory is immediate.
In my machine I use an external hard disk that I have called IOmega_HDD, in your case you can rename it in the script according to your case.
Another thing is to add or remove directories from the copy. In the same script as the comment line it is explained how to do it.
To automate the task you can add a line to crontab containing the python interpreter and the path where you want to put the script. I hope it is useful to you.

Warning: the wordpress editor does not allow a spacing at the beginning of the line, thus the necessary indentation in the script has been lost, so I have replaced the blank spaces with periods (.) that you must eliminate with an editor and replace with spaces.

-------------------------------
# -*- coding: utf-8 -*-
import os
ruta_usuario=os.getcwd()
ruta_volumen="/media/Iomega_HDD" #Modificar según nombre de disco externo
directorio_destino=ruta_volumen + "/" + "RsyncBackup"
try:
....if os.path.exists(directorio_destino):
........pass
....else:
........os.mkdir(directorio_destino,0777)
....directorios_origen=[] ....rutas_directorios_origen=[] ....#Se añaden los directorios para sincronizar
....directorios_origen.append("Documentos")
....directorios_origen.append("Imágenes")
....directorios_origen.append("Descargas")
....#Añadir aquí otros directorios que se deseen sincronizar
....#o eliminar de las líneas anteriores los que no se deseen
....for rutas in directorios_origen:
....rutas_directorios_origen.append(ruta_usuario + "/" + rutas)
....for rutas in rutas_directorios_origen:
....print "Sincronizando " + rutas + " con " + directorio_destino
....os.system("rsync -ahv --progress" + " " + rutas + " " + directorio_destino)
....print "Proceso terminado"
except OSError:
print "Ha ocurrido un error ¿está el disco externo listo?"
except:
print "Ha ocurrido un error"

---------------------------


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

18 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.   Matias said

    Hi, how are you?
    I liked the script, very simple.
    No offense, I made some modifications to make it simpler and more readable, as well as supporting Python 2 and 3 (currently it could only be run in Python 2)

    I leave you the link with the 2 versions, in case you are interested.
    http://linkode.org/1np9l2bi8IiD5oEkPIUQb5/Yfa4900cA76BpcTpcf4nG1

    1.    dandutrich said

      Great mods and I'm glad you like the script

  2.   niphosio said

    The intention is appreciated, but the result is tacky and sausage.
    My 4 year old niece is capable of making a more intelligent and configurable script than this potato you have put here.

    By the way, the indentation of the code is wrong, check your loops and I don't mean the hair ones

    1.    dandutrich said

      The script works perfectly, I've been using it for a long time and, in fact, due to the number of people sharing it, it shouldn't be as crappy as you say. Maybe you should call your niece to see if you have set everything correctly

    2.    tr said

      Hey, learn to value and instead of criticizing, correct, if you brag so much.

      1.    dandutrich said

        Exactly tr, Matias has made some great modifications. Certainly the script can be improved and that is what it is about in the world of collaboration and that is what Matias has expressed. It is a pity that individuals are around here to sour the good atmosphere that should prevail. There they.

    3.    abaddon s said

      Do you think that a rude criticism is useful and that it does not contribute anything to the scrip? BETTER DON'T YOU WRITE A FUCKING SCRIPT AND SHARE IT !!!!!!!

  3.   Idunno said

    Here another version: https://gist.github.com/Itsuki4/5acc3d03f3650719b88d
    Comment on the errors that I have, I will correct it (now I am in windows and I could not test it).

  4.   zetaka01 said

    Well I use rsync directly with a shell script, without using python.
    I put a line for each source and destination directory.
    I have several scripts depending on the device on which I make the copy, in my case incremental.
    For example, to copy my books to a 128MB usb that is installed by default in
    / media / zetaka01 / Sandisk128 I put in the script LibrosAusb128.sh the following line:

    rsync -av –delete / home / zetaka01 / Books / media / zetaka01 / Sandisk128 /

    If the destination directory does not exist, it creates it for you and deletes from the destination what is not in the origin, recursively of course.
    A greeting.

  5.   zetaka01 said

    Ah, a copy / paste –delete error, with two hyphens.

    a greeting

  6.   dandutrich said

    Do you want to create a graphical interface? I have seen the possibilities of Tkinter and Tix but for the control of the choice of directories perhaps Wx is better

  7.   zetaka01 said

    There is already a graphical interface based on GTK, it is called grsync.
    I leave the link to Wikipedia, https://en.wikipedia.org/wiki/Grsync
    A greeting.

  8.   fernando said

    Hello everyone. The script can be a marvel or a simplicity I do not know or care but things can be said in a thousand ways and when they can be said well, why say them wrong? Having said that, I have to say that I have been a Linux user since 2008 and despite all this time I am slow to learn and I have a hard time understanding many things including how to run a script (I know it's easy but one doesn't give any more). install programs by compiling them etc. etc. That is why when I read that there is a version with a graphical interface, I have looked and found this page where they give you everything even chewed up. For the clumsy as a server I leave it here. Greetings and thanks for your effort.
    http://www.opbyte.it/grsync/download.html

    1.    dandutrich said

      fernando, without any acrimony and if you don't mind answering, I'm curious why you use Gnu / Linux. Thanks and best regards

  9.   zetaka01 said

    Well, a graphical interface is very friendly but it doesn't give you the options that a full command gives you.
    In addition, it is not my case that I do it to measure, a script, either shell or python or whatever you want, allows you to program it to run whenever you want.
    Ah, in your linux distro you should have rsync and grsync without problems in the repositories.
    A greeting.

  10.   zetaka01 said

    Ah Fernando, if you've been using linux since 2008 and you don't know how to run a script, I have no words.
    a greeting

  11.   Gonzalo Martinez said

    Pa are all systems engineers here who criticize the script that someone makes to collaborate, and if to use console / script or whatever?

    How much shit to fart for God's sake.

    I have been managing Linux servers for 10 years, and the truth is that the electronic erection of doing everything with scripts happened to me a while ago, for example, to administer Bacula, I prefer to use the graphical interface than the shell to pretend to be important, which is really a criminal medium .

    One has to be productive, if someone feels more comfortable doing it through the interface, well for him, what matters is the result, not how you do it.

    In my previous job I directed the IT area of ​​a company, and the people in charge asked him to do something specific, I was interested in the result, he did not say «Configure another vhost in apache, using the vi without color in a terminal 30 × 20 ”, that he does it as he is most comfortable, if the guy preferred to do it like that, mounting by SFTP and using the Windows notepad, or praying our Father, I did not care as long as he did it right.

    dandutrech, the script fulfills its objective, which is the important thing, now what I would change is that instead of invoking the command from the shell, it would suddenly be using Python-librsync, which is a library to use the rsync functions within Python.

    With that you gain portability, the script runs in any environment, be it Linux, Windows or OS X.

  12.   dandutrecht said

    Thanks, Gonzalo. Your suggestion I think is very good and I am going to put it in the script. a greeting