Rclone backup all VPS to microsoft onedrive

In this tutorial, How to use Rclone backup all VPS to microsoft onedrive. It's very simple, free to backup all to Cloud with Rclone.

Rclone supports a lot of Cloud services

  • Google Drive
  • Amazon S3
  • Openstack Swift / Rackspace cloud files / Memset Memstore
  • Dropbox
  • Google Cloud Storage
  • Amazon Drive
  • Microsoft OneDrive
  • Hubic
  • Backblaze B2
  • Yandex Disk
  • SFTP
  • The local filesystem

Install Rclone on VPS

Link direct download here: I use Ubuntu server on VPS.

cd /root/
wget https://downloads.rclone.org/rclone-current-linux-amd64.zip
unzip rclone-current-linux-amd64.zip
cp rclone-v*-linux-amd64/rclone /usr/sbin/
rm -rf rclone-*

Rclone command basic

  • rclone config – Configure the connection to the cloud service.
  • rclone copy – Copy files from server to cloud, skip if data already exists.
  • rclone sync – Synchronize between server and cloud, only update data on cloud.
  • rclone move – Move files from server to cloud.
  • rclone delete – Delete the data of the folder.
  • rclone purge – Deletes the data of the folder and all its contents.
  • rclone mkdir – Create folder.
  • rclone rmdir – Delete empty folder at path.
  • rclone rmdirs – Delete all empty folders at the path.
  • rclone check – Check if the server and cloud data is in sync or not.
  • rclone ls – List all data including size and path.
  • rclone lsd – List entire directories.
  • rclone lsl – List all data including modification time, size and path.
  • rclone size – Returns the directory size.

Backup VPS to Cloud with Rclone

The first, config connection to microsoft onedrive. This only has to be done once.

rclone config
The output terminal as below:
root@localhost:~# rclone config
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> remote
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
 1 / 1Fichier
   \ "fichier"
 2 / Alias for an existing remote
   \ "alias"
 3 / Amazon Drive
   \ "amazon cloud drive"
 4 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, SeaweedFS, and Tencent COS
   \ "s3"
 5 / Backblaze B2
   \ "b2"
 6 / Box
   \ "box"
 7 / Cache a remote
   \ "cache"
 8 / Citrix Sharefile
   \ "sharefile"
 9 / Compress a remote
   \ "compress"
10 / Dropbox
   \ "dropbox"
11 / Encrypt/Decrypt a remote
   \ "crypt"
12 / Enterprise File Fabric
   \ "filefabric"
13 / FTP Connection
   \ "ftp"
14 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
15 / Google Drive
   \ "drive"
16 / Google Photos
   \ "google photos"
17 / Hadoop distributed file system
   \ "hdfs"
18 / Hubic
   \ "hubic"
19 / In memory object storage system.
   \ "memory"
20 / Jottacloud
   \ "jottacloud"
21 / Koofr
   \ "koofr"
22 / Local Disk
   \ "local"
23 / Mail.ru Cloud
   \ "mailru"
24 / Mega
   \ "mega"
25 / Microsoft Azure Blob Storage
   \ "azureblob"
26 / Microsoft OneDrive
   \ "onedrive"
27 / OpenDrive
   \ "opendrive"
28 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
29 / Pcloud
   \ "pcloud"
30 / Put.io
   \ "putio"
31 / QingCloud Object Storage
   \ "qingstor"
32 / SSH/SFTP Connection
   \ "sftp"
33 / Sugarsync
   \ "sugarsync"
34 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
35 / Transparently chunk/split large files
   \ "chunker"
36 / Union merges the contents of several upstream fs
   \ "union"
37 / Uptobox
   \ "uptobox"
38 / Webdav
   \ "webdav"
39 / Yandex Disk
   \ "yandex"
40 / Zoho
   \ "zoho"
41 / http Connection
   \ "http"
42 / premiumize.me
   \ "premiumizeme"
43 / seafile
   \ "seafile"
Storage> 26
OAuth Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>
OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret>
Choose national cloud region for OneDrive.
Enter a string value. Press Enter for the default ("global").
Choose a number from below, or type in your own value
 1 / Microsoft Cloud Global
   \ "global"
 2 / Microsoft Cloud for US Government
   \ "us"
 3 / Microsoft Cloud Germany
   \ "de"
 4 / Azure and Office 365 operated by 21Vianet in China
   \ "cn"
