So, since my system crashes from time to time (mainly due messed up installations of either an OS or certain software) and I lost all my photos, project files, assets and other files, I wanted to do some kind of backup, up until now I simply copied, by hand, all new files to my home NAS to back it up. But this doesn't work for me as a solution. Now I was wondering if anyone knows a way to set a backup cycle in which Linux (openSUSE) copies the folder to the NAS (or rather looks if there are new files or updated files) and sends them to the NAS. I'm pretty sure that there's software in Windows for that but for Linux I haven't found anything. Best solution for me would be to control something like that via Terminal.
The easier way to do this using CLI utilities would be to use tar and cron. I'm going to walk you through a basic setup, then you can go from there.
Manually backing up a file/dir
You can manually backup a dir like this
tar -cvpzf /BackupDirectory/backupfilename.tar.gz /ImportantData/directory/path
Where
tar = tape archive
c = Create
v = Verbose mode
p = Preserving Files and Directory Permissions.
z = This will tell tar that compress the files further to reduce the size of tar file.
f = It allows tar to get file name.
Next step will be to create a script file (backup.sh) to automate the process:
#!/bin/bash
TIME=`date +%b-%d-%y` # This Command will add date in Backup File Name.
FILENAME=backup-$TIME.tar.gz # Here i define Backup file name format.
SRCDIR=/imp-data # Location of Important Data Directory (Source of backup).
DESDIR=/mybackupfolder # Destination of backup file.
tar -cpzf $DESDIR/$FILENAME $SRCDIR
Automating the backup process
To schedule a task in Linux you can use cron jobs. For setting up cron jobs we use crontab -e command in shell, the first time you use that command cron will ask you the default text editor, just pick the one you prefer. Open cronab editor utility:
crontab -e
Format of Crontab
It has 6 Parts:
Minutes Hours Day of Month Month Day of Week Command
0 to 59 0 to 23 1 to 31 1 to 12 0 to 6 Shell Command
Let’s assume I want to run the backup process on every Mon and Sat at 1 pm. Using the above syntax my crontab file should be something like this
M H DOM M DOW CMND
01 13 * * 1,6 /bin/bash /backup.sh
This script will run at 01:01:00 PM at every Monday and Saturday.