Linux Backup script for your save files

I looked through linux GSM and there is one thing missing a good backup script. In my case in Ubuntu I will be backing up locally and do a manual backup to a thumb drive and rotate thumb drives. Backup best practices are you have a local backup and an offsite backup a minimum of 50 miles away ( think a nuke goes off you have a clean copy of your files to restore worst case scenario.) In my case the thumb drives will be rotated between home and a separate property with a fireproof safe

first let us make the backup directory:
sudo mkdir /home/backup
now set us make the backup script:
sudo vi /usr/local/bin/backup.sh
Now copy in after hitting i to enter insert mode:

#!/bin/bash
systemctl stop 7days # this stops the server to do the backup
sleep 30 # give the system time to stop the service/server
SOURCE_DIR="/home/7days/7days/saves" # Replace with your game's directory
BACKUP_DIR="/home/backup" # Replace with your backup directory
TIMESTAMP=$(date +%Y%m%d%H%M%S)
BACKUP_NAME="game_backup_$TIMESTAMP"
FULL_BACKUP_PATH="$BACKUP_DIR/$BACKUP_NAME"

# Create backup directory if it doesn't exist
mkdir -p "$FULL_BACKUP_PATH"

# Copy game files using rsync
rsync -av "$SOURCE_DIR/" "$FULL_BACKUP_PATH/"

echo "Backup completed to $FULL_BACKUP_PATH"

# Optional: Remove old backups (e.g., keep only the last 7 days)
find "$BACKUP_DIR" -maxdepth 1 -type d -name "game_backup_*" -mtime +7 -exec rm -rf {} \;
systemctl start 7days # start the server backup after rsync and backup cleanups are done

hit escape to exit insert mode
type:
:wq

type:
chmod +x /usr/local/bin/backup.sh

Now add this as a crontab job to run at a set time. In my case I will do it at 5am as generally I am asleep or generally not playing at that hour:
sudo crontab -e

add in:
0 5 * * * /usr/local/bin/backup.sh

type ctrl+x then hit y then enter to exit.

I will add a later post when I do the USB transfer to show how to find the removable drive and then to copy a set of file to it.
 
OK, as far as transferring and using a usb drive.

first run:
sudo fdisk -l

In my case I get this output:

Disk /dev/loop8: 49.29 MiB, 51687424 bytes, 100952 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes


Disk /dev/sdb: 28.86 GiB, 30992760832 bytes, 60532736 sectors
Disk model: MassStorageClass
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device Boot Start End Sectors Size Id Type
/dev/sdb1 8192 60532735 60524544 28.9G c W95 FAT32 (LBA)

next run:
sudo lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop0 7:0 0 13.2M 1 loop /snap/canonical-livepatch/338
loop1 7:1 0 13.2M 1 loop /snap/canonical-livepatch/341
loop2 7:2 0 63.8M 1 loop /snap/core20/2582
loop3 7:3 0 63.8M 1 loop /snap/core20/2599
loop4 7:4 0 73.9M 1 loop /snap/core22/2010
loop5 7:5 0 73.9M 1 loop /snap/core22/2045
loop6 7:6 0 91.9M 1 loop /snap/lxd/32662
loop7 7:7 0 50.9M 1 loop /snap/snapd/24718
loop8 7:8 0 49.3M 1 loop /snap/snapd/24792
sda 8:0 0 279.4G 0 disk
├─sda1 8:1 0 278.4G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 976M 0 part [SWAP]
sdb 8:16 1 28.9G 0 disk
└─sdb1 8:17 1 28.9G 0 part
sr0 11:0 1 1024M 0 rom

And then run:
sudo findmnt | more

You get:
/ /dev/sda1 ext4 rw,relatime,errors=remount-ro,stripe=64
├─/sys sysfs sysfs rw,nosuid,nodev,noexec,relatime
│ ├─/sys/kernel/security securityfs securityfs rw,nosuid,nodev,noexec,relatime
│ ├─/sys/fs/cgroup cgroup2 cgroup2 rw,nosuid,nodev,noexec,relatime
│ ├─/sys/fs/pstore pstore pstore rw,nosuid,nodev,noexec,relatime
│ ├─/sys/fs/bpf bpf bpf rw,nosuid,nodev,noexec,relatime,mode=700
│ ├─/sys/kernel/debug debugfs debugfs rw,nosuid,nodev,noexec,relatime
│ │ └─/sys/kernel/debug/tracing tracefs tracefs rw,nosuid,nodev,noexec,relatime
│ ├─/sys/kernel/tracing tracefs tracefs rw,nosuid,nodev,noexec,relatime
│ ├─/sys/fs/fuse/connections fusectl fusectl rw,nosuid,nodev,noexec,relatime
│ └─/sys/kernel/config configfs configfs rw,nosuid,nodev,noexec,relatime
├─/proc proc proc rw,nosuid,nodev,noexec,relatime
│ └─/proc/sys/fs/binfmt_misc systemd-1 autofs rw,relatime,fd=33,pgrp=1,timeout=0,minpro
to=5,maxproto=5,direct,pipe_ino=7662
│ └─/proc/sys/fs/binfmt_misc binfmt_misc binfmt_misc rw,nosuid,nodev,noexec,relatime
├─/dev udev devtmpfs rw,nosuid,relatime,size=12237184k,nr_inod