region> 1
Edit advanced config?
y) Yes
n) No (default)
y/n> n
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine

y) Yes (default)
n) No
y/n> n
For this to work, you will need rclone available on a machine that has
a web browser available.

For more help and alternate methods see: https://rclone.org/remote_setup/

Execute the following on the machine with the web browser (same rclone
version recommended):

        rclone authorize "onedrive"

Then paste the result.

Enter a string value. Press Enter for the default ("").
config_token> {"access_token":"
pwCAA8l6BAAU6k7+XVQzkGyMv7VHB/h4c3IVm81BwY4/ULMWzOHWLu24Wzehsgkq18g4RhWB16lEJMq/HBAk13vvozrhv4E8LmpNSZp30YCrl6A6wBHB0peKczPqMXizWjXGJ1UKEXGe9U+rxwmisoGFHvviqUlfItyhSQcfFP4bDnB6/aTdEShmhdO09E329irON9AZAZN8GY3xA7D/rjNT66ojI8Dy+GxOchC2YCKokxxPf6nzz/2nw6HrZ9hBJu7cDAgsCTsafvIWMDZgAACASD0ZJZZYHSUAIThL7dgXf9Tl4NC/IL44/3MbYZebzYoy7VN+Ej8W1vh9fqbCcVeejkG7gg6/C86DqTlO4OUFddYLXgfmOOxFtz5ktRO8aD/t99dr4aeb5BBhEnlniYSjxBzvytRybCA0s+XAIyDOS5gOQtr/CB+smTtveo+oeg+TzAnbLk8BqprYBU9GZ+qCfpFBx0PkcGxpyN2Vb0N3dg+BvGvnb0QEAXUur0gpwvjdZTryBKhhjutV1gaugUoxPPG89EkU4yO7/QuvnUS5+ppF0qtyDI53sJCCZ/Wmn7IyHqFXi/H1Ou6nkNCNvfGaf6qSQRggPlNNl8fMLGmW7DDiuCHPwTrecRhTYvo26tI+Fdx8AlULuh1Bn86/f7f0ciNZKvxSV1U9TTJ4LuJKOKQDLwtYc+SaUrkFG//9NRJmtz239Jlt5Llz3dSbR0e+aXBdfBzb1hSSk6fdaf2Ip+ddCtw3nLig8q0yZFmlhQe1VClLszYSoaspL59QANZadXNN7i47LsIJLU5yFAFOt0RkJxc2yb0VVp7lYBBZhg7pFunoO6Y4xcrPxbMqGQIgoDEZvBk0dLCi40RTnr9gSaE7xx8VnHldYr6w1BY+IaHPyPkJ4CtRNs2f21xLSaGdEMi0CQ0FCyz/h1LnOEimn+WBwBrZOte2v1+CVVmb7c+Vg92bTWnWux6Dpxpf8m7GxndG2MbxK7a78ISIsrDyzTZjO1DBRv6f0M76bRvwmUwPBdvH/l06fNefUZAtV7VVIt9tZx8BwPvQj8cJCtMRTREcZg89=","token_type":"Bearoer","refresh_token":"M.R3iiiii.-CdEs4SulZZ7XkBir126KUIZccWLh8KUdRK9m5Y4fzmNpDEc5NplDD4lZZpOZqjyZjDGsEwi826iHeZsTz7sUuv6XillExylF01vkclraRQD4vCenotlB*eTXMogsD0aa6pDv4VdmlwEfxiY7Hps4osH4XYdsDwPeb*uhil7PKaCrQZuYDvPWqtxtOvwDirfJwEIK6CZbA3P1UdkXy3Dnb6HjOD5dUPsL9xI3RMwhqM4dJJpSKC7VMfMJSPwbsnuI4c5DOKE7f71HEKwWnuOGoiLq5ljFB9VETczM3FXP8UcH1HVOTSeF6Ls5QWGL6pn05KIzqQywPHg83OHqCRRrcO6gXooooo2nk5HDTr","expiry":"2020-08-19T22:52:49.33462949+07:00"}
Type of connection
Enter a string value. Press Enter for the default ("onedrive").
Choose a number from below, or type in an existing value
 1 / OneDrive Personal or Business
   \ "onedrive"
 2 / Root Sharepoint site
   \ "sharepoint"
 3 / Sharepoint site name or URL (e.g. mysite or https://contoso.sharepoint.com/sites/mysite)
   \ "url"
 4 / Search for a Sharepoint site
   \ "search"
 5 / Type in driveID (advanced)
   \ "driveid"
 6 / Type in SiteID (advanced)
   \ "siteid"
 7 / Sharepoint server-relative path (advanced, e.g. /teams/hr)
   \ "path"
config_type> 1
Drive OK?

Found drive "root" of type "personal"
URL: https://onedrive.live.com/?cid=119105d1d4e6778

y) Yes (default)
n) No
y/n> y
--------------------
[remote]
type = onedrive
token = {"access_token":"QCAA8l6BAAU6k7+XVQzkGyMv7VHB/h4cHbJYRAAAYlXlM8Qu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refresh_token":"M..-CdEs4SulZZ7XkBir126KUIZccWLh8KUdRK9m5Y4fEc5NplDD4lZZpOZqjyZjDGsEwi826iHeZsTz7sUuv6XillExylF01vkclraRQD4vCenotlB*eTXMogsD0aa6pDv4VdmlwEfxiY7Hps4osH4XYdsDwPeb*uhil7PKaCrQZuYDvPWqtxtOvwDirfJwEIK6CZbA3P1UdkXy3Dnb6HjOD5dUPsL9xI3RMwhqM4dJJpSKC7VbsnuI4c5DOKE7f71HEKwWnuOGoiLq5ljFB9VETczM3FXP8UcH1HVOTSeF6Ls5QWGL6pn05KIzqQywPHg83OHqCRL0!jAO6gX2nk5HDTr","expiry":"2020-08-19T22:52:49.3346249+07:00"}
drive_id = 119105d1d4e6778
drive_type = personal
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:

