add scripts for db backup and upload to remote cloud stores

This commit is contained in:
Siddhartha 2022-09-15 10:53:10 +05:30
parent d3bc7616e9
commit 894bb7553d
6 changed files with 187 additions and 0 deletions

View File

@ -0,0 +1,33 @@
#!/bin/bash
echo "Creating database dumps..."
CURRENTDATETIME=`date +"%Y-%m-%d_%H_%M_%S"`
FILENAME_WITH_DIR="/home/ubuntu/backups/one800-social/incomplete-transactions/incomplete_transactions_"$CURRENTDATETIME
FILENAME="dump_"$CURRENTDATETIME
/usr/bin/docker exec -t postgres psql -d one800Studio -U one800 -c 'COPY (select row_number() over (order by a.id) as serial_no, a.mobile_number, b.title, a.device_age_in_months, c.title, (a.creation_time + 19800 * interval '\''1 second'\'') as creation_time, a.results_before_location, a.results as results_after_location, a.location from incomplete_transaction as a inner join device_view as b
on a.device_id = b.id inner join issue as c on c.id = a.issue_id order by a.id desc ) TO STDOUT WITH CSV HEADER' > $FILENAME_WITH_DIR.csv
if [ $? -eq 0 ]; then
echo "dump created successfully!"
else
echo "dump failed!"
fi
sleep 2
echo "running rclone copy..."
/usr/local/bin/rclone copy /home/ubuntu/backups/one800-social onedrive-social:Database-dumps
if [ $? -eq 0 ]; then
echo "copied to onedrive successfully!"
echo "removing local copy..."
sleep 2
rm $FILENAME_WITH_DIR.csv
echo "local file removed"
else
echo "rclone copy failed!"
fi

32
db-backups/order_tracking.sh Executable file
View File

@ -0,0 +1,32 @@
#!/bin/bash
echo "Creating order tracking dump..."
CURRENTDATETIME=`date +"%Y-%m-%d_%H_%M_%S"`
FILENAME_WITH_DIR="/home/ubuntu/backups/one800-social/order-tracking-history/order_tracking_"$CURRENTDATETIME
FILENAME="dump_"$CURRENTDATETIME
/usr/bin/docker exec -t postgres psql -d one800Studio -U one800 -c 'COPY (select id, order_id, tracking_status, (creation_time + 19800 * interval '\''1 second'\'') as creation_time from order_tracking) TO STDOUT WITH CSV HEADER' > $FILENAME_WITH_DIR.csv
if [ $? -eq 0 ]; then
echo "dump created successfully!"
else
echo "dump failed!"
fi
sleep 2
echo "running rclone copy..."
/usr/local/bin/rclone copy /home/ubuntu/backups/one800-social onedrive-social:Database-dumps
if [ $? -eq 0 ]; then
echo "copied to onedrive successfully!"
echo "removing local copy..."
sleep 2
rm $FILENAME_WITH_DIR.csv
echo "local file removed"
else
echo "rclone copy failed!"
fi

View File

@ -0,0 +1,31 @@
#!/bin/bash
echo "Creating postgres database dumps..."
CURRENTDATETIME=`date +"%Y-%m-%d_%H_%M_%S"`
FILENAME_WITH_DIR="/home/ubuntu/backups/postgres/dump_"$CURRENTDATETIME
FILENAME="dump_"$CURRENTDATETIME
/usr/bin/docker exec -t postgres pg_dumpall -c -U one800 > $FILENAME_WITH_DIR.sql
if [ $? -eq 0 ]; then
echo "dump created successfully!"
else
echo "dump failed!"
fi
echo "Uploading to b2..."
/usr/local/bin/b2 upload-file one800-backups $FILENAME_WITH_DIR.sql postgres/db-dumps/$FILENAME.sql --noProgress --quiet
if [ $? -eq 0 ]; then
echo "dump uploaded to b2 successfully!"
echo "removing the local dump file..."
sleep 2
rm $FILENAME_WITH_DIR.sql
echo "removed the local dump file successfully!"
else
echo "dump upload to b2 failed!"
fi

30
db-backups/repair_orders.sh Executable file
View File

