DEV Community

Cover image for How to use Node.js to backup your personal files (and learn some webdev skills along the way)

How to use Node.js to backup your personal files (and learn some webdev skills along the way)

Alex Eagleson on December 29, 2021

All code from this tutorial as a complete package is available in this repository. If you find this tutorial helpful, please share it with your fr...
Collapse
 
ecnepsnai profile image
Ian Spence

Great write up! I have one suggestion: test your backups.

Especially if you are using custom software or scripts to actually do the backup, testing that it was successful is of the utmost priority.

Speaking from experience, there is nothing worse than finding out a backup you thought you had taken actually turned out to be corrupted or broken.

Collapse
 
alexeagleson profile image
Alex Eagleson

Great suggestion. What's your preference for testing backups?

Collapse
 
ecnepsnai profile image
Ian Spence

Since your script is just copying the files over to the destination using rsync, you can do a simple integrity test to ensure all files were copied successful.

Here's what I'd do: When your backup script is collecting the list of files to copy, hash them using something like XXHash (or any other fast non-cryptographic hashing algorithm). Record a mapping of files -> hash in a file and back that file up along with the rest.

To test the integrity, iterate over each backed up file and hash it, testing if it matches the original hash when the files was backed up.

Depending on how active the system is, you may wish to also look at using file locking (flock) to prevent writes to a file that is actively being hashed & copied.

Thread Thread
 
alexeagleson profile image
Alex Eagleson

Thank you, I really appreciate the detail of the response. I'm going to look into this and make an effort to implement it with an update so everyone can benefit. Much appreciated.

Collapse
 
december1981 profile image
Stephen Brown

Very nice. I think an optional archive step would be a good idea to add to this - after the rsync to the output directory, tgz or zip the output folder, name the archive file suitably with a date/time, and copy to an archived directory or upload to a cloud service (perhaps encrypting also in the latter case.)

Collapse
 
alexeagleson profile image
Alex Eagleson

That's a REALLY good idea. If I get a chance in the next couple of days I'll definitely add that as en extra section, thank you!

Collapse
 
manomite profile image
Adeyeye George

Instead of hashing which might consume more memory, why not just create a txt file and place the file path and names inside...

Then when checking just compare the file path and name. This will also prevent you from using lock to lock a file too

Collapse
 
gabrieltoma profile image
Gabriel Toma

I always end up with this error: "Backup completed with status code: 1" (I know status 1 is a syntax error or usage). Even with your piece of code, copy/paste, I have the same result.

Suggestions? 🙌

Collapse
 
alexeagleson profile image
Alex Eagleson

Verify the command on your terminal outside of the Node app to get more details. If you add console.log(cmd) inside the rsync.execute block it will print out exactly the syntax of the command that it is running in the background, so you can try running it manually.

You can also add the -v flag to get more verbose output to troubleshoot

So for example on the terminal the command using the directories from this tutorial would look like:

rsync -av example-source/ example-destination/
Enter fullscreen mode Exit fullscreen mode

If you find the error and it turns out to be something others might encounter definitely let me know and I'll make an update.

Collapse
 
tkroo profile image
tkroo

I found it helpful to see the rsync command:

// Begin the cronjob
console.log("command:" + rsync.command());
job.start();

Enter fullscreen mode Exit fullscreen mode
Collapse
 
suhakim profile image
sadiul hakim

Nice job.💖

Collapse
 
manojsworld profile image
Manoj Shukla

Great work....

Collapse
 
garvae profile image
Vova_Garvae

Awesome