Name                 Type
====                 ====
remote               onedrive

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q
root@localhost:~#
Note:
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/

Execute the following on the machine with the web browser (same rclone
version recommended):

        rclone authorize "onedrive"

Then paste the result.

 Run command "rclone authorize "onedrive" on Laptop or PC to get access_token

Confirm list folder on Microsoft onedrive
root@localhost:~# rclone lsd remote:
          -1 2021-08-19 15:41:26         0 Backup_Huuphan
          -1 2020-01-01 15:40:34         1 Desktop
          -1 2021-07-24 14:23:28         2 Documents
          -1 2021-02-15 20:17:00         3 Pictures
root@localhost:~#

Example, Backup DB VPS to Microsoft OneDrive

Create backup backupVPS.sh file with content as below:

#!/bin/bash

FOLDER_TARGET=Backup_Huuphan

TIMESTAMP=$(date +"%F")
BACKUP_DIR="/root/backup/$TIMESTAMP"
MYSQL=/usr/bin/mysql
MYSQLDUMP=/usr/bin/mysqldump
SECONDS=0

mkdir -p "$BACKUP_DIR/mysql"

echo "Starting Backup Database";
mydatabases=`$MYSQL -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql)"`

for mydb in $mydatabases; do
    $MYSQLDUMP --force --opt $mydb | gzip > "$BACKUP_DIR/mysql/$mydb.gz"
done
echo "Finished";
echo '';

size=$(du -sh $BACKUP_DIR | awk '{ print $1}')

echo "Uploading Backup";
/usr/sbin/rclone move $BACKUP_DIR "remote:$FOLDER_TARGET/$TIMESTAMP" >> /var/log/rclone.log 2>&1
# Clean up
rm -rf $BACKUP_DIR
/usr/sbin/rclone -q --min-age 2w delete "remote:$FOLDER_TARGET" #Remove all backups older than 2 week
/usr/sbin/rclone -q --min-age 2w rmdirs "remote:$FOLDER_TARGET" #Remove all empty folders older than 2 week
echo "Finished";
echo '';

duration=$SECONDS
echo "Total $size, $(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed."

Change permission it

chmod +x backupVPS.sh

auto backup with cron job

 crontab -e

Add the following

0 4 * * * /root/backupVPS.sh > /dev/null 2>&1 

Download backup from Cloud into your VPS

mkdir /tmp/restore
rclone copy "remote:/Backup_Huuphan/2020-08-20" /tmp/restore/

Comments

Popular posts from this blog

zimbra some services are not running [Solve problem]

Bash script list all IP addresses connected to Server

How to install php7 on centos 6