@ -0,0 +1,30 @@
#!/bin/bash
echo "Creating repair orders dump..."
CURRENTDATETIME=`date +"%Y-%m-%d_%H_%M_%S"`
FILENAME_WITH_DIR="/home/ubuntu/backups/one800-social/repair-orders/repair-orders_"$CURRENTDATETIME
/usr/bin/docker exec -t postgres psql -d one800Studio -U one800 -c 'COPY (select a.id as order_id, row_number() over (order by a.id) as serial_no, b.name as user_name, b.mobile_number as user_mobile_number, f.title as device, g.title as issue, c.name as service_centre_name, c.area as service_centre_area, e.quote as service_centre_quotation, a.tax_amount as gst_amount, (a.pickup_delivery_charge + a.drop_delivery_charge) as delivery_charge, (a.repair_amount + a.tax_amount + a.pickup_delivery_charge + a.drop_delivery_charge) as total_amount, a.pickup_address, e.part_type, e.warranty_in_months, a.pickup_trip_id, a.drop_trip_id, h.payment_link as payment_link, h.status as payment_status, (a.creation_time + 19800 * interval '\''1 second'\'') as creation_time from repair_order as a inner join end_user as b on a.user_id = b.id inner join service_centre as c on a.service_centre_id = c.id inner join order_link_repair_quotation as d on a.id = d.order_id inner join repair_quotation as e on d.repair_quotation_id = e.id inner join device as f on e.device_id = f.id inner join issue as g on e.issue_id = g.id left join repair_payment as h on a.id = h.order_id order by a.id desc) TO STDOUT WITH CSV HEADER' > $FILENAME_WITH_DIR.csv
if [ $? -eq 0 ]; then
echo "dump created successfully!"
else
echo "dump failed!"
fi
sleep 2
echo "running rclone copy..."
/usr/local/bin/rclone copy /home/ubuntu/backups/one800-social onedrive-social:Database-dumps
if [ $? -eq 0 ]; then
echo "copied to onedrive successfully!"
echo "removing local copy..."
sleep 2
rm $FILENAME_WITH_DIR.csv
echo "local file removed"
else
echo "rclone copy failed!"
fi

30
db-backups/repair_quotations.sh Executable file
View File

@ -0,0 +1,30 @@
#!/bin/bash
echo "Creating repair quotations dump..."
CURRENTDATETIME=`date +"%Y-%m-%d_%H_%M_%S"`
FILENAME_WITH_DIR="/home/ubuntu/backups/one800-social/repair-quotations/repair-quotations_"$CURRENTDATETIME
/usr/bin/docker exec -t postgres psql -d one800Studio -U one800 -c 'COPY (select row_number() over (order by a.id) as serial_no, b.name as service_centre_name, d.title as issue, c.title as device, a.part_type, a.warranty_in_months, a.quote, a.tax_amount, (a.recent_updation_time + 19800 * interval '\''1 second'\'') as recent_updation_time, a.expired from repair_quotation as a inner join service_centre as b on a.service_centre_id = b.id inner join device as c on a.device_id = c.id inner join issue as d on a.issue_id = d.id order by a.id) TO STDOUT WITH CSV HEADER' > $FILENAME_WITH_DIR.csv
if [ $? -eq 0 ]; then
echo "dump created successfully!"
else
echo "dump failed!"
fi
sleep 2
echo "running rclone copy..."
/usr/local/bin/rclone copy /home/ubuntu/backups/one800-social onedrive-social:Database-dumps
if [ $? -eq 0 ]; then
echo "copied to onedrive successfully!"
echo "removing local copy..."
sleep 2
rm $FILENAME_WITH_DIR.csv
echo "local file removed"
else
echo "rclone copy failed!"
fi

31
db-backups/users_signed_up.sh Executable file
View File

@ -0,0 +1,31 @@
#!/bin/bash
echo "Creating the list of registered users..."
CURRENTDATETIME=`date +"%Y-%m-%d_%H_%M_%S"`
FILENAME_WITH_DIR="/home/ubuntu/backups/one800-social/onboarded-users/onboarded_users_"$CURRENTDATETIME
/usr/bin/docker exec -t postgres psql -d one800Studio -U one800 -c 'COPY (select row_number() over (order by id) as serial_no, id as user_id, name, slug as username, mobile_number, delivery_address, delivery_location_json as gps_coordinates, (creation_time + 19800 * interval '\''1 second'\'') as creation_time from end_user order by id desc) TO STDOUT WITH CSV HEADER' > $FILENAME_WITH_DIR.csv
if [ $? -eq 0 ]; then
echo "dump created successfully!"
else
echo "dump failed!"
fi
sleep 2
echo "running rclone copy..."
/usr/local/bin/rclone copy /home/ubuntu/backups/one800-social onedrive-social:Database-dumps
if [ $? -eq 0 ]; then
echo "copied to onedrive successfully!"
echo "removing local copy..."
sleep 2
rm $FILENAME_WITH_DIR.csv
echo "local file removed"
else
echo "rclone copy failed!"
fi