Search for previous posts

Blog History

8/24/20

AWS Ubuntu Unifi config backup to S3 via Rclone

There seems to be many strategies online for copying Unifi's .unf backup files off the controller, but I wanted something simple, clean, and lightweight. Here is the solution I decided upon, with the following three requirements:

  1. Not duplicate or overwrite files after they were uploaded
  2. Run everday
  3. Remove files after a certain set of days
The first is solved by Rclone's [1] copy switch, as it only copies "new or changed files to cloud storage". Caveat: the controller has to be running on a Linux host. If you use a Unifi Cloud Key or Windows, your steps will probably be different (Possibly a pull instead of a push).

First is to download and install Rclone on your instance:
curl https://rclone.org/install.sh | sudo bash

Next is the configuration of Rclone. As my controller was running on an EC2 instance, using an IAM instance profile [2] is easiest and best practice, but you can also just create a service user with AWS programmatic credentials. The below command will walk you through options, customize to your preferences. Mine was for AWS S3 [3]:

rclone config

I named my config UnifiBackup. Now make the S3 bucket:

rclone mkdir UnifiBackup:<bucket name>

Now you're ready to run the copy process:

sudo su

rclone copy /usr/lib/unifi/data/backup/autobackup UnifiBackup:<bucket name>

If everything is correctly configured, all your autobackup files should be in your S3 bucket. If so, follow these steps:

- Create a simple one line bash script from the above copy command 

- Modify the script to be executable:
 
    chmod +x <script name>

- Setup a cronjob [4] to run at your desired schedule. Make sure to edit root's cron schedule, as Ubiquiti has decided to lock down perms to the Unifi backups directory:

    sudo crontab -e

then

    0 2 * * * /home/ubuntu/<script name>

The third requirement I had was solved by AWS S3 "Object lifecycle" [5]. You set the objects to expire current version of object after __ days from object creation and then permanently delete previous versions after __ days from becoming a previous version.

Done.

References:

[1] https://rclone.org

[2] https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html

[3] https://aws.amazon.com/s3

[4] https://crontab.guru

[5] https://docs.aws.amazon.com/AmazonS3/latest/dev/object-lifecycle-mgmt.html

No comments:

Post a Comment