How to set up a simple backup on your UGREEN NAS using rsync & cron scheduler
As much as I love my UGREEN NAS, it doesn't come with a built-in app for backups. There's one, actually, but it only allows backups between different NASes or between your NAS and your PC. While I needed to set up a backup from one drive on my NAS to another drive on the same NAS.
I tried a few Docker containers with web UI, but I wasn't able to make them work, or they didn't work as I wanted (for example one app encrypted the backups, and I wasn't able to change this. And I didn't want my files encrypted, I just needed a simple copy).
So in the end, I decided to go with creating a script that runs rsync for a backup and schedule it with cron.
I just needed to back up my music folder from one drive to another. Here's how I did this:
1) Log in to your NAS via SSH.
2) Run:
sudo nano /usr/local/bin/backup_music.sh
3) The Nano editor will open, paste this there (update the script to use your paths)
SOURCE="/volume1/4TB_NVME/Music/"
DEST="/mnt/@usb/sdd1/Backups/Music/"
LOGFILE="/var/log/backup_music_errors.log"
# Ensure destination directory exists
mkdir -p "$DEST"
# Check if destination is mounted
if ! mountpoint -q /mnt/@usb/sdd1; then
logger -t backup_music "Error: destination is not mounted"
echo "Error: destination is not mounted" >> "$LOGFILE"
exit 1
fi
# Perform rsync backup, compatible with fuseblk (NTFS/exFAT)
sudo rsync -rlv --delete --no-perms --no-owner --no-group --no-times --progress "$SOURCE" "$DEST" 2>> "$LOGFILE"
if [ $? -eq 0 ]; then
logger -t backup_music "Music backup completed at $(date)"
echo "Music backup completed at $(date)" >> "$LOGFILE"
else
logger -t backup_music "Error during backup at $(date). Check $LOGFILE"
echo "Error during backup at $(date)" >> "$LOGFILE"
exit 1
fi
sudo chmod +x /usr/local/bin/backup_music.sh
sudo touch /var/log/backup_music_errors.log
sudo chmod 644 /var/log/backup_music_errors.log
sudo chown root:root /var/log/backup_music_errors.log
6) Test the script:
sudo /usr/local/bin/backup_music.sh
After it starts copying, press CTRL+C to stop it, check that some files have been copied to the destination and empty the destination directory after this test.
7) In my case the timezone on my NAS was incorrect, so I set the correct timezone:
sudo timedatectl set-timezone America/New_York timedatectl
8) Edit the root crontab to schedule the backup monthly:
sudo crontab -e
9) Paste this there:
0 2 1 * * /usr/local/bin/backup_music.sh
(0 2 1 * * means it will runs at 2:00 AM on the 1st day of every month.)
Ctrl + X & then Y to save and exit.
sudo service cron restart
11) Check the cron is running (active):
sudo systemctl status cron
12) Check the crontab:
sudo crontab -l
That should be it!
But I highly recommend testing that the scheduled job will actually execute, so let's temporarily update the cron schedule to something that will run in a few minutes. Let's say now is May 28th, 2025, 8:10pm and we want to run the job at 8:15pm. To do this:
1) sudo crontab -e
2) Change the schedule to
15 20 28 5 * /usr/local/bin/backup_music.sh
(this will run the script at 20:15 on May 28th, i.e. in 5 minutes)
CTRL + X, then Y to save and exit.
3) Wait until the time comes and make sure the backup has started.
4) If it's started successfully, change the schedule back:
sudo crontab -e
Change back to
0 2 1 * * /usr/local/bin/backup_music.sh
Save & exit.
5) Restart cron to update the schedule & also stop the running backup:
sudo service cron restart
6) Empty the destination directory, as it will have some backed up files left from our test.
7) Enjoy!
Comments
Post a Comment