I am running my Raspberry 24/7 and would like to use a backup-script on a daily basis via a cronjob. That script puts certain contents together in an archive and should then store the archive somewhere. Can I just use a simple 8GB USB-Stick or is it better to use a SSD for example or a cloud based solution to push the archive, have it online?
-
Define better? Faster, Cost, etc.– Steve RobillardCommented Apr 12, 2020 at 20:58
-
I am already owning a SSD and a USB-Stick, so cost should be zero, concerning speed, there are no specific requirements. It is just one RaspberryPi Model 3 Model B.– quizmaster987Commented Apr 12, 2020 at 21:03
2 Answers
Yes - you can use a simple 8GB USB-Stick. That's what I do. I'm guessing you already know how to set this up, but to make this a more complete answer, here's an outline. If you have any questions, suggest you edit your question & leave a comment to my answer.
- Format your USB drive as
ext4
, then make an entry in/etc/fstab
. Here's an example:
LABEL=SANDISK8GB /home/pi/mntBackupDrv ext4 rw,user,nofail 0 0
- Decide what files & folders you want to back up, then create a
bash
script that usesrsync
to perform the backup. Here's an example script:
#!/bin/bash
rsync -av -F --delete /home/pi/ /home/pi/mntBackupDrv/rpi3b_backup/home/ >> /home/pi/mntBackupDrv/rpi3b_backup/home/rsync_home_backup.txt && \
/bin/echo -e "\nrsync completed: $(date)\n" >> /home/pi/mntBackupDrv/rpi3b_backup/home/rsync_home_backup.txt
The script is fairly simple. /home/pi
is the source, my USB thumb drive is the destination. The activity is logged to a file that's also on the USB drive along with a time stamp.
Read man rsync
for an explanation of the options; the -F
simply excludes some files & folders defined in files named .rsync-filter
.
If you want this stored an archive (tar), and maybe compressed, you could add that to the script - or use it instead of rsync
.
You should run this script manually to make sure it does what you want it to do before you schedule it with cron
. Also consider the use of the --dry-run
option in rsync
to verify that.
- Schedule your script to run using
crontab -e
. You could do something like this to backup your/home/pi
folder:
#----------- BACKUP `/home/pi` WITH `rsync` AT ./mntBackupDrv -----------
#
# NOTE: See root's crontab for the backup of /etc
#
55 1 * * * crontab -l > /home/pi/crontab_listing_home.txt
0 2 * * * /home/pi/bkup_pihome.sh
Notes re crontab
:
- This runs two jobs:
- at 01:55 each day, contents of the
crontab
are output to a file (this backs up mycrontab
) - at 02:00 each day, the script
/home/pi/bkup_pihome.sh
is run (see above for listing).
- at 01:55 each day, contents of the
- No
sudo
required for backing up/home/pi
- I also backup a few files in /etc - anything I might have changed extensively or frequently. That's not very many files, btw. That requires
sudo
; I do that withroot's crontab
; i.e.sudo crontab -e
, and a separate script.
Addendum:
I'd like to include this excellent reference for Using rsync and cron to automate incremental backups. There are several ideas relevant to a backup strategy in this post worth consideration.
-
Thank you so far. For formatting I used tecadmin.net/format-usb-in-linux \n What is the effect for the usb drive of the entry in /etc/fstab? Commented Apr 15, 2020 at 19:52
-
Concerning scheduling with a cronjob and the backup-script I got everything together, though I had to use the crontab of root
sudo su -
+crontab -e
since I want to backup folders in /etc as well. Did not use rsync just tar the content together. Commented Apr 15, 2020 at 19:55 -
One point - if you use ext4 for the format, you are basically forcing yourself to use Linux or extra drivers to read the USB stick. Formatting the stick as ExFAT allows Macs and PCs to read your backup data if you need to. The addition of ExFat on the Pi is documented all over (e.g. pimylifeup.com/raspberry-pi-exfat) also think about how you remove the backup - you need to make sure the data is written to the USB stick before pulling it - a backup in the same physical location is not a solid idea :-)– user115418Commented Apr 15, 2020 at 20:53
-
@Andyroo: True, but for me,
ext4
is the way to go. Storing backups onExFAT
seems a bit like keeping my money under the mattress :) But all kidding aside - yeah - if cross-platform access to your backups is an important consideration, then exFAT might fit the bill. And yes - alwaysumount
a mounted drive before pulling it from a running system.– SeamusCommented Apr 15, 2020 at 21:33 -
@Marius: Entry in
/etc/fstab
only means that when you "plug in" that particular USB drive, it is mounted by the system automatically. The alternative is to manually mount the drive before you use it.– SeamusCommented Apr 15, 2020 at 21:48
A NAS. or SSD. But if you your ability to configure it's your own personal cloud and can hold a lot of storage. I have thought about this for years and finally bought one. It's a learning and I riding that curve hard but I think it's worth the time energy, efficiency, and if it matters to you a lot protection. I explained it to my parents as a way to backup their computer just like onedrive wihout that added costs. But you now have onedrive, google drive (to an extent), Icloud (if you hvae an iphone) consolidated in one location. My phone tends to fill up quickly as well as my computer. I download apps and take photos and videos. Next thing you know you have to reinvent the wheel. With this my hope is you set it up accordingly, learn more about computers, and save money and time during those new computer trasnitions. I had a disaster last year. The back took 18 hours and it didn't fully work. Thats when I said NAS is the answer