How do you read that.

The loop listings are part of the OS updates and kernal setup.

In most cases your OS level drives are going to be of the form SDA or SDB. In my case the OS drive is SDA. If you know your storage and in my case it is dual 300 gig sas drives. So the OS/boot drive is
SDA1 OS
SDA2 header
SDA5 swap

Now I know the SDcard being used is 32gigs, minus formatting overhead it comes to 28.9 gigabytes. So we an see that it is SDB and more specfically sdb1.

findmount shows us that the usb drive is not mounted

Now where do you mount drives in linux? In the /mnt directory.
run:
sudo mkdir /mnt/sdb1

Now let us mount the drive:
sudo mount /dev/sdb1 /mnt/sdb1

Now let us copy the backup files:
In my case the backups I run via rsync are in /home/backup. Now all user profiles are in /home. all scripts are in /usr/local/bin

so we run:
sudo cp -r /usr/local/bin /mnt/sdb1/bin
sudo cp -r /home/backup /mnt/sdb1/backup
now we need to make backups of some specific config files
configs server firewall settings
sudo cp /etc/csf/csf.conf /mnt/sdb1/bin
network settings: ( this was to fix some boot issues with dhcp usetin netplan
sudo cp /etc/netplan/99_config.yaml /mnt/sdb1/bin

Make a complete backup of the home directory
sudo cp -r /home /mnt/sdb1/home (note you will get errors, but they are harmless, you are trying to get the bulk of files)
 
Now to safely unmount a drive in linux:
sudo umount /mnt/sdb1
Just remove the usb device and you are good with your backup.

Remember best practice is to have an onsite backup and an offsite backup a minimum of 50 miles away in case the worst happens. And if possible store them in a fireproof safe.
 
I just use duplicati. Easy peasy and all cloud services supported. Thumbdrives are soooo 1980.
Bear in mind my response is not going after you but pointing out fallacies on the mindset prevalent now.

The forced Parler shutdown by AWS due to bad politics is a great example of why cloud based is a bad idea if you are on the wrong side of party lines or history when those are ruling the roost:
That shutdown forced many enterprises to re-evaluate offshoring their IT infratructure and caused a massive build out frenzy as company after company keyed back up their own on premsis data centers so that they owned, possessed, and had complete control over THEIR data and systems. Microsoft and vmware are getting greedy now in light of this and there is now a bigger push to move more and more systems over to linux to avoid onerous and punitive licensing fees. Even entire governments and munincipalities in Europe and elsewhere are banning Microsoft from their enterprises to save on money and mandatory periodic upgrades.

But, in short:

If it is in the cloud, you do not own, nor possess it. Completely offline backups ensure a EM protection against destruction, vandal destruction of communications infrastructure rendering online backups impossible, hacker proof storage as it is air gapped from destruction or interference. Plus offline backups ensure no external parties can destroy your data because it encroaches on this or that objective. Apple and others have deleted files in cloud backups automatically and some files are scanned for and will cause automatic reports to agencies for followup if certain files or file structures are present. Offline backups ensure no government interference on your copy of files.

Air gaps are the number one way to secure networks and equipment. Not that it means much if you keep bluetooth on as theree are airgap attack exploit vectors using bluetooth and other obscure protocol driver hardware combinations.

Supposedly the F35 plans were stolen via tiktok on a phone sitting near an airgapped computer using an airgap attack. But if you have to worry about that level of sphisticated attack vectors you have bigger problems to worrry about.

But in everything plan for the worst, hope for the best, plan accordingly and nothing world ending will cause an unforseen unrecoverable trainwreck

Cloud backups do have their place though.

I am old school as I remember the early days of the net where stuff constantly disappeared and hardcore censorship going on realizing offline backups were the only solution to retain and safeguard knowledge and data. I find it disturbing we are moving back to the 90s era of internet disappearances of data and information.

I have first hand seen data disappear and seen it get wiped from the collective. So forgive me if I am not a fan of online only solutions, but understand that online backups are a piece or part of exhaustive data backup schema.
 
Back
Top