In daily object management, you often need to synchronize local files to cloud storage to ensure safe backup and convenient access to data. Alibaba Cloud Object Storage Service (OSS) is a stable and reliable cloud storage that meets your requirements for object storage and management. This topic describes how to configure a scheduled synchronization task to automatically synchronize local files to OSS. This helps you manage objects in a more efficient and convenient manner.
Scenarios
Files in a directory named folder are added, deleted, and modified every day. You must synchronize the folder directory to OSS on a regular basis. To improve data transmission efficiency and prevent a large number of synchronized files from being repeatedly transferred, you want to upload only new files that are updated or created within a specific period of time.
In this case, you can use time-based filtering parameters, such as --max-age, to filter new files within a specific period of time based on your business requirements. This improves the efficiency of file transfer and achieves accurate file synchronization.
You can use destination filtering parameters, such as --update/-u, to exclude files that are already synchronized. This significantly improves the efficiency of file synchronization and reduces repeated operations and resource consumption.
Prerequisites
OSS is activated. For more information, see Get started with OSS.
An OSS bucket is created. For more information, see Create a bucket.
ossutil 2.0 is installed. For more information, see Install ossutil.
Procedure
Linux
Use Cron to configure a scheduled task to run the cp command
Open the terminal and enter the following command:
crontab -e
Configure a scheduled task to run the cp command to upload files that are updated or created within the previous seven days in the /path/folder path at 3:00 a.m. every day.
0 3 * * * ossutil cp /path/folder oss://your-bucket/path/a -r -f -u --max-age 7d
Use Cron to configure a scheduled task to run scripts
Create a shell script file named upload.sh and add the following scripts:
#!/bin/bash ossutil cp /path/folder oss://your-bucket/path/a -r -f -u --max-age 7d
Configure a scheduled task to run the scripts to upload files that are updated or created within the previous seven days in the /path/folder path at 3:00 a.m. every day.
0 3 * * * /path/to/upload.sh
Windows
Create a batch processing file and compile scripts.
Create a batch processing file, such as upload_to_oss.bat, and add the following scripts to upload all files in the folder subdirectory to the folder directory of a bucket named your-bucket:
cd /d "C:\ossutil" ossutil cp "C:\local\folder" "oss://your-bucket/folder" -r -u -f
Configure a task scheduler.
Search for and open Task Scheduler on your computer.
In the left-side navigation pane, select Task Scheduler Library and click Create Basic Task in the Actions column.
Follow the instructions in the wizard to specify the name of the task, the trigger (select the time and frequency of the task to be executed), and the actions (select the program or scripts to run, and specify the parameters).
In the left-side navigation pane, choose Action > Start a program and select the batch processing file (.bat file) that you created.
Test the scheduled task.
Double-click to run the batch processing file (.bat file) that you created.
Log on to the OSS console and check whether the files that you want to upload to the specified OSS path are displayed in the object list.
After the files are displayed in the object list, the scheduled task automatically runs the scripts at the specified time to notify ossutil to upload the files.