Example of setting up scheduled backups
Objective
Create a script that will regularly launch a command-line client to archive and transfer critical data to S3.
Requirements
- a console client (in the example, S3cmd with the crontab automation tool);
- a cloud or dedicated server running Ubuntu 18.04 or higher;
- a user with access to S3.
Result
The script will create a backup of a file or directory using tar and upload the backup to S3 using s3cmd.
Steps
- Create a script.
- Transfer files to S3.
- Configure flow control.
- Check the script.
- Optional: automate backups using crontab or Cyberduck.
1. Create a script
-
Open the home directory on your server:
cd ~ -
Using the nano editor, create an empty file (for example, named
bkupscript):nano bkupscript.sh -
Start writing the backup script in a text editor with a shebang:
#!/bin/bashA shebang is an interpreter directive that allows scripts or data files to be run as commands and looks like a two-character sequence:
#and!. Thanks to the shebang at the beginning of the script, the shell runs the file's commands in bash. -
Below the shebang at the top of the text file, add variables to the script:
#!/bin/bashDATETIME=`date +%y%m%d-%H_%M_%S`SRC=$1DST=$2GIVENNAME=$3Where:
DATETIME— the timestamp to append to the resulting file name. Each file backed up in the space will have a unique name. This timestamp is generated by calling thedatecommand and formatting the output to display the last two digits of the year (% y), two digits of the month (% m), two digits of the day (% d), hour (% H), minutes (% M), and seconds (% S);SRC— the source path for the file or folder being backed up.$1indicates that the value is taken from the first parameter passed to the script;DST— the file destination. In the example, this is the name of the space where the backup is uploaded. This name will be obtained from the second parameter passed to the script, as specified in$2;GIVENNAME— the user-selected name for the destination file. The resulting file name will start withGIVENNAME, followed byDATETIME. This name comes from the third parameter passed to the script,$3.
-
Add the
showhelpfunction to the backup script to output messages if the script fails:#!/bin/bashDATETIME=`date +%y%m%d-%H_%M_%S`SRC=$1DST=$2GIVENNAME=$3showhelp(echo "\n This script will backup files/folders into a single compressed file and will store it in the current folder."} -
Collect the necessary files and combine them into a single package that can be uploaded:
#!/bin/bashDATETIME=`date +%y%m%d-%H_%M_%S`SRC=$1DST=$2GIVENNAME=$3showhelp(echo "\n This script will backup files/folders into a single compressed file and will store it in the current folder."){}tarandzip(){echo "\n##### Gathering files #####\n"tar -czvf $GIVENNAME-$DATETIME.tar.gz $SRC}When the
ifstatement is called, the script executes thetarcommand and waits for the result. If the command succeeds, the lines after thethenstatement will be executed:- outputting a message that the
tarprocess completed successfully; - returning exit code
0so that the part of the code calling this function knows everything is working properly.
The
elsepart of the script will execute only if thetarcommand encounters an error during execution:- outputting a message that the
tarcommand failed; - returning error code 1, which indicates that something went wrong.
- outputting a message that the
-
End the
if/then/elsescript withfi. The completedtarandzipfunction:tarandzip(){echo "\n##### Gathering files #####\n"if tar -czvf $GIVENNAME-$DATETIME.tar.gz $SRC; thenecho "\n##### Done gathering files #####\n"return 0elseecho "\n##### Failed to gather files #####\n"return 1fi}
2. Transfer files to S3
-
Add the
movetoSpacefile transfer function to the backup script to upload to the selected space. To create the command that will place backup files in the selected space, use the previously declared variables:movetoSpace(){/bin/s3cmd put $GIVENNAME-$DATETIME.tar.gz s3://$DST}Where:
/bin/s3cmd— calls s3cmd, a command-line tool used to manage object storage buckets;put— used by s3cmd to upload data to a bucket;$GIVENNAME-$DATETIME.tar.gz— the name of the backup file to be uploaded to the space. It consists of the fourth and first declared variables, followed by.tar.gz, and is created by thetarandzipfunction;s3://$DST;— the location where the file will be uploaded;s3://— a URI scheme used to describe object storage locations on the network, and$DST;is the third variable declared earlier.
-
Add a notification confirming that the file transfer process has started:
movetoSpace(){echo “\n##### MOVING TO SPACE #####\n”/bin/s3cmd put $GIVENNAME-$DATETIME.tar.gz s3://$DST} -
Configure the output of a message regarding the command execution result:
if /bin/s3cmd put $GIVENNAME-$DATETIME.tar.gz s3://$DST; thenecho "\n##### Done moving files to s3://"$DST" #####\n"return 0elseecho "\n##### Failed to move files to the Space #####\n"return 1fi
3. Configure flow control
If the script is configured correctly, when run, it should read the input command, assign values from it to each variable, execute the tarandzip function, and then execute movetoSpace.
If the script fails at any step, it should print the output of the showhelp function to help with troubleshooting.
Add an if / then / else conditional statement to the end of the file:
if [ ! -z "$GIVENNAME" ]; then
if tarandzip; then
movetoSpace
else
showhelp
fi
else
showhelp
fi
4. Check the script
-
Check the script:
#!/bin/bashDATETIME=`date +%y%m%d-%H_%M_%S`SRC=$1DST=$2GIVENNAME=$3showhelp(){echo "\n This script will backup files/folders into a single compressed file and will store it in the current folder."}tarandzip(){echo "\n##### Gathering files #####\n"if tar -czvf $GIVENNAME-$DATETIME.tar.gz $SRC; thenecho "\n##### Done gathering files #####\n"return 0elseecho "\n##### Failed to gather files #####\n"return 1fi}movetoSpace(){echo "\n##### MOVING TO SPACE #####\n"if /bin/s3cmd put $GIVENNAME-$DATETIME.tar.gz s3://$DST; thenecho "\n##### Done moving files to s3://"$DST" #####\n"return 0elseecho "\n##### Failed to move files to the Spac #####\n"return 1fi}if [ ! -z "$GIVENNAME" ]; thenif tarandzip; thenmovetoSpaceelseshowhelpfielseshowhelpfi -
Before exiting nano, close the file ( **Ctrl+X **) and save the changes ( **Y+Enter **).
5. Optional: automate backups
Crontab
Cyberduck
-
Set up a cron job that will use the script for regular backups to the selected space. In this example, the backup will be performed every minute.
-
Make the script executable:
chmod +x bkupscript.sh -
Edit the crontab file so that the script runs every minute:
crontab -e -
When running the
crontab -ecommand for the first time, you will be prompted to select an editor from the list:no crontab for root - using an empty oneSelect an editor. To change later, run "select-editor".1. /bin/ed2. /bin/nano <---- easiest3. /usr/bin/vim.basic4. /usr/bin/vim.tinyChoose 1-4 [2]: -
Choose nano by default or another text editor.
-
In crontab, add the line at the end of the script:
* * * * * ~/bkupscript.sh ~/backupthis nameofyourspace cronupload -
Close the file ( **Ctrl+X **) and save the changes ( **Y+Enter **).
-
If you leave the cron job running without changes, a new file will be copied to the selected space every minute. If cron is working successfully, reconfigure crontab to back up files at the required interval.
Cyberduck options
When automating backups with Cyberduck, you can use additional options:
-
compare— perform differential backups:duck --upload selectel:/<bucket_name>/<prefix> <path> --existing compare --username <selectel_account>:<username> --password <password>The uploaded backup will be compared with the existing one by size, modification date, and checksum. If the parameters differ, the old version will be deleted and the new one uploaded to storage;
-
skip— only new files will be uploaded to storage (those that appeared in the folder on the local machine after the previous upload). Existing files will not be uploaded, even if they were modified on the local machine; -
overwrite— deletes the existing backup from storage and uploads a